Article content from: http://www.codeproject.com/gdi/gdionbitmaps.asp
Introduction: This article describes how to draw lines, graphics or text on a bitmap. It is very simple to give a convenient and quick reference to beginners.
Using HBitmap Operating Bitmap First We need a background bitmap, this bitmap is referenced by an HBitmap handle. As for how hbitmap gets, there can be many ways: Hbitmap previously operated; create returned with the CreateBitmap function; or imported by resource: hbitmap hbitmap =: lookbitmap (AFXGetInstanceHandle (), makeintResource (idb_bitmap1)); / / Loadbitmap loaded from the project's resources, the ID of the bitmap is IDB_bitmap1
Then pass the HBitmap handle, we can get some basic information about bitmaps: Bitmap BM; // bitmap object structure getObject (Hbitmap, sizeof (bitmap), & bm); // Return to the basic information of the HbitMap nature to the structure Object BM long width = bm.bmwidth; long heiGHT = bm.bmheight; // Get the height, width of bitmap
Then create a memory DC, and select a new bitmap.
BITMAPINFO bmInfo; // head structure defines a bitmap objects memset (& bmInfo.bmiHeader, 0, sizeof (BITMAPINFOHEADER)); bmInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER); bmInfo.bmiHeader.biWidth = width; bmInfo.bmiHeader.biHeight = Height; bminfo.bmiheader.BIPLANES = 1; bminfo.bmiheader.bibitcount = 24; // Create a temporary memory DC object HDC PDC = :: getDC (0); HDC TMPDC = CreateCompALPLEDC (PDC);
// Create a new bitmap using the bitmap structure object Bminfo, and select the created memory DCBYTE * PBASE; Hbitmap TmpBMP = Createdibsection (PDC, & Bminfo, DIB_RGB_COLORS, (Void **) & PBase, 0, 0); HgDiobj Tmpobj = selectObject (TMPDC, TMPBMP);
We can draw lines, write text or drawings on the TMPDC created. The following code is how to write text on a background bitmap:
// Put the background bitmap to the DC device HDC DCBMP = CreateCompatibleDC (TMPDC); hgdiobj tmpobj2 = selectObject (dcbmp, hbitmap); // Put hbitmap in memory Dcbitblt (TMPDC, 0, 0, Width, Height, DCBMP, 0 , 0, srcopy); // Copy the picture to the memory DCselectObject (TMPDC, TmpObj2); deletedc (dcbmp);
// Select Create Font CFont M_Font; LogFont * M_PLF; M_PLF = (LogFont *) Calloc (1, SizeOf (LogFont)); STRNCPY (M_PLF-> LFFACENAME, "Times New Roman", 31); M_PLF-> Lfheight = 64 ; m_plf-> lfWeight = 600; m_plf-> lfital = 1; m_plf-> lfunderLine = 0; m_font.createfontIndirect (m_PLF); // Create a font // with the logfont structure to select the created font object to DCCDC DC; Dc.attach (TMPDC); cfont * PoldFont = NULL; if (m_font.m_hobject) // Judgment font creation is successful {// If the creation is successful, select DC PoldFont = DC.SelectObject (& m_Font);} else {/// If creating is not successful, select the default font dc.selectObject (getStockObject (default_gui_font));}
// Set text color Dc.SetTextColor (RGB (60, 120, 240)); // Set the location of the add text Rect POS = {40, 40, 0, 0}; // Add text DC.SetBkMode (Transparent); dc.drawtext "TEST", 4, & POS, DT_CALCRECT); DC.DRAWText ("Test", 4, & POS, 0);
// Release and clear IF (PoldFont) {dc.selectObject (PoldFont);} m_font.deleteObject (); dc.detach (); free (m_plf);
There are two two bitmap objects: hbitmap and tmpBMP, you can save both new images, or replace hbitmap: deleteObject (hbitmap) with TMPBMP; hbitmap = tmpBMP;
Finally, we can delete the temporary DC. Do not delete hbitmap and tmpbmp, otherwise you may lose bitmap.
// Last resource clear SelectObject (TMPDC, Tmpobj); deletedc (TMPDC);
Conclusion: The article starts with a background image saved in hbitmap, and finally forms a new image, saved in a new hbitmap. This gives us a lot of flexibility during the operation: You can create images, such as CBITMAP, like FreeImage, and CXImage These graphics processing libraries, you can also add an effect or save pictures to files.