Archived

This topic is now archived and is closed to further replies.

Saving bmp in windows

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a simple dialog box program that takes the filename of a bitmap, a couple of user defined dimensions, and then saves small sections of the bitmap to a new file. I can load the bitmap using LoadImage, and I know how to retrieve information about the bitmap using GetObject, but I cannot find any useful information about saving sections of the bitmaps to new files. The App doesn''t need to show the bitmap, just save sections of it in a new bitmap file. Can anyone point me to information on a function that can do this (WITHOUT HAVING TO CREATE A WINDOW OR USE DIRECTX)????? Cheers, pan narrans

Share this post


Link to post
Share on other sites
I dig the caps and multiple question marks, and understand that you must have an answer immediately or your spleen will rupture.

You do not have to use DirectX, but you do need to get access to the bitmap, using GetDIBits.

Check these out.
http://www.codeproject.com/bitmap/gditutorial.asp
http://www.geocities.com/foetsch/screenshot/screenshot.htm#step1


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
OK, thanx to the previous posts I now have a function that can save a bitmap to any filename given a particular HBITMAP, and BITMAPINFO structure.

However, I want to save segments of the initial HBITMAP and I am still unsure of how to create a new HBITMAP containing only a segment of the first.

Any ideas, peeps?

Thanx,
pan narrans

Share this post


Link to post
Share on other sites
Create another hbitmap and bind a dc to it. Then bitblt the section you want onto the new hbitmap. Then convert the hbitmap to a dib then save. I have to goto class right now... if you need any code I''ll post them when I get back.

Share this post


Link to post
Share on other sites
I have the following:


  
// Access desktop DC

HDC hScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
// Create a compatible DC to hold our bitmap

HDC hCompDC = CreateCompatibleDC(hScreen);
// Get width and height of desktop

int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeight = GetSystemMetrics(SM_CYSCREEN);
// Create bitmap to receive the screenshot

HBITMAP hBmp = CreateCompatibleBitmap(hScreen, nWidth, nHeight);
// Select bitmap into target DC

HBITMAP hOld = (HBITMAP)SelectObject(hCompDC, hBmp);
// Blit bits from screen to target buffer

BitBlt(hCompDC, 0, 0, 256, 256, hScreen, 0, 0, SRCCOPY);
// De-select bitmap

SelectObject(hCompDC, hOld);

// Save

BITMAPINFO BitmapInfo = *CreateBitmapInfoStruct(hBmp);
if(!CreateBMPFile(g_SaveFileName, &BitmapInfo, hBmp, hCompDC))
{
MessageBox(0, "Failed to save image.",
"Error!", MB_OK | MB_ICONEXCLAMATION);
}

// Free DCs

DeleteDC(hScreen);
DeleteDC(hCompDC);


This saves a 256*256 portion of the screen to a bitmap. How can I load a bitmap to the first HDC?

Share this post


Link to post
Share on other sites
this should load a bmp into a memory device context, return the context and store image width/size in the supplied vars.
from the memory dc you can then blit to wherever you want.

and perhaps you should add some error checking to my function



  

HDC LoadBMP ( char* bmpfile, int* width, int* height )
{
// load bitmap into a bitmap handle

HBITMAP hBmp = LoadImage ( NULL, bmpfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );

// create memory dc

HDC dcmem = CreateCompatibleDC ( NULL );

// now select bitmap into the memory dc

SelectObject ( dcmem, hBmp );

// now get the bmp size

BITMAP bm;
GetObject ( hBmp, sizeof(bm), &bm );

*width = bm.bmWidth;
*height = bm.bmHeight;

return dcmem
}


and if you want to load a bmp thats included as a resource just leave out the LR_LOADFROMFILE




------------
Runicsoft

Share this post


Link to post
Share on other sites