C++ 将图像保存到内存

C++ 将图像保存到内存,c++,variables,stream,dev-c++,C++,Variables,Stream,Dev C++,我有一个dev-c++应用程序,它可以生成一个屏幕截图并将其写入一个文件。现在我想将图像写入一个变量/流。最初我使用了三个Writefile函数,它们将头、信息和hbitmap写入一个文件。现在我不想将数据保存到文件中,而是保存到流中,这样我就可以使用它进行进一步的处理。我使用的代码如下: /* <Include> */ #include <windows.h> #include <iostream> #include <sstream> /* &

我有一个dev-c++应用程序,它可以生成一个屏幕截图并将其写入一个文件。现在我想将图像写入一个变量/流。最初我使用了三个
Writefile
函数,它们将头、信息和hbitmap写入一个文件。现在我不想将数据保存到文件中,而是保存到流中,这样我就可以使用它进行进一步的处理。我使用的代码如下:

/* <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;
}
/**/
#包括
#包括
#包括
/*  */
/*  */
const char*AppName=“是”;
使用名称空间std;
/*  */
/*  */
无效保存屏幕(HWND pScreen、stringstream路径)
{
int Width=GetSystemMetrics(SM_CXSCREEN);/1280;
int Height=GetSystemMetrics(SM_CYSCREEN);//1024;
HDC-hdcScreen;
HBITMAP-hbmScreen;
//---------------位图信息
BITMAPINFO-infobmp;
infobmp.bmiHeader.biSize=sizeof(BitMapInfo头文件);
infobmp.bmiHeader.biWidth=宽度;
infobmp.bmiHeader.biHeight=高度;
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.biclrued=0;
infobmp.bmiHeader.biClrImportant=0;
int*位图=新int[宽度*高度*3];
位图文件头;
bfheader.bfType=19778;
bfheader.bfSize=sizeof(BITMAPFILEHEADER)+宽*高*3+sizeof(BitMapInfo头);
bfheader.bfReserved1=0;
bfheader.bfReserved2=0;
bfheader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BitMapInfo头);
//位图--------------------信息
hdcScreen=GetWindowDC(pScreen);
hbmScreen=CreateCompatibleBitmap(hdcScreen,Width,Height);
//临时区议会
HDC hdcTemp=CreateCompatibleDC(hdcScreen);
//位图重新插入器
HBITMAP hbmOld=(HBITMAP)SelectObject(hdcTemp,hbmScreen);
//Inhalt von Desktop?bertragen
BitBlt(hdcTemp,0,0,宽度,高度,hdcScreen,0,0,SRCCOPY);
int-iResult=GetDIBits(hdcTemp、hbmScreen、0、高度、位图和infobmp、DIB_RGB_颜色);
//奥夫鲁门
选择对象(hdcTemp、hbmOld);
删除对象(hbmScreen);
DeleteDC(hdcTemp);
//HANDLE hfile=CreateFile(路径,一般写入,0,0,始终打开,0,0);
//达蒂施雷本酒店
德沃德词;
WriteFile(路径和bfheader,14和word,NULL);
WriteFile(路径和信息bmp,40和word,NULL);
WriteFile(路径、位图、宽度*高度*3、&word、空);
//路径=&bfheader&&infobmp&bitmap;
释放DC(pScreen、hdcScreen);
//闭合手柄(hfile);
删除[]位图;
}
/*  */
int WINAPI WinMain(
HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{   
HWND HWND=FindWindow(NULL,AppName);
stringstream ms;
保存屏幕(hWnd,ms);
返回0;
}

有人能告诉我我做错了什么吗?

你可以创建这样的结构:

struct ScreenShotBuffer
{
BITMAPFILEHEADER bfheader;
BITMAPINFO infobmp;
int* bitmap;
};
然后使用该结构创建一个全局变量

struct ScreenShotBuffer myScreenShot;
无论如何,您正在使用malloc分配“内存”来保存数据。 因此,您可以只使用这三行,而不是
WriteBuffer()


具体的进一步处理是什么?是否可以将此信息写入普通内存数组?@AlexFarber我想将其用作HTTP请求中的Post数据,并将其写入变量?然后您可以使用一个包含足够空间的结构来保存该数据。@Sreekar如何使用结构?我对C++编程很陌生,因为我是一个14岁的家伙,但我想为我的学校写一个程序,程序几乎完成了,我只是错过了这个功能。你能给我一个工作的例子吗?
myScreenShot.bfheader=bfheader;
myScreenShot.infobmp=infobmp;
myScreenShot.bitmap=bitmap;