Image processing C++;从位图获取图像缓冲区
我想拍一张PNG格式的屏幕截图。Image processing C++;从位图获取图像缓冲区,image-processing,visual-c++,screenshot,gdi+,screen-resolution,Image Processing,Visual C++,Screenshot,Gdi+,Screen Resolution,我想拍一张PNG格式的屏幕截图。 但我有一个严重的问题,我认为这对教授来说似乎很容易。 1。如何从位图(类)中获取图像缓冲区。 2.我使用多显示器,也有不同的分辨率。如何获得全屏分辨率。 实施: #pragma comment (lib, "gdiplus.lib") #include "stdafx.h" #include <afxwin.h> #include <stdio.h> #include <tchar.h> #include <gdiplu
但我有一个严重的问题,我认为这对教授来说似乎很容易。
1。如何从位图(类)中获取图像缓冲区。
2.我使用多显示器,也有不同的分辨率。如何获得全屏分辨率。 实施:
#pragma comment (lib, "gdiplus.lib")
#include "stdafx.h"
#include <afxwin.h>
#include <stdio.h>
#include <tchar.h>
#include <gdiplus.h>
using namespace Gdiplus;
DWORD PNGScreenshot();
INT GetEncoderClsid(const WCHAR * format, CLSID* pClsid);
INT GetEncoderClsid(const WCHAR * format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1;
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;
}
}
free(pImageCodecInfo);
return -1;
}
DWORD PNGScreenshot()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
INT x = GetDeviceCaps(hScreenDC, HORZRES);
INT y = GetDeviceCaps(hScreenDC, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
StretchBlt(hMemoryDC, 0, 0, x, y, hScreenDC, 0, 0, x, y, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
Bitmap *bmp = new Bitmap(hBitmap, NULL);
// Get Image Buffer from Bitmap
// GetBufferFromBitmap(LPBYTE lpImageBuffer, Bitmap * bmp);
// {
// 1. Get Size of Bitmap
// 2. Get Image buffer from Bitmap
// 3. Copy Image buffer to lpImageBuffer
// }
// ... Do ...
delete bmp;
GdiplusShutdown(gdiplusToken);
return GetLastError();
}
int _tmain(int argc, _TCHAR* argv[])
{
PNGScreenshot();
return 0;
}
#pragma注释(lib,“gdiplus.lib”)
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间Gdiplus;
德沃德·普格斯林肖特();
INT GetEncoderClsid(常量WCHAR*格式,CLSID*pClsid);
INT GetEncoderClsid(常量WCHAR*格式,CLSID*pClsid)
{
UINT num=0;
UINT大小=0;
ImageCodecInfo*pImageCodecInfo=NULL;
GetImageEncodersSize(&num,&size);
如果(大小==0)
返回-1;
pImageCodecInfo=(ImageCodecInfo*)(malloc(size));
如果(pImageCodecInfo==NULL)
返回-1;
GetImageEncoders(num、size、pImageCodecInfo);
对于(UINT j=0;j
这只是我的实现,那么有没有其他方法可以将PNG屏幕截图作为缓冲区?另一种实现可能是使用该接口。
使用该方法将位图存储到带有
clsid_png
编解码器的IStream
对象,然后返回到图像@Barmak使用memcpy
而不是FromStream
另一种实现可能是使用该接口。使用该方法将位图存储到带有
clsid_png
编解码器的IStream
对象,然后返回到图像@Barmak使用memcpy
而不是FromStream
我使用INT x=GetSystemMetrics(SM_XVIRTUALSCREEN)代码>INT y=GetSystemMetrics(SM\u虚拟屏幕)代码>但出现了相同的问题。我使用了intx=GetSystemMetrics(SM_XVIRTUALSCREEN)代码>INT y=GetSystemMetrics(SM\u虚拟屏幕)代码>但发生了相同的问题。