Local database reading edition

Displaying Windows Bitmap (*.bmp) file in VC++

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Visual C++ Programming

Opening post

skpradhan · 2001-04-29 11:23:42

Here is an explanation for how to display a bitmap file in VC++. You could display a bitmap file in VC++ in different ways. I have given two sample codes below. 1. You can import the .bmp file to the project resource as a bitmap and use that resource to display it. This is something like statically displaying a bitmap, as the bitmap itself is included in the project and hence in the application EXE file too. 2. Dynamically loading a bitmap file on-fly, and displaying it. I have given two sample codes below for both of the above methods. For testing the sample code you can create a simple SDI project in VC++ and copy the codes to OnDraw() function of your view class. Sample code for method 1: CBitmap bmp; //Load the bitmap from resource(ex : IDB_BITMAP1) bmp.LoadBitmap(IDB_BITMAP1); //get the bitmap header info BITMAP bm; bmp.GetBitmap(&bm); //create a temporary memory DC CDC mdc; mdc.CreateCompatibleDC(pDC); mdc.SelectObject(&bmp); //Use CDC::BitBlt() or StretchBlt(); to draw. Here I have used BitBlt() here //If you want to enhance/reduce the bitmap display use StretchBlt() pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&mdc,0,0,SRCCOPY); //delete the temp DC when finished mdc.DeleteDC(); NOTE: for this method you need to first import your bitmap file to the project resource. Sample code for method 2: //load the bitmap directly from file HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(), "C:\\winnt\\winnt.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //attach it to a CBitmap object CBitmap bmp; if(hBitmap) { bmp.Attach(hBitmap); } //get the bitmap header info BITMAP bm; bmp.GetBitmap(&bm); //create a temporary memory DC CDC mdc; mdc.CreateCompatibleDC(pDC); mdc.SelectObject(&bmp); //Use CDC::BitBlt() or StretchBlt(); here i have used BitBlt() here pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&mdc,0,0,SRCCOPY); //delete the temp DC when finished mdc.DeleteDC(); Regards SKP Santanu Kumar Pradhan

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 146