Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - Bitmap speichernSprachenübersicht/C / C++/ C#/Grafik Herzlichen Dank an Anton Staruschkin von www.cpp-programming.de/, für die Erlaubnis seine Quellcodes zu veröffentlichen.
Code: int DC2Bitmap(HDC hdc, int width, int height, char *filename) { HDC hdc2; HBITMAP aBmp; BITMAPINFO bi; HGDIOBJ OldObj; void *dibvalues; HANDLE fileHandle; BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; DWORD bytes_write; DWORD bytes_written; hdc2=CreateCompatibleDC(hdc); ZeroMemory(&bmih,sizeof(BITMAPINFOHEADER)); bmih.biSize=sizeof(BITMAPINFOHEADER); bmih.biHeight=height; bmih.biWidth=width; bmih.biPlanes=1; bmih.biBitCount=24; bmih.biCompression=BI_RGB; bmih.biSizeImage = ((((bmih.biWidth * bmih.biBitCount) + 31) & ~31) >> 3) * bmih.biHeight; bmih.biXPelsPerMeter = 0; bmih.biYPelsPerMeter = 0; bmih.biClrImportant = 0; bi.bmiHeader=bmih; aBmp=CreateDIBSection(hdc,&bi,DIB_RGB_COLORS,(void**)&dibvalues,0,0); if(aBmp==NULL) { OutputDebugString("CreateDIBSection failed!\n"); return 0; } OldObj=SelectObject(hdc2,aBmp); BitBlt(hdc2,0,0,width,height,hdc,0,0,SRCCOPY); ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER)); bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bmfh.bfSize=(3*bmih.biHeight*bmih.biWidth)+sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER); bmfh.bfType=0x4d42; bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; fileHandle=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,(DWORD)0,NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL); if (fileHandle==INVALID_HANDLE_VALUE) { OutputDebugString("CreateFile failed!\n"); return 0; } bytes_write=sizeof(BITMAPFILEHEADER); if (!WriteFile(fileHandle,(void*)&bmfh,bytes_write,&bytes_written,NULL)) { OutputDebugString("WriteFile failed!\n"); return 0; } bytes_write=sizeof(BITMAPINFOHEADER); if(!WriteFile(fileHandle,(void*)&bmih,bytes_write,&bytes_written,NULL)) { OutputDebugString("WriteFile failed!\n"); return 0; } bytes_write=bmih.biSizeImage; if(!WriteFile(fileHandle,(void*)dibvalues, bytes_write,&bytes_written,NULL)) { OutputDebugString("WriteFile failed!\n"); return 0; } CloseHandle(fileHandle); DeleteObject(SelectObject(hdc2,OldObj)); DeleteDC(hdc2); return 1; } Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Grafik/Bitmap speichern |