GDI+ 屏幕截图转JPG, 为何总从0,0左边开始?如果改开始坐标??
void CBbbDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HWND hwnd = ::GetDesktopWindow();
HDC hdc = ::GetWindowDC(NULL);
int x = GetDeviceCaps(hdc, HORZRES);
int y = GetDeviceCaps(hdc, VERTRES);
HBITMAP hbmp = ::CreateCompatibleBitmap(hdc, x, y), hold;
HDC hmemdc = ::CreateCompatibleDC(hdc);
hold = (HBITMAP)::SelectObject(hmemdc, hbmp);
BitBlt(hmemdc, 0, 0, x, y, hdc, 0, 0, SRCCOPY);
SelectObject(hmemdc, hold);
const unsigned short *pszFileName=L"C:\\new.jpg"; //保存路径
int xs= GetSystemMetrics(SM_CXSCREEN);
int ys= GetSystemMetrics(SM_CYSCREEN);
Bitmap bit2(hbmp, NULL), bit(400, 300); //截屏生成的jpg 文件,长度,宽度,,,为何总从0,0起,,改其他参数总是不性??
Graphics g(&bit);
g.ScaleTransform((float)xs/x, (float)ys/y);
g.DrawImage(&bit2, 0, 0);
CLSID encoderClsid;
ULONG quality;
quality = 50; //转换压缩率
EncoderParameters encoderParameters;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value = &quality;
GetEncoderClsid(L"image/jpeg",&encoderClsid);
bit.Save(pszFileName, &encoderClsid, &encoderParameters);
::DeleteObject(hbmp);
::DeleteObject(hmemdc);
AfxMessageBox("ok");
return;
}
int CBbbDlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num,&size);
if(size == 0) return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL) return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j =0; j<num; ++j)
{
if(wcscmp(pImageCodecInfo[j].MimeType, format) ==0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
{
// TODO: Add your control notification handler code here
HWND hwnd = ::GetDesktopWindow();
HDC hdc = ::GetWindowDC(NULL);
int x = GetDeviceCaps(hdc, HORZRES);
int y = GetDeviceCaps(hdc, VERTRES);
HBITMAP hbmp = ::CreateCompatibleBitmap(hdc, x, y), hold;
HDC hmemdc = ::CreateCompatibleDC(hdc);
hold = (HBITMAP)::SelectObject(hmemdc, hbmp);
BitBlt(hmemdc, 0, 0, x, y, hdc, 0, 0, SRCCOPY);
SelectObject(hmemdc, hold);
const unsigned short *pszFileName=L"C:\\new.jpg"; //保存路径
int xs= GetSystemMetrics(SM_CXSCREEN);
int ys= GetSystemMetrics(SM_CYSCREEN);
Bitmap bit2(hbmp, NULL), bit(400, 300); //截屏生成的jpg 文件,长度,宽度,,,为何总从0,0起,,改其他参数总是不性??
Graphics g(&bit);
g.ScaleTransform((float)xs/x, (float)ys/y);
g.DrawImage(&bit2, 0, 0);
CLSID encoderClsid;
ULONG quality;
quality = 50; //转换压缩率
EncoderParameters encoderParameters;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value = &quality;
GetEncoderClsid(L"image/jpeg",&encoderClsid);
bit.Save(pszFileName, &encoderClsid, &encoderParameters);
::DeleteObject(hbmp);
::DeleteObject(hmemdc);
AfxMessageBox("ok");
return;
}
int CBbbDlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num,&size);
if(size == 0) return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL) return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j =0; j<num; ++j)
{
if(wcscmp(pImageCodecInfo[j].MimeType, format) ==0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
...全文
相关推荐
vc GDI+实现屏幕截图,图片裁剪,
在本文中,我们将深入探讨如何使用VC++ 2015和GDI+库来实现屏幕截图以及图片裁剪的功能。
MFC截图开发实战:GDI坐标系、设备上下文与位图内存管理
屏幕截图是Windows桌面应用的基础能力,其底层依赖GDI设备上下文(HDC)、位图内存管理及坐标系映射等核心机制。理解CDC生命周期、GetDC与ReleaseDC配对规则、CreateDIBSection内存分配原理,是避免黑屏、崩溃和GDI泄漏的关键。技术价值体现在UI自动化测试、远程控制、教学录屏等工程场景中,尤其在MFC框架下需兼顾传统GDI资源手动管理与现代高DPI适配需求。本文聚焦VS环境下的C++截图实现,深入解析BitBlt与PrintWindow的适用边界、CImage多格式保存陷阱,
WPF调用WinForms实现屏幕截图完整方案
方法声明如下:bitmap:目标位图对象,必须已初始化且未被其他Graphics引用。:指定控件内容绘制到该位图上的矩形区域(通常设为示例用法:// 保存结果该代码成功将按钮的外观渲染至 PNG 文件中。然而,其背后并非简单的像素拷贝,而是模拟了一次完整的控件重绘过程。
屏幕截图功能,VB.NET代码实现
下面我们将详细探讨如何使用VB.NET代码实现屏幕截图功能。首先,我们需要了解VB.NET中的Graphics类。Graphics类是GDI+的核心部分,它可以用于绘制图形、文字和图像。
gdi+实现截屏保存png文件,鼠标钩子,直接截图
在本文中,我们将深入探讨如何使用GDI+技术来实现屏幕截图,并将其保存为PNG文件。


