Sign in to follow this  
DesignerX

Win32 api questions !

Recommended Posts

DesignerX    174
1. How can I save a bitmap to a file using the WinAPI functions ? (I have an uncompressed array of RGB values) 2. If I want to get a pixel color from a bitmap at a specified coordinate (like 100, 100), what is the fastest way to get it ? (I used GetPixel after selecting a bitmap to a device context but it worked very very slow !) 3. When I use StretchBlt() function I get some small distortions when bitmap is painted (You can see the complete bitmap but there are some undesired color distortions), what may cause this ? Thanks for your help !

Share this post


Link to post
Share on other sites
Endurion    5407
1)

Alas there is no SaveBitmap function in the API. You may have to write a binary file with the headers yourselves. (Lookup Wotsit for the format, it's also in the MSDN).

2)

You might consider using GetDIBits if you have a standard bitmap which will provide you with a pointer to the pixel data. You could also use CreateDIBSection for creating the bitmap in the first place (which you have to provide a pointer to the pixel data beforehand).

Plus: Faster, since direct access to pixel data
Minus: Different color/pixel formats, different getpixel code.

3)

Look into SetStretchBltMode, you probably want COLORONCOLOR.

Share this post


Link to post
Share on other sites
Empirical    190
Here are the functions I use to save a bitmap. You only need to use SaveImgToDisc with a compatible hDC your bitmap and file name.

Its code i got from somewhere or other so unsupported, but give it a try anyway!

PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp);

cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
if (cClrBits != 24) pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));
else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));

// Initialize the fields in the BITMAPINFO structure.

pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24) pbmi->bmiHeader.biClrUsed = (1<<cClrBits);

// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8* pbmi->bmiHeader.biHeight;
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}

void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
{
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;

pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS);
// Create the .BMP file.
hf = CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL);
WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, NULL);
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL);
CloseHandle(hf);
GlobalFree((HGLOBAL)lpBits);
return;
}
bool SaveImgToDisc(HDC hDC, HBITMAP hBitmap,char * Filename)
{
CreateBMPFile(Filename, CreateBitmapInfoStruct(hBitmap),hBitmap,hDC);
return true;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this