我有一个dev-c ++应用程序做一个截图并将其写入文件。现在我想把图像写入一个变量/流。最初我使用了三个 Writefile 函数,它将头,信息和hbitmap写入文件。现在我想保存数据不是到文件,而是一个流,所以我可以使用它进一步处理。我使用的代码是:
I have an dev-c++ Application which makes an Screenshot and writes it into an File. Now I want to write the image into an Variable/Stream. Originally I used three Writefile Functions, which write the header, the info and the hbitmap into an file. Now I want to save the data not to the file, but to an Stream, so I can use it for further processing. The Code I use is this:
/* <Include> */ #include <windows.h> #include <iostream> #include <sstream> /* </Include> */ /* <Const> */ const char *AppName="Yeah"; using namespace std; /* </Const> */ /* <Function> */ void SaveScreen(HWND pScreen, stringstream Path) { int Width = GetSystemMetrics(SM_CXSCREEN);//1280; int Height = GetSystemMetrics(SM_CYSCREEN);//1024; HDC hdcScreen; HBITMAP hbmScreen; //---------------Bitmap Informationen BITMAPINFO infobmp; infobmp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); infobmp.bmiHeader.biWidth = Width; infobmp.bmiHeader.biHeight = Height; infobmp.bmiHeader.biPlanes = 1; infobmp.bmiHeader.biBitCount = 24; infobmp.bmiHeader.biCompression = 0; infobmp.bmiHeader.biSizeImage = 0; infobmp.bmiHeader.biXPelsPerMeter = 0; infobmp.bmiHeader.biYPelsPerMeter = 0; infobmp.bmiHeader.biClrUsed = 0; infobmp.bmiHeader.biClrImportant = 0; int* bitmap = new int[Width*Height*3]; BITMAPFILEHEADER bfheader; bfheader.bfType = 19778; bfheader.bfSize = sizeof(BITMAPFILEHEADER) + Width*Height*3 + sizeof(BITMAPINFOHEADER); bfheader.bfReserved1 = 0; bfheader.bfReserved2 = 0; bfheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //Bitmap ----------------------- Informationen hdcScreen = GetWindowDC(pScreen); hbmScreen = CreateCompatibleBitmap(hdcScreen, Width, Height); // tempor?rer DC HDC hdcTemp = CreateCompatibleDC(hdcScreen); // Bitmap reinselektieren HBITMAP hbmOld = (HBITMAP)SelectObject(hdcTemp, hbmScreen); // Inhalt von Desktop ?bertragen BitBlt(hdcTemp, 0, 0, Width, Height, hdcScreen, 0, 0, SRCCOPY); int iResult = GetDIBits(hdcTemp, hbmScreen, 0, Height, bitmap, &infobmp, DIB_RGB_COLORS); // aufr?umen SelectObject(hdcTemp, hbmOld); DeleteObject(hbmScreen); DeleteDC(hdcTemp); // HANDLE hfile = CreateFile(Path, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0); //Datei Schreiben DWORD word; WriteFile(Path, &bfheader, 14, &word, NULL); WriteFile(Path, &infobmp, 40,& word, NULL); WriteFile(Path, bitmap, Width*Height*3, &word,NULL); // Path = &bfheader & &infobmp & bitmap; ReleaseDC(pScreen, hdcScreen); // CloseHandle(hfile); delete[] bitmap; } /* </Function> */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd = FindWindow(NULL, AppName); stringstream ms; SaveScreen(hWnd, ms); return 0; }有人可以告诉我我做错了什么吗?
Can someone please show me what I am doing wrong?
推荐答案您可以创建如下结构:
struct ScreenShotBuffer { BITMAPFILEHEADER bfheader; BITMAPINFO infobmp; int* bitmap; };然后使用该结构创建一个全局变量。
Then create a global variable using that structure.
struct ScreenShotBuffer myScreenShot;无论如何你正在分配内存使用malloc保存数据。 所以你可以使用这三行代替 WriteBuffer():
Anyway you are allocating the "memory" to hold the data using malloc. So you can just use these three lines instead of WriteBuffer() :
myScreenShot.bfheader=bfheader; myScreenShot.infobmp=infobmp; myScreenShot.bitmap=bitmap;更多推荐
将图像保存到内存
发布评论