Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open and modify bitmap

Status
Not open for further replies.

mound

Programmer
Jan 11, 2006
1
0
0
US
Using VC++ 6.0 and MFC, can somebody show me a function that will do the following:

Given a path to an 8bit grayscale bitmap

1. open the file
2. increase its height by 30pix at the bottom.
3. Fill that new 30px "bar" at the bottom with white
4. Draw a given line of text in that new 30pix high whitespace, in black.
4. Save the file back to disc.

(basically open the existing file and expand it's height to accomodate a 30pixel high bar of whitespace in which I can draw text)

thanks!
 
I am unfarmiliar with MFC. However, I DO know how to use the MS WinAPI, which is what the MFC are based on, to do exactly what you want. If it's a stand-alone application, you might think about trying WinAPI code. I show how to do this below...



You'd want to start by creating a new bitmap of the size you want. I don't know how to change the original bitmap's size directly, but the other method will yield equivilant and satisfying results. After you create a new bitmap, you can use BitBlt to copy the original image into the new one, and the rest should automatically be filled with the default white. If not, you'll have to fill it in yourself. Here is some code that will help get you started.

LPTSTR filename = "C:/..."; //whatever the path is

HBITMAP hBitmap = LoadImage(0, filename, 0, 0, 0, LR_LOADFROMFILE);

BITMAP bmp;

GetObject(hBitmap, sizeof(bmp), &bmp);

HDC hdcBitmap = CreateCompatibleDC(0);

SelectObject(hdcBitmap, hBitmap);

//that much will load the bitmap so you can copy it

HDC hdcNewBitmap = CreateCompatibleDC(hdcBitmap);

//this is where you expand it by 30 pixels
HBITMAP hNewBitmap = CreateCompatibleBitmap(hdcBitmap, bmp.bmWidth, bmp.bmHeight + 30);

SelectObject(hdcNewBitmap, hNewBitmap);

//this is where you copy the original image into the new one
BitBlt(hdcNewBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);

That much will get you a handel to the bitmap you're looking for. If you're looking to save it to a disc, you'll have to do some research because that's beyond my scope of knowledge, but all that code should work. You can check it my blitting it to a window.

hWnd = CreateWindow(szProgName, "Bitmap Test", WS_OVERLAPPEDWINDOW, 0, 0, bmp.bmWidth, bmp.bmHeight, (HWND)NULL, (HMENU)NULL, hInst, (LPSTR)NULL);

HDC hdcScreen = getWindowDC(hWnd);

BitBlt(hdcScreen, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcNewBitmap, 0, 0, SRCCOPY);

Once this works, you can use paint functions to write the text to the bitmap handel, such as BeginPaint, TextOut, EndPaint, and so on.

Let me know if you have any problems. Hopefully someone else can give a straighter answer for MFC.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top