Overview
A program was developed a few days ago to synthesize text and bitmap images and save them as bitmap files. However, no one gave me a satisfactory answer after asking for a long time, so I had to explore it in the dark. It took a lot of effort to synthesize the image and text, and drag the text with the mouse. Now I post the code, hoping to help anyone who needs it.
Key functions
INT setdibits (HDC, // compatible DC handle hbitmap hbmp, // compatible with bitmap handle uint ustartscan, // scan the number of the starting line uint cscanlines, // number of rows scanned const void * lpvbits, // bitmap buffer pointer, requires that the allocated space const bitmapinfo * lpbmi, // bitmap information uint fucoloruse // color format );
This function posts bitmap data in one pixel and one pixel to a compatible bitmap. HDC is compatible with bitmap-compatible DC.
Int getdibits (HDC, // compatible DC handle hbitmap hbmp, // compatible with bitmap handle uint ustartscan, // scan the number of the starting line uint cscanlines, // Number of scanned rows lpvoid lpvbits, // bitmap buffer pointer, requires the allocated space lpbitmapinfo lpbi, // bitmap information uint uusage // color format );
This function is opposite to SetDIBits. It copies the existing data in a compatible bitmap to the data buffer zone (lpvBits) in one pixel per pixel. In this way, the implementation of the program is clearer. First, create a compatible bitmap based on the size of the bitmap, then draw the image to the compatible bitmap, and then write the text to the compatible bitmap, then, the synthesized data is obtained from the compatible bitmap.
Key program sectionCreatememobject (CDC * PDC, bitmapinfo * lpbmi, int * pnlinechar) {If (null = m_pmemdc) {m_pmemdc = new CDC (); m_pmemdc-> createcompatibledc (PDC );} if (m_pmembmp! = NULL) {m_pmemdc-> SelectObject (m_poldbmp); Delete m_pmembmp; m_pmembmp = NULL;} m_pmembmp = new cbitmap (); // create a compatible bitmap m_pmembmp> createcompatiblebitmap (PDC, m_lpbmphdr-> biwidth, m_lpbmphdr-> biheight) based on the image size; m_poldbmp = m_pmemdc-> SelectObject (m_pmembmp ); m_pmemdc-> fillsolidrect (0, 0, m_lpbmphdr-> biwidth, m_lpbmphdr-> biheight, RGB (255,255,255 )); // draw image data to a compatible bitmap. setdibits (m_pmemdc-> getsafehdc (), (hbitmap) m_pmembmp-> m_hobject, 0, m_lpbmphdr-> biheight, (lpvoid) m_lpdibits, (bitmapinfo *) m_lpbmphdr, dib_rgb_colors);} setbmp text (const cstring & strtext, bool brepaint) {// Add text m_strbmp text = strtext to the in-place map of this function; setdibits (m_pmemdc-> getsafehdc (), (hbitmap) m_pmembmp-> m_hobject, 0, blocks-> biheight, (lpvoid) m_lpdibits, (bitmapinfo *) m_lpbmphdr, callback ); if (m_strbmp text.getlength ()> 0) {// create transparent and ghosting effects m_pmemdc-> setbkmode (transparent); m_pmemdc-> settextcolor (RGB (0, 0, 0 )); m_pmemdc-> textout (10, 10, m_strbmp text); m_pmemdc-> settextcolor (RGB (255,255,255); m_pmemdc-> textout (11, 11, m_strbmp text);} If (brepainting) {invalidate () ;}} setsavebmppath (lpctstr pszpath) {// retrieve the Synthesized Data byte * ptemp = new byte [m_dwimagesize]; zeromemory (ptemp, m_dwimagesize) from the compatible bitmap ); int ncount = getdibits (m_pmemdc-> getsafehdc (), (hbitmap) m_pmembmp-> m_hobject, 0, bytes-> biheight, (lpvoid) ptemp, (bitmapinfo *) m_lpbmphdr, dib_rgb_colors );}