<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[speichern einer Bitmap - was fehlt?]]></title><description><![CDATA[<p>Hallo,<br />
mit folgendem Code, zusammengesetzt aus dem was ich so in Tutorials gefunden habe, möchte ich ein CBitmap Objekt &quot;m_bmpBitmap&quot; in einer Datei speichern, aber etwas entscheidendes fehlt. Es läuft, speichert aber ein leeres (schwarzes) Bitmap der korrekten Größe in Pixeln ab obwohl vorher eine Bitmap eingeladen wurde, die sich auch anzeigen, manipulieren etc, lässt. Wahrscheinlich mache ich irgend einen ganz dummen Fehler bei der Übergabe von &quot;hdc&quot;; vermutlich sind die Zeilen 12 und 13 auch Nonsens?.<br />
Ich weiß auch, daß es schöne fertige Bibliotheken für sowas gibt, aber trotzdem würde ich gerne wissen was ich hier falsch mache.<br />
Vielleicht kann mir ja jemand aus dem Forum auf die Sprünge helfen.</p>
<pre><code class="language-cpp">void CMainFrame::OnDateiBitmapspeichern() 
{

   //m_bmpBitmap vom Typ CBitmap enthält die zu speichernde Bitmap
   BITMAP bm; 
   m_bmpBitmap.GetBitmap(&amp;bm);

   int width = bm.bmWidth;
   int height = bm.bmHeight;

   HDC hdc;
   hdc = CreateCompatibleDC(hdc);
   SelectObject(hdc, m_bmpBitmap);

   char* filename;
   filename = &quot;C:/temp/testsave.bmp&quot;;

   DC2Bitmap(hdc, width, height, filename);

}

int CMainFrame::DC2Bitmap(HDC hdc, int width, int height, char *filename)
{

   HDC hdc2; 
   HBITMAP aBmp;

   BITMAPINFO bi; 
   HGDIOBJ OldObj; 
   void *dibvalues; 
   HANDLE fileHandle; 

   BITMAPFILEHEADER bmfh; 
   BITMAPINFOHEADER bmih; 
   DWORD bytes_write; 
   DWORD bytes_written; 

   hdc2=CreateCompatibleDC(hdc); 

   ZeroMemory(&amp;bmih,sizeof(BITMAPINFOHEADER)); 
   bmih.biSize=sizeof(BITMAPINFOHEADER); 
   bmih.biHeight=height; 
   bmih.biWidth=width; 
   bmih.biPlanes=1; 
   bmih.biBitCount=24; 
   bmih.biCompression=BI_RGB; 
   bmih.biSizeImage = ((((bmih.biWidth * bmih.biBitCount) + 31) 
                           &amp; ~31) &gt;&gt; 3) * bmih.biHeight; 

   bmih.biXPelsPerMeter = 0; 
   bmih.biYPelsPerMeter = 0; 
   bmih.biClrImportant = 0; 

   bi.bmiHeader=bmih; 

   aBmp=CreateDIBSection(hdc,&amp;bi,DIB_RGB_COLORS,(void**)&amp;dibvalues,0,0);

   if(aBmp==NULL) 
   { 
       OutputDebugString(&quot;CreateDIBSection failed!\n&quot;); 
       return 0; 
   } 

   OldObj=SelectObject(hdc2,aBmp); 
   BitBlt(hdc2,0,0,width,height,hdc,0,0,SRCCOPY); 

   ZeroMemory(&amp;bmfh,sizeof(BITMAPFILEHEADER)); 
   bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
   bmfh.bfSize=(3*bmih.biHeight*bmih.biWidth)+sizeof(BITMAPFILEHEADER) 
                                             +sizeof(BITMAPINFOHEADER); 

   bmfh.bfType=0x4d42; 
   bmfh.bfReserved1 = 0; 
   bmfh.bfReserved2 = 0; 

   fileHandle=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,(DWORD)0,NULL, 
                        CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL); 

   if (fileHandle==INVALID_HANDLE_VALUE) 
   { 
       OutputDebugString(&quot;CreateFile failed!\n&quot;); 
       return 0; 
   } 

   bytes_write=sizeof(BITMAPFILEHEADER); 

   if (!WriteFile(fileHandle,(void*)&amp;bmfh,bytes_write,&amp;bytes_written,NULL)) 
   { 
       OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   } 

   bytes_write=sizeof(BITMAPINFOHEADER); 

   if(!WriteFile(fileHandle,(void*)&amp;bmih,bytes_write,&amp;bytes_written,NULL)) 
   { 
        OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   }     

   bytes_write=bmih.biSizeImage; 

   if(!WriteFile(fileHandle,(void*)dibvalues, 
              bytes_write,&amp;bytes_written,NULL)) 
   { 
       OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   } 

   CloseHandle(fileHandle); 

   DeleteObject(SelectObject(hdc2,OldObj)); 
   DeleteDC(hdc2); 
   return 1; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/219684/speichern-einer-bitmap-was-fehlt</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 07:24:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/219684.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 Aug 2008 11:26:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Wed, 06 Aug 2008 11:26:10 GMT]]></title><description><![CDATA[<p>Hallo,<br />
mit folgendem Code, zusammengesetzt aus dem was ich so in Tutorials gefunden habe, möchte ich ein CBitmap Objekt &quot;m_bmpBitmap&quot; in einer Datei speichern, aber etwas entscheidendes fehlt. Es läuft, speichert aber ein leeres (schwarzes) Bitmap der korrekten Größe in Pixeln ab obwohl vorher eine Bitmap eingeladen wurde, die sich auch anzeigen, manipulieren etc, lässt. Wahrscheinlich mache ich irgend einen ganz dummen Fehler bei der Übergabe von &quot;hdc&quot;; vermutlich sind die Zeilen 12 und 13 auch Nonsens?.<br />
Ich weiß auch, daß es schöne fertige Bibliotheken für sowas gibt, aber trotzdem würde ich gerne wissen was ich hier falsch mache.<br />
Vielleicht kann mir ja jemand aus dem Forum auf die Sprünge helfen.</p>
<pre><code class="language-cpp">void CMainFrame::OnDateiBitmapspeichern() 
{

   //m_bmpBitmap vom Typ CBitmap enthält die zu speichernde Bitmap
   BITMAP bm; 
   m_bmpBitmap.GetBitmap(&amp;bm);

   int width = bm.bmWidth;
   int height = bm.bmHeight;

   HDC hdc;
   hdc = CreateCompatibleDC(hdc);
   SelectObject(hdc, m_bmpBitmap);

   char* filename;
   filename = &quot;C:/temp/testsave.bmp&quot;;

   DC2Bitmap(hdc, width, height, filename);

}

int CMainFrame::DC2Bitmap(HDC hdc, int width, int height, char *filename)
{

   HDC hdc2; 
   HBITMAP aBmp;

   BITMAPINFO bi; 
   HGDIOBJ OldObj; 
   void *dibvalues; 
   HANDLE fileHandle; 

   BITMAPFILEHEADER bmfh; 
   BITMAPINFOHEADER bmih; 
   DWORD bytes_write; 
   DWORD bytes_written; 

   hdc2=CreateCompatibleDC(hdc); 

   ZeroMemory(&amp;bmih,sizeof(BITMAPINFOHEADER)); 
   bmih.biSize=sizeof(BITMAPINFOHEADER); 
   bmih.biHeight=height; 
   bmih.biWidth=width; 
   bmih.biPlanes=1; 
   bmih.biBitCount=24; 
   bmih.biCompression=BI_RGB; 
   bmih.biSizeImage = ((((bmih.biWidth * bmih.biBitCount) + 31) 
                           &amp; ~31) &gt;&gt; 3) * bmih.biHeight; 

   bmih.biXPelsPerMeter = 0; 
   bmih.biYPelsPerMeter = 0; 
   bmih.biClrImportant = 0; 

   bi.bmiHeader=bmih; 

   aBmp=CreateDIBSection(hdc,&amp;bi,DIB_RGB_COLORS,(void**)&amp;dibvalues,0,0);

   if(aBmp==NULL) 
   { 
       OutputDebugString(&quot;CreateDIBSection failed!\n&quot;); 
       return 0; 
   } 

   OldObj=SelectObject(hdc2,aBmp); 
   BitBlt(hdc2,0,0,width,height,hdc,0,0,SRCCOPY); 

   ZeroMemory(&amp;bmfh,sizeof(BITMAPFILEHEADER)); 
   bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
   bmfh.bfSize=(3*bmih.biHeight*bmih.biWidth)+sizeof(BITMAPFILEHEADER) 
                                             +sizeof(BITMAPINFOHEADER); 

   bmfh.bfType=0x4d42; 
   bmfh.bfReserved1 = 0; 
   bmfh.bfReserved2 = 0; 

   fileHandle=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,(DWORD)0,NULL, 
                        CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL); 

   if (fileHandle==INVALID_HANDLE_VALUE) 
   { 
       OutputDebugString(&quot;CreateFile failed!\n&quot;); 
       return 0; 
   } 

   bytes_write=sizeof(BITMAPFILEHEADER); 

   if (!WriteFile(fileHandle,(void*)&amp;bmfh,bytes_write,&amp;bytes_written,NULL)) 
   { 
       OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   } 

   bytes_write=sizeof(BITMAPINFOHEADER); 

   if(!WriteFile(fileHandle,(void*)&amp;bmih,bytes_write,&amp;bytes_written,NULL)) 
   { 
        OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   }     

   bytes_write=bmih.biSizeImage; 

   if(!WriteFile(fileHandle,(void*)dibvalues, 
              bytes_write,&amp;bytes_written,NULL)) 
   { 
       OutputDebugString(&quot;WriteFile failed!\n&quot;); 
       return 0; 
   } 

   CloseHandle(fileHandle); 

   DeleteObject(SelectObject(hdc2,OldObj)); 
   DeleteDC(hdc2); 
   return 1; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1560160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1560160</guid><dc:creator><![CDATA[polaris]]></dc:creator><pubDate>Wed, 06 Aug 2008 11:26:10 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Wed, 06 Aug 2008 20:08:35 GMT]]></title><description><![CDATA[<p>Versuchs mal in einem anderes Unterforum. z.B Grafik/Spieleprogrammierung, oder auch das WinAPI Unterforum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1560458</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1560458</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 06 Aug 2008 20:08:35 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Wed, 06 Aug 2008 20:11:42 GMT]]></title><description><![CDATA[<p>polaris schrieb:</p>
<blockquote>
<p>mit folgendem Code, zusammengesetzt aus dem was ich so in Tutorials gefunden habe,</p>
</blockquote>
<p>Programmieren bedeutet nicht, etwas mit Copy&amp;Paste zusammenzufügen, sondern selber was tippen und verstehen was man macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1560460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1560460</guid><dc:creator><![CDATA[Rolf Imo]]></dc:creator><pubDate>Wed, 06 Aug 2008 20:11:42 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Wed, 06 Aug 2008 20:56:34 GMT]]></title><description><![CDATA[<p>Rolf Imo schrieb:</p>
<blockquote>
<p>polaris schrieb:</p>
<blockquote>
<p>mit folgendem Code, zusammengesetzt aus dem was ich so in Tutorials gefunden habe,</p>
</blockquote>
<p>Programmieren bedeutet nicht, etwas mit Copy&amp;Paste zusammenzufügen, sondern selber was tippen und verstehen was man macht.</p>
</blockquote>
<p>...und Antworten in Foren bedeutet zum Thema schreiben, nicht dumme Kommentare - ein altes Foren Problem.<br />
Ok, wenns einige beruhigt, 95% des Gesamtprojektes (eher mehr) stammen aus der papiernen Literatur, sind eigenhändig eingetippt und abgewandelt, und funktionieren, aber mit dem speichern einer Bitmap kam ich halt nicht weiter. Dazu, daß ich Anfänger bin, stehe ich.<br />
Rolf, ich bin auf Deine Lösung gespannt - oder weißt Du es selber nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1560485</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1560485</guid><dc:creator><![CDATA[polaris]]></dc:creator><pubDate>Wed, 06 Aug 2008 20:56:34 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Thu, 07 Aug 2008 18:37:05 GMT]]></title><description><![CDATA[<p>polaris schrieb:</p>
<blockquote>
<p>Rolf, ich bin auf Deine Lösung gespannt - oder weißt Du es selber nicht?</p>
</blockquote>
<p>Zweifelst du etwa an meinen Highendüberpro Skills? Na dann zeig ich dir jetzt mal wie das geht.... lol, n billigerer Trick fällt dir nicht ein, um an die Lösung zu kommen.</p>
<p>PS: <a href="http://www.google.de/search?hl=de&amp;q=rofl+imo+&amp;btnG=Suche&amp;meta=" rel="nofollow">Wer ist Rolf</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1561044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1561044</guid><dc:creator><![CDATA[Rolf Imo]]></dc:creator><pubDate>Thu, 07 Aug 2008 18:37:05 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Thu, 07 Aug 2008 18:39:53 GMT]]></title><description><![CDATA[<p>PPS: Lies mal Dokus dazu und benutz nen Debugger. Oder Google solang weiter bis du ne funktionierende Lösung findest. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1561045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1561045</guid><dc:creator><![CDATA[Rolf Imo]]></dc:creator><pubDate>Thu, 07 Aug 2008 18:39:53 GMT</pubDate></item><item><title><![CDATA[Reply to speichern einer Bitmap - was fehlt? on Sat, 09 Aug 2008 16:07:56 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-403.html" rel="nofollow">HumeSikkins</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-4.html" rel="nofollow">WinAPI</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1561889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1561889</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Sat, 09 Aug 2008 16:07:56 GMT</pubDate></item></channel></rss>