Windows question - efficient way to write a text on a loaded bitmap?
-
Hi Let's say I have a pointer to a BITMAPINFO structure, and the structure holds information about an RGB bitmap in memory. Is there a way for me to directly write some string on top of the bitmap? Currently I do it like this: HDC hDC = CreateCompatibleDC(NULL); void *pDIB; HBITMAP hBmp = CreateDIBSection(hDC, pBmpInfo, DIB_RGB_COLORS, &pDIB, NULL, NULL); //pBmpInfo in of type BITMAPINFO* memcpy(pDIB, pData, iWidth * iHeight * 3); //iWidth and iHeight are the width and height of the image, respectively, and pData is a pointer to the bitmap's pixel values, multiplied by 3 because it's RGB SelectObject(hDC, hBmp); DrawText(hDC, "This is a test", strlen(cstrStatus), &CRect(5, 5, iWidth, iHeight), DT_LEFT); memcpy(pData, pDIB, iWidth*iHeight*3); DeleteObject(hBmp); DeleteDC(hDC); So basically I first copy the bitmap's pixel values to a buffer created using CreateDIBSection, write the text on the DIB, and copy the DIB pixel values back to the original bitmap pixels' location. Is there a more efficient way to do this without copying the pixel values back and forth between the two buffers (something like writing the text directly to pData)? Due to the way the system is designed, my function must work on preloaded BITMAPINFO* input that resides in memory, not bitmap image files or other types of input. And because my system needs all the speed it can get, any tips on how to make the above code faster will be greatly appreciated. Thanks!
-
Hi Let's say I have a pointer to a BITMAPINFO structure, and the structure holds information about an RGB bitmap in memory. Is there a way for me to directly write some string on top of the bitmap? Currently I do it like this: HDC hDC = CreateCompatibleDC(NULL); void *pDIB; HBITMAP hBmp = CreateDIBSection(hDC, pBmpInfo, DIB_RGB_COLORS, &pDIB, NULL, NULL); //pBmpInfo in of type BITMAPINFO* memcpy(pDIB, pData, iWidth * iHeight * 3); //iWidth and iHeight are the width and height of the image, respectively, and pData is a pointer to the bitmap's pixel values, multiplied by 3 because it's RGB SelectObject(hDC, hBmp); DrawText(hDC, "This is a test", strlen(cstrStatus), &CRect(5, 5, iWidth, iHeight), DT_LEFT); memcpy(pData, pDIB, iWidth*iHeight*3); DeleteObject(hBmp); DeleteDC(hDC); So basically I first copy the bitmap's pixel values to a buffer created using CreateDIBSection, write the text on the DIB, and copy the DIB pixel values back to the original bitmap pixels' location. Is there a more efficient way to do this without copying the pixel values back and forth between the two buffers (something like writing the text directly to pData)? Due to the way the system is designed, my function must work on preloaded BITMAPINFO* input that resides in memory, not bitmap image files or other types of input. And because my system needs all the speed it can get, any tips on how to make the above code faster will be greatly appreciated. Thanks!
Indrawati wrote: Is there a more efficient way to do this without copying the pixel values back and forth between the two buffers (something like writing the text directly to pData)? nope. not if you're using the GDI text functions - they require DCs, and DCs require HBITMAPs and HBITMAPs require you to SelectObject them, etc.. if you want to get into 3rd party text-rendering engines, you might be able to avoid it, but that's probably a lot more trouble than it's worth. Software | Cleek