<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Library &#187; Bitmap</title>
	<atom:link href="http://www.ucosoft.com/tag/bitmap/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Mon, 19 Jul 2010 04:51:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Draw Bitmap from a bmp File?</title>
		<link>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html</link>
		<comments>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html#comments</comments>
		<pubDate>Fri, 05 Jan 2007 07:06:52 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[How-to]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/72.html</guid>
		<description><![CDATA[How to load a bitmap from bmp file and draw it in DC? There are two main steps: Load the bitmap from the file to a memory DC. Paint the content of the memory DC to the one which display the bitmap. In MFC, there is no API to load a bitmap directly from a]]></description>
			<content:encoded><![CDATA[<p>How to load a bitmap from bmp file and draw it in DC?</p>
<p>There are two main steps:</p>
<ul>
<li>Load the bitmap from the file to a memory DC.</li>
<li>Paint the content of the memory DC to the one which display the bitmap.</li>
</ul>
<p>In MFC, there is no API to load a bitmap directly from a file, but you can use a Win32 API: <strong>LoadImage</strong>.<br />
Here is the delaration of LoadImage.</p>
<p><coolcode lang="cpp" linenum="no"><br />
HANDLE LoadImage(<br />
HINSTANCE hinst, //handle to instance, if load from file, it should be NULL<br />
LPCTSTR lpszName, //image resource name or the image file path<br />
UINT uType, //image type<br />
//1. IMAGE_BITMAP<br />
//2. IMAGE_CURSOR<br />
//3. IMAGE_ICON<br />
int cxDesired, //desired width<br />
int cyDesired, //desired height<br />
UINT fuLoad //load options<br />
);<br />
</coolcode></p>
<p>After this,&nbsp; the following steps are similar with loading a bitmap from resource. Here is a short example.</p>
<p><span id="more-72"></span><br />
<coolcode lang="cpp" linenum="no" download="ShowBmp_demo.cpp"><br />
CClientDC dc(this);<br />
CDC *mdc=new CDC;<br />
mdc->CreateCompatibleDC(&#038;dc);</p>
<p>CBitmap bitmap;<br />
//CBitmap is devived from CGdiObject<br />
//It has a handle:m_hObject, which can obtained with LoadImage<br />
bitmap.m_hObject=(HBITMAP)::LoadImage(<br />
   NULL,<br />
   &#8220;b1.bmp&#8221;,<br />
   IMAGE_BITMAP,<br />
   500,<br />
   400,<br />
   LR_LOADFROMFILE);</p>
<p>mdc->SelectObject(bitmap);<br />
CRect rect;<br />
GetClientRect(&#038;rect);<br />
//BitBlt()<br />
dc.BitBlt(0,0,rect.right,rect.bottom,mdc,0,0,SRCCOPY);</p>
<p>//release mdc<br />
delete mdc;<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/bitmap" title="Bitmap" rel="tag">Bitmap</a>, <a href="http://www.ucosoft.com/tag/cpp" title="Cpp" rel="tag">Cpp</a>, <a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/ucogrid-a-great-grid-control-of-mfc.html" title="ucoGrid, a great grid control of MFC (November 26, 2006)">ucoGrid, a great grid control of MFC</a> (18)</li>
	<li><a href="http://www.ucosoft.com/sscanf-with-cstring.html" title="sscanf with CString (March 20, 2007)">sscanf with CString</a> (3)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/set-the-head-backgroud-color-of-cpropertypage.html" title="Set the head backgroud color of CPropertyPage (October 18, 2007)">Set the head backgroud color of CPropertyPage</a> (0)</li>
	<li><a href="http://www.ucosoft.com/moving-selection-of-clistctrl.html" title="Moving selection of CListCtrl (December 3, 2006)">Moving selection of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-subclass-ctreectrl-in-ctreeview.html" title="How to subclass CTreeCtrl in CTreeView? (October 25, 2007)">How to subclass CTreeCtrl in CTreeView?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sort-items-of-clistctrl-or-clistview.html" title="How to sort items of CListCtrl or CListView? (October 22, 2007)">How to sort items of CListCtrl or CListView?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-program-to-retrieve-the-authenticode-information.html" title="How to retrieve the authenticode information (December 31, 2006)">How to retrieve the authenticode information</a> (3)</li>
	<li><a href="http://www.ucosoft.com/how-to-read-the-graphics-card-ram-capacity.html" title="How to read the graphics card RAM capacity (December 18, 2006)">How to read the graphics card RAM capacity</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-draw-bitmap-from-a-bmp-file.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple code to create an EMF or Bitmap file from existed draw code</title>
		<link>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html</link>
		<comments>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html#comments</comments>
		<pubDate>Sun, 17 Dec 2006 02:04:30 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[emf]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/58.html</guid>
		<description><![CDATA[We often need to create an Enhanced Meta File(EMF) or Bitmap(bmp) from an existed DC draw code. It&#8217;s not very difficult but it&#8217;s boring to code again and again. The following is a simple solution, what you need to do is only to implement an IPicDrawer and call DrawEnhMeta or DrawBitmap function. struct IPicDrawer {]]></description>
			<content:encoded><![CDATA[<p>We often need to create an Enhanced Meta File(EMF) or Bitmap(bmp) from an existed DC draw code. It&#8217;s not very difficult but it&#8217;s boring to code again and again.<br />
The following is a simple solution, what you need to do is only to implement an <strong>IPicDrawer</strong> and call <strong>DrawEnhMeta</strong> or <strong> DrawBitmap</strong> function.</p>
<p><span id="more-58"></span><br />
<!--adsense--><br />
<coolcode lang="cpp" linenum="no" download="demo_src.cpp"><br />
struct IPicDrawer<br />
{<br />
  virtual void GetSize(CSize&#038;) = 0; // It&#8217;s the smallest size for EMF<br />
  virtual BOOL Draw(CDC*) = 0;<br />
};</p>
<p>BOOL DrawEnhMeta(LPCTSTR lpszFileName, IPicDrawer *pIDrawer)<br />
{<br />
  // Draw EMF file, the limitation of DC function refer to MSDN<br />
  HDC hDC = ::CreateEnhMetaFile(NULL, lpszFileName, NULL,<br />
      _T(&#8220;FQ EMF Func\0No Title\0&#8243;));<br />
  CDC *pDC = CDC::FromHandle(hDC);<br />
  if(pDC)<br />
  {<br />
    CSize sz;<br />
    pIDrawer->GetSize(sz);<br />
    pDC->FillRect(CRect(0,0,sz.cx,sz.cy), &#038;CBrush(RGB(255,255,255)));<br />
    if(pIDrawer->Draw(pDC))<br />
    {<br />
      HENHMETAFILE hEmf = ::CloseEnhMetaFile(hDC);<br />
      if(hEmf)<br />
      {<br />
        ::DeleteEnhMetaFile(hEmf);<br />
        return TRUE;<br />
      }<br />
      else<br />
        ::DeleteFile(lpszFileName);<br />
    }<br />
  }<br />
  return FALSE;<br />
}</p>
<p>BOOL DrawBitmap(LPCTSTR lpszFileName, IPicDrawer *pIDrawer)<br />
{<br />
  // Draw 24 bit true color bitmap file<br />
  HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_READ | GENERIC_WRITE,<br />
    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);<br />
  BOOL bRet = FALSE;<br />
  if(hFile)<br />
  {<br />
    CSize sz;<br />
    pIDrawer->GetSize(sz);<br />
    BITMAPINFO BmpInfo = {0, }; //<br />
    BmpInfo.bmiHeader.biWidth = sz.cx;<br />
    BmpInfo.bmiHeader.biHeight = sz.cy;<br />
    BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);<br />
    BmpInfo.bmiHeader.biPlanes = 1;<br />
    BmpInfo.bmiHeader.biBitCount = 24; // 24bit<br />
    BmpInfo.bmiHeader.biCompression = BI_RGB;<br />
    BmpInfo.bmiHeader.biSizeImage = BmpInfo.bmiHeader.biBitCount / 8 *<br />
      BmpInfo.bmiHeader.biWidth * BmpInfo.bmiHeader.biHeight;</p>
<p>    BITMAPFILEHEADER bmpFileHeader = {0, }; //<br />
    bmpFileHeader.bfType = *(WORD*)_T(&#8220;BM&#8221;);<br />
    bmpFileHeader.bfOffBits =<br />
      sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);<br />
    bmpFileHeader.bfOffBits +=<br />
      sizeof(DWORD) &#8211; (bmpFileHeader.bfOffBits % sizeof(DWORD));<br />
    bmpFileHeader.bfSize = bmpFileHeader.bfOffBits + BmpInfo.bmiHeader.biSizeImage;<br />
    //write file header and image information<br />
    DWORD dwNum = 0;<br />
    bRet = ::WriteFile(hFile, &#038;bmpFileHeader, sizeof(BITMAPFILEHEADER),<br />
      &#038;dwNum, NULL);<br />
    bRet = bRet &#038;&#038; ::WriteFile(hFile, &#038;(BmpInfo.bmiHeader), sizeof(BITMAPINFOHEADER),<br />
      &#038;dwNum, NULL);<br />
    if(bRet)<br />
    {<br />
      bRet = FALSE;<br />
      //create file mapping<br />
      HANDLE hMap = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE,<br />
        0, bmpFileHeader.bfSize, NULL);<br />
      if(hMap)<br />
      {<br />
        // create bitmap object<br />
        void *pBits = NULL;<br />
        HBITMAP hBMP = ::CreateDIBSection(NULL, &#038;BmpInfo, DIB_RGB_COLORS,<br />
          &#038;pBits, hMap, bmpFileHeader.bfOffBits);<br />
        if(hBMP)<br />
        {<br />
          // draw the specified figure to file<br />
          CDC memTmpDC;<br />
          memTmpDC.CreateCompatibleDC(NULL);<br />
          if(memTmpDC.GetSafeHdc())<br />
          {<br />
            CBitmap *pbmp;<br />
            pbmp = CBitmap::FromHandle(hBMP);<br />
            if(pbmp)<br />
            {<br />
              memTmpDC.SelectObject(pbmp);<br />
              memTmpDC.FillRect(CRect(0,0,sz.cx,sz.cy), &#038;CBrush(RGB(255,255,255)));<br />
              bRet = pIDrawer->Draw(&#038;memTmpDC);<br />
            }<br />
          }<br />
          ::DeleteObject(hBMP);<br />
        }<br />
        ::CloseHandle(hMap);<br />
      }<br />
    }<br />
    ::CloseHandle(hFile);<br />
  }<br />
  if(!bRet)<br />
    ::DeleteFile(lpszFileName);<br />
  return bRet;<br />
}<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/bitmap" title="Bitmap" rel="tag">Bitmap</a>, <a href="http://www.ucosoft.com/tag/emf" title="emf" rel="tag">emf</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/write-and-read-binary-data-in-variant.html" title="Write and read binary data in VARIANT (November 22, 2006)">Write and read binary data in VARIANT</a> (0)</li>
	<li><a href="http://www.ucosoft.com/ucogrid-a-great-grid-control-of-mfc.html" title="ucoGrid, a great grid control of MFC (November 26, 2006)">ucoGrid, a great grid control of MFC</a> (18)</li>
	<li><a href="http://www.ucosoft.com/sscanf-with-cstring.html" title="sscanf with CString (March 20, 2007)">sscanf with CString</a> (3)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/simple-cedit-validation.html" title="Simple CEdit Validation (June 27, 2007)">Simple CEdit Validation</a> (0)</li>
	<li><a href="http://www.ucosoft.com/moving-selection-of-clistctrl.html" title="Moving selection of CListCtrl (December 3, 2006)">Moving selection of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-translate-among-cstringstring-and-char-array.html" title="How to translate among CString,string and char array (June 22, 2007)">How to translate among CString,string and char array</a> (0)</li>
	<li><a href="http://www.ucosoft.com/get-clistctrl-item-text-of-another-process.html" title="How to get ClistCtrl item text of another process (September 24, 2008)">How to get ClistCtrl item text of another process</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-check-whether-the-window-is-in-minimise-or-maximise-mode.html" title="How to check whether the window is in minimise or Maximise mode (March 21, 2007)">How to check whether the window is in minimise or Maximise mode</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-change-the-row-height-of-clistctrl.html" title="How to change the row height of CListCtrl (December 5, 2006)">How to change the row height of CListCtrl</a> (1)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
