HANDLE hData = Bitmap2Dib(hbitmap, wbitcount); // Hbitmap convert to bitmap
UINT nSize = GlobalSize(hData);
CMemFile* fo= new CMemFile((BYTE *)GlobalLock(hData), nSize);
// 内存文件转化为流
IStream* pStmBmp = NULL;
int iBmpSize = fo->GetLength();
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL)
{
return FALSE;
}
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
if (pStmBmp == NULL)
{
GlobalFree(hMemBmp);
return false;
}
HRESULT stat;
CImage *image = new CImage();
image->Create(bitmap.bmWidth, bitmap.bmHeight, wbitcount);
// Load IStream Fail
HRESULT hResult = image->Load(pStmBmp);
if(S_OK != hResult)
{
return false;
}
stat = image->Save(lpfilename, ImageFormatJPEG);
if(S_OK != stat)
{
return false;
}
pStmBmp->Release();
delete image;
1,我想把HBITMAP在内存中转化成为jpg格式的图像文件保存。结果在HRESULT hResult = image->Load(pStmBmp); load IStream的时候出错,不知道是什么原因?
2,我看MSDN上说Load的参数是pStream
A pointer to a stream containing the name of the image file to load.
我的指针里只是包含了图像信息并没有保存图像的名字,不知道在流里应该怎样加图像的名字?
15 个解决方案
#1
sf
#2
流中没有数据
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL)
{
return FALSE;
}
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
另外用gdi+ 简单多了
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL)
{
return FALSE;
}
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
另外用gdi+ 简单多了
#3
GDI+
#4
GDI+怎么做,可以说得详细一些吗?谢谢
#5
pStmBmp->Write((BYTE *)GlobalLock(hData), nSize, &nByteWriten);
我加了这样一句,还是不可以,image->Load(pStmBmp); 的时候返回值是E_FAIL.
究竟是怎么回事呀!大家帮帮忙吧!急死了!!!
我加了这样一句,还是不可以,image->Load(pStmBmp); 的时候返回值是E_FAIL.
究竟是怎么回事呀!大家帮帮忙吧!急死了!!!
#6
VOID Example_SaveFile(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object based on a PNG file.
Image image(L"Mosaic.png");
// Draw the image.
graphics.DrawImage(&image, 10, 10);
// Construct a Graphics object based on the image.
Graphics imageGraphics(&image);
// Alter the image.
SolidBrush brush(Color(255, 0, 0, 255));
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
// Draw the altered image.
graphics.DrawImage(&image, 200, 10);
// Save the altered image.
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
image.Save(L"Mosaic2.png", &pngClsid, NULL);
}
#7
GDI+应该可以
#8
倦怠,这样是可以实现的,但是现在要求的是从IStream流中读取一幅图,并不是从一个文件中读取一幅图
Image image(L"Mosaic.png");
这句如果换成这样能成功的话就OK了
Image* imImage = NULL;
imImage->FromStream(pStmBmp, FALSE);
从pStmBmp里读取一幅图。
Image image(L"Mosaic.png");
这句如果换成这样能成功的话就OK了
Image* imImage = NULL;
imImage->FromStream(pStmBmp, FALSE);
从pStmBmp里读取一幅图。
#9
直接用COleStreamFile就好了
#10
问题解决了,原来很简单,谢谢大家了,结帖!
#11
解决了也不说下如何解决的……
#12
bs
#13
此istream非彼IStream
后者是接口类
后者是接口类
#14
不好意思,怎么没有贴上解决的办法。是这么解决的:最后HBITMAP没有转化成IStream。而是直接转化为Bmp格式。代码如下:
bool CMPRCtrl::SaveBitmapToFile(HBITMAP hbitmap , LPSTR lpfilename)
{
CAtlString strImgType;
strImgType = m_pImgType;
HDC hdc;
int nBits;
// how many character of one pixel own while in the current resolution ratio
WORD wbitcount;
// how many character of on pixel own in the bitmap
DWORD dwpalettesize = 0;
// the size of color palette
DWORD dwbmbitssize;
// the size of bitmap
DWORD dwdibsize;
// how many character write to file
DWORD dwwritten;
// the property of bitmap file
BITMAP bitmap;
// information about the type, size, and layout of a file that contains a DIB
BITMAPFILEHEADER bmfhdr;
// information about the dimensions and color format of a DIB
BITMAPINFOHEADER bi;
// the pointer point to bitmap information
LPBITMAPINFOHEADER lpbi;
HANDLE fh = NULL;
HANDLE hdib = NULL;
HANDLE hpal = NULL;
HANDLE holdpal=NULL;
hdc = CreateDC("display", NULL, NULL, NULL);
// how many character of on pixel own in the bitmap
nBits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (nBits <= 1)
{
wbitcount = 1;
}
else if (nBits <= 4)
{
wbitcount = 4;
}
else if (nBits <= 8)
{
wbitcount = 8;
}
else if (nBits <= 16)
{
wbitcount = 16;
}
else if (nBits <= 24)
{
wbitcount = 24;
}
else
{
wbitcount = 32;
}
// calculate the size of color palette
if (wbitcount <= 8)
{
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
}
// set the information of bitmap
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount + 31) / 32) * 4 * bitmap.bmHeight;
// allocate core storage
hdib = GlobalAlloc(GHND, dwbmbitssize + dwpalettesize
+ sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// process the palette
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// get the new pixel of palette
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
// set bitmap file
bmfhdr.bfType = 0x4d42;
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
if ("jpg" == strImgType)
{
HRESULT stat;
CImage *image = new CImage();
image->Attach(hbitmap);
stat = image->Save(lpfilename, ImageFormatJPEG);
if(S_OK != stat)
{
return false;
}
image->Detach();
delete image;
}
// create bitmap file
if ("bmp" == strImgType)
{
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
{
GlobalUnlock(hdib);
GlobalFree(hdib);
return false;
}
// write image to bitmap file
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// write the information to bitmap file
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
CloseHandle(fh);
}
GlobalUnlock(hdib);
GlobalFree(hdib);
return true;
}
bool CMPRCtrl::SaveBitmapToFile(HBITMAP hbitmap , LPSTR lpfilename)
{
CAtlString strImgType;
strImgType = m_pImgType;
HDC hdc;
int nBits;
// how many character of one pixel own while in the current resolution ratio
WORD wbitcount;
// how many character of on pixel own in the bitmap
DWORD dwpalettesize = 0;
// the size of color palette
DWORD dwbmbitssize;
// the size of bitmap
DWORD dwdibsize;
// how many character write to file
DWORD dwwritten;
// the property of bitmap file
BITMAP bitmap;
// information about the type, size, and layout of a file that contains a DIB
BITMAPFILEHEADER bmfhdr;
// information about the dimensions and color format of a DIB
BITMAPINFOHEADER bi;
// the pointer point to bitmap information
LPBITMAPINFOHEADER lpbi;
HANDLE fh = NULL;
HANDLE hdib = NULL;
HANDLE hpal = NULL;
HANDLE holdpal=NULL;
hdc = CreateDC("display", NULL, NULL, NULL);
// how many character of on pixel own in the bitmap
nBits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (nBits <= 1)
{
wbitcount = 1;
}
else if (nBits <= 4)
{
wbitcount = 4;
}
else if (nBits <= 8)
{
wbitcount = 8;
}
else if (nBits <= 16)
{
wbitcount = 16;
}
else if (nBits <= 24)
{
wbitcount = 24;
}
else
{
wbitcount = 32;
}
// calculate the size of color palette
if (wbitcount <= 8)
{
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
}
// set the information of bitmap
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount + 31) / 32) * 4 * bitmap.bmHeight;
// allocate core storage
hdib = GlobalAlloc(GHND, dwbmbitssize + dwpalettesize
+ sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// process the palette
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// get the new pixel of palette
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
// set bitmap file
bmfhdr.bfType = 0x4d42;
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
if ("jpg" == strImgType)
{
HRESULT stat;
CImage *image = new CImage();
image->Attach(hbitmap);
stat = image->Save(lpfilename, ImageFormatJPEG);
if(S_OK != stat)
{
return false;
}
image->Detach();
delete image;
}
// create bitmap file
if ("bmp" == strImgType)
{
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
{
GlobalUnlock(hdib);
GlobalFree(hdib);
return false;
}
// write image to bitmap file
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// write the information to bitmap file
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
CloseHandle(fh);
}
GlobalUnlock(hdib);
GlobalFree(hdib);
return true;
}
#15
看看,找不到解决方法
#1
sf
#2
流中没有数据
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL)
{
return FALSE;
}
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
另外用gdi+ 简单多了
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL)
{
return FALSE;
}
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
另外用gdi+ 简单多了
#3
GDI+
#4
GDI+怎么做,可以说得详细一些吗?谢谢
#5
pStmBmp->Write((BYTE *)GlobalLock(hData), nSize, &nByteWriten);
我加了这样一句,还是不可以,image->Load(pStmBmp); 的时候返回值是E_FAIL.
究竟是怎么回事呀!大家帮帮忙吧!急死了!!!
我加了这样一句,还是不可以,image->Load(pStmBmp); 的时候返回值是E_FAIL.
究竟是怎么回事呀!大家帮帮忙吧!急死了!!!
#6
VOID Example_SaveFile(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object based on a PNG file.
Image image(L"Mosaic.png");
// Draw the image.
graphics.DrawImage(&image, 10, 10);
// Construct a Graphics object based on the image.
Graphics imageGraphics(&image);
// Alter the image.
SolidBrush brush(Color(255, 0, 0, 255));
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
// Draw the altered image.
graphics.DrawImage(&image, 200, 10);
// Save the altered image.
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
image.Save(L"Mosaic2.png", &pngClsid, NULL);
}
#7
GDI+应该可以
#8
倦怠,这样是可以实现的,但是现在要求的是从IStream流中读取一幅图,并不是从一个文件中读取一幅图
Image image(L"Mosaic.png");
这句如果换成这样能成功的话就OK了
Image* imImage = NULL;
imImage->FromStream(pStmBmp, FALSE);
从pStmBmp里读取一幅图。
Image image(L"Mosaic.png");
这句如果换成这样能成功的话就OK了
Image* imImage = NULL;
imImage->FromStream(pStmBmp, FALSE);
从pStmBmp里读取一幅图。
#9
直接用COleStreamFile就好了
#10
问题解决了,原来很简单,谢谢大家了,结帖!
#11
解决了也不说下如何解决的……
#12
bs
#13
此istream非彼IStream
后者是接口类
后者是接口类
#14
不好意思,怎么没有贴上解决的办法。是这么解决的:最后HBITMAP没有转化成IStream。而是直接转化为Bmp格式。代码如下:
bool CMPRCtrl::SaveBitmapToFile(HBITMAP hbitmap , LPSTR lpfilename)
{
CAtlString strImgType;
strImgType = m_pImgType;
HDC hdc;
int nBits;
// how many character of one pixel own while in the current resolution ratio
WORD wbitcount;
// how many character of on pixel own in the bitmap
DWORD dwpalettesize = 0;
// the size of color palette
DWORD dwbmbitssize;
// the size of bitmap
DWORD dwdibsize;
// how many character write to file
DWORD dwwritten;
// the property of bitmap file
BITMAP bitmap;
// information about the type, size, and layout of a file that contains a DIB
BITMAPFILEHEADER bmfhdr;
// information about the dimensions and color format of a DIB
BITMAPINFOHEADER bi;
// the pointer point to bitmap information
LPBITMAPINFOHEADER lpbi;
HANDLE fh = NULL;
HANDLE hdib = NULL;
HANDLE hpal = NULL;
HANDLE holdpal=NULL;
hdc = CreateDC("display", NULL, NULL, NULL);
// how many character of on pixel own in the bitmap
nBits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (nBits <= 1)
{
wbitcount = 1;
}
else if (nBits <= 4)
{
wbitcount = 4;
}
else if (nBits <= 8)
{
wbitcount = 8;
}
else if (nBits <= 16)
{
wbitcount = 16;
}
else if (nBits <= 24)
{
wbitcount = 24;
}
else
{
wbitcount = 32;
}
// calculate the size of color palette
if (wbitcount <= 8)
{
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
}
// set the information of bitmap
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount + 31) / 32) * 4 * bitmap.bmHeight;
// allocate core storage
hdib = GlobalAlloc(GHND, dwbmbitssize + dwpalettesize
+ sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// process the palette
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// get the new pixel of palette
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
// set bitmap file
bmfhdr.bfType = 0x4d42;
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
if ("jpg" == strImgType)
{
HRESULT stat;
CImage *image = new CImage();
image->Attach(hbitmap);
stat = image->Save(lpfilename, ImageFormatJPEG);
if(S_OK != stat)
{
return false;
}
image->Detach();
delete image;
}
// create bitmap file
if ("bmp" == strImgType)
{
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
{
GlobalUnlock(hdib);
GlobalFree(hdib);
return false;
}
// write image to bitmap file
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// write the information to bitmap file
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
CloseHandle(fh);
}
GlobalUnlock(hdib);
GlobalFree(hdib);
return true;
}
bool CMPRCtrl::SaveBitmapToFile(HBITMAP hbitmap , LPSTR lpfilename)
{
CAtlString strImgType;
strImgType = m_pImgType;
HDC hdc;
int nBits;
// how many character of one pixel own while in the current resolution ratio
WORD wbitcount;
// how many character of on pixel own in the bitmap
DWORD dwpalettesize = 0;
// the size of color palette
DWORD dwbmbitssize;
// the size of bitmap
DWORD dwdibsize;
// how many character write to file
DWORD dwwritten;
// the property of bitmap file
BITMAP bitmap;
// information about the type, size, and layout of a file that contains a DIB
BITMAPFILEHEADER bmfhdr;
// information about the dimensions and color format of a DIB
BITMAPINFOHEADER bi;
// the pointer point to bitmap information
LPBITMAPINFOHEADER lpbi;
HANDLE fh = NULL;
HANDLE hdib = NULL;
HANDLE hpal = NULL;
HANDLE holdpal=NULL;
hdc = CreateDC("display", NULL, NULL, NULL);
// how many character of on pixel own in the bitmap
nBits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (nBits <= 1)
{
wbitcount = 1;
}
else if (nBits <= 4)
{
wbitcount = 4;
}
else if (nBits <= 8)
{
wbitcount = 8;
}
else if (nBits <= 16)
{
wbitcount = 16;
}
else if (nBits <= 24)
{
wbitcount = 24;
}
else
{
wbitcount = 32;
}
// calculate the size of color palette
if (wbitcount <= 8)
{
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
}
// set the information of bitmap
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount + 31) / 32) * 4 * bitmap.bmHeight;
// allocate core storage
hdib = GlobalAlloc(GHND, dwbmbitssize + dwpalettesize
+ sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// process the palette
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// get the new pixel of palette
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
// set bitmap file
bmfhdr.bfType = 0x4d42;
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
if ("jpg" == strImgType)
{
HRESULT stat;
CImage *image = new CImage();
image->Attach(hbitmap);
stat = image->Save(lpfilename, ImageFormatJPEG);
if(S_OK != stat)
{
return false;
}
image->Detach();
delete image;
}
// create bitmap file
if ("bmp" == strImgType)
{
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
{
GlobalUnlock(hdib);
GlobalFree(hdib);
return false;
}
// write image to bitmap file
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// write the information to bitmap file
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
CloseHandle(fh);
}
GlobalUnlock(hdib);
GlobalFree(hdib);
return true;
}
#15
看看,找不到解决方法