用graphics画一个矩形 怎么能保存到一张图片上!

HRESULT SaveImage( BYTE * pBuffer, BITMAPINFOHEADER *bmpInfo) { Gdiplus::BitmapData bitmapData; bitmapData.Width = bmpInfo->biWidth; bitmapData.Height = bmpInfo->biHeight; bitmapData.Stride = 3 * bmpInfo->biWidth; bitmapData.PixelFormat = PixelFormat24bppRGB; bitmapData.Scan0 = (VOID*)pBuffer; bitmapData.Reserved = NULL; Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; if ( GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Ok ) return E_FAIL; Gdiplus::Bitmap *pBitmap = new Gdiplus::Bitmap(bmpInfo->biWidth, bmpInfo->biHeight, PixelFormat24bppRGB); if ( pBitmap->LockBits(&Gdiplus::Rect(0, 0, bmpInfo->biWidth, bmpInfo->biHeight), Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeUserInputBuf, PixelFormat24bppRGB, &bitmapData) == Gdiplus::Ok ) { pBitmap->UnlockBits(&bitmapData); // actually a wrapper for GdipSaveImageToFile, creates the file but with size 0 kb // st returns FileNotFound, also tried with differents paths and compressors Gdiplus::Status st = pBitmap->Save(L"C://data.bmp", &Gdiplus::ImageFormatBMP, NULL); } delete pBitmap; Gdiplus::GdiplusShutdown(gdiplusToken); return S_OK; }  

void CGood111View::OnFileSave() { // TODO: Add your command handler code here CClientDC dc(this); CBitmap bmpForSave; CDC cdcForSave; CRect r; GetClientRect(&r); bmpForSave.CreateCompatibleBitmap(&dc,m_wath,m_high);//这个地方你要改一下,变成0,0 cdcForSave.CreateCompatibleDC(&dc); cdcForSave.SelectObject(bmpForSave); cdcForSave.BitBlt(m_pict.x, m_pict.y, r.Width(), r.Height(), &dc, 0, 0, SRCCOPY);//这个地方也改成00 SaveBitmap((HBITMAP)bmpForSave); } void CGood111View::SaveBitmap(HBITMAP hBitMap) { CImage2005 image; image.Attach(hBitMap); image.Save("out.bmp"); image.Detach(); } 

类似于这个代码,大至的思想如下
1.做一个内存dc和大小正好为那块区域大的BITMAP.把BITMAP选到内存dc上。  
2.把你要存的图BitBlt到内存dc。  
3.用CImage类,把BITMAP的内容保存

你可能感兴趣的:(用graphics画一个矩形 怎么能保存到一张图片上!)