2013-10-08 160 views
0

我有一個開發屏幕並將其寫入文件的dev-C++應用程序。現在我想將圖像寫入變量/流。最初我使用了三個Writefile函數,它們將頭文件,信息和hbitmap寫入文件。現在我想將數據保存到文件中,但是保存到Stream中,以便我可以使用它進行進一步處理。我使用的代碼是這樣的:將圖像保存到內存

/* <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; 
} 

有人可以告訴我我做錯了什麼嗎?

+0

什麼進一步處理?將這些信息寫入普通存儲器陣列是否可行? –

+0

@AlexFarber我想用它作爲HTTP請求中的Post-Data – user2855954

+0

給變量?然後,您可以使用包含足夠空間的結構來保存該數據。 – Sreekar

回答

0

您可以創建一個結構是這樣的:

struct ScreenShotBuffer 
{ 
BITMAPFILEHEADER bfheader; 
BITMAPINFO infobmp; 
int* bitmap; 
}; 

然後創建使用該結構的全局變量。

struct ScreenShotBuffer myScreenShot; 

無論如何,你正在使用malloc分配「內存」來保存數據。 因此,您可以使用這三行代替WriteBuffer()

myScreenShot.bfheader=bfheader; 
myScreenShot.infobmp=infobmp; 
myScreenShot.bitmap=bitmap;