截屏保存为BMP, 然后转换为jpg, 用GDI和GDI+
2009-05-09 16:04
525 查看
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CRUNMonitorDlg::OnBnClickedBmptojpg()
{
CString strFilePath = "5.bmp";
CString strSavePath = "5.jpg";
CString strEXT = "JPEG";
Image* pImage = Image::FromFile(strFilePath.AllocSysString());
if(Ok == pImage->GetLastStatus())
{
UINT nDecCount = 0; // number of image decoders
UINT nDecSize = 0; // size of the image decoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageDecodersSize(&nDecCount, &nDecSize);
pImageCodecInfo = (ImageCodecInfo*) malloc(nDecSize);
GetImageDecoders(nDecCount, nDecSize, pImageCodecInfo);
UINT nIndex = 0;
for(; nIndex < nDecCount; nIndex++)
{
if(strEXT == pImageCodecInfo[nIndex].FormatDescription)
{
break;
}
}
Status status = pImage->Save(strSavePath.AllocSysString(), &(pImageCodecInfo[1].Clsid));
if(Ok != status)
{
TRACE2(_T("/nFailed to save image in '%s' file/n")
_T("GDI+ Error: %u"),
strFilePath,
status);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CRUNMonitorDlg::OnBnClickedCaptureScreen()
{
CBitmap bmScreen;
//屏幕DC
CDC *pDesktopDC;
//获取当前整个屏幕DC
pDesktopDC = CDC::FromHandle(::GetDC(NULL));
int Width = pDesktopDC-> GetDeviceCaps(HORZRES);
int Height = pDesktopDC-> GetDeviceCaps(VERTRES);
bmScreen.CreateCompatibleBitmap(pDesktopDC, Width, Height);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDesktopDC);
CBitmap *oldmemBitmap;//建立和屏幕兼容的bitmap
//bmScreen.CreateCompatibleBitmap(pDesktopDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&bmScreen);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDesktopDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
memDC.SelectObject(oldmemBitmap);
//以下代码保存memDC中的位图到文件
BITMAP bmp;
BITMAPINFOHEADER bihScreen;
bmScreen.GetBitmap(&bmp);//获得位图信息
FILE *fp = fopen("5.bmp", "w+b");
ZeroMemory(&bihScreen, sizeof(BITMAPINFOHEADER));//位图信息头
bihScreen.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bihScreen.biCompression = BI_RGB;
bihScreen.biHeight = bmp.bmHeight;//高度
bihScreen.biPlanes = 1;
bihScreen.biSize = sizeof(BITMAPINFOHEADER);
bihScreen.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bihScreen.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfhScreen;
ZeroMemory(&bfhScreen, sizeof(BITMAPFILEHEADER));//位图文件头
bfhScreen.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfhScreen.bfSize = bfhScreen.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfhScreen.bfType = (WORD)0x4d42;
fwrite(&bfhScreen, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bihScreen, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
BYTE * pbmScreenData = NULL;
pbmScreenData = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP)bmScreen.m_hObject, 0, Height, pbmScreenData,
(LPBITMAPINFO) &bihScreen, DIB_RGB_COLORS);//获取位图数据
fwrite(pbmScreenData, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete []pbmScreenData;
pbmScreenData = NULL;
fclose(fp);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CRUNMonitorDlg::OnBnClickedBmptojpg()
{
CString strFilePath = "5.bmp";
CString strSavePath = "5.jpg";
CString strEXT = "JPEG";
Image* pImage = Image::FromFile(strFilePath.AllocSysString());
if(Ok == pImage->GetLastStatus())
{
UINT nDecCount = 0; // number of image decoders
UINT nDecSize = 0; // size of the image decoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageDecodersSize(&nDecCount, &nDecSize);
pImageCodecInfo = (ImageCodecInfo*) malloc(nDecSize);
GetImageDecoders(nDecCount, nDecSize, pImageCodecInfo);
UINT nIndex = 0;
for(; nIndex < nDecCount; nIndex++)
{
if(strEXT == pImageCodecInfo[nIndex].FormatDescription)
{
break;
}
}
Status status = pImage->Save(strSavePath.AllocSysString(), &(pImageCodecInfo[1].Clsid));
if(Ok != status)
{
TRACE2(_T("/nFailed to save image in '%s' file/n")
_T("GDI+ Error: %u"),
strFilePath,
status);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CRUNMonitorDlg::OnBnClickedCaptureScreen()
{
CBitmap bmScreen;
//屏幕DC
CDC *pDesktopDC;
//获取当前整个屏幕DC
pDesktopDC = CDC::FromHandle(::GetDC(NULL));
int Width = pDesktopDC-> GetDeviceCaps(HORZRES);
int Height = pDesktopDC-> GetDeviceCaps(VERTRES);
bmScreen.CreateCompatibleBitmap(pDesktopDC, Width, Height);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDesktopDC);
CBitmap *oldmemBitmap;//建立和屏幕兼容的bitmap
//bmScreen.CreateCompatibleBitmap(pDesktopDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&bmScreen);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDesktopDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
memDC.SelectObject(oldmemBitmap);
//以下代码保存memDC中的位图到文件
BITMAP bmp;
BITMAPINFOHEADER bihScreen;
bmScreen.GetBitmap(&bmp);//获得位图信息
FILE *fp = fopen("5.bmp", "w+b");
ZeroMemory(&bihScreen, sizeof(BITMAPINFOHEADER));//位图信息头
bihScreen.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bihScreen.biCompression = BI_RGB;
bihScreen.biHeight = bmp.bmHeight;//高度
bihScreen.biPlanes = 1;
bihScreen.biSize = sizeof(BITMAPINFOHEADER);
bihScreen.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bihScreen.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfhScreen;
ZeroMemory(&bfhScreen, sizeof(BITMAPFILEHEADER));//位图文件头
bfhScreen.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfhScreen.bfSize = bfhScreen.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfhScreen.bfType = (WORD)0x4d42;
fwrite(&bfhScreen, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bihScreen, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
BYTE * pbmScreenData = NULL;
pbmScreenData = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP)bmScreen.m_hObject, 0, Height, pbmScreenData,
(LPBITMAPINFO) &bihScreen, DIB_RGB_COLORS);//获取位图数据
fwrite(pbmScreenData, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete []pbmScreenData;
pbmScreenData = NULL;
fclose(fp);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
相关文章推荐
- GDI+ 将BMP 32图像转换为JPG文件格式保存
- VB60用 GDI+保存为JPG/TIFF/PNG/GIF/BMP等格式
- VC实现:bmp转jpg、jpg转bmp、截屏保存jpg
- vc6.0使用gdi+在内存中绘图并将其保存为bmp,jpg,gif,png等格式的图片
- GDI+实例教程之: 转换Image(BMP,JPG,GIF,PNG等) 到MetaFile(EMF等) 格式
- VC实现:bmp转jpg、jpg转bmp、截屏保存jpg
- VC++ 截屏代码,并保存为想要的格式(BMP,JPG,PNG,GIF等格式)
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。
- [WIN32]VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- 保存一个免费的在线的图片转换工具网站,支持BMP,JPG,IOC,PNG和GIF
- vc6.0使用gdi+在内存中绘图并将其保存为bmp,jpg,gif,png等格式的图片
- bmp转jpg、jpg转bmp、截屏保存jpg
- VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。
- 图像转换(jpg to bmo)(bmp to jpg)
- C 实现BMP 转换为JPG 附源代码
- 消除GDI与GDI+之间DP到LP的转换值的误差
- android上用C语言读取fb0实现截屏,并保存为rgb565的bmp
- 在 VC++6.0 中将 JPG 格式图片转换成 BMP 格式