标签:
void CSaveView::OnFileSave() { BITMAP info;//原始图片 m_bitmap.GetBitmap(&info); CDC DC1; DC1.CreateCompatibleDC(NULL); DC1.SelectObject(&m_bitmap); DC1.MoveTo(0,0); DC1.LineTo(info.bmWidth,info.bmHeight); CDC DC2; CBitmap bmp;//缩放后的图片 bmp.CreateCompatibleBitmap(&DC1,200,200); DC2.CreateCompatibleDC(NULL); DC2.SelectObject(&bmp); int mode = SetStretchBltMode(DC2, COLORONCOLOR); //设置不失真缩放 DC2.StretchBlt(0,0,200,200,&DC1,0,0,info.bmWidth,info.bmHeight,SRCCOPY); SetStretchBltMode(DC2, mode); CFileDialog dlg(false,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL,this); if(IDOK == dlg.DoModal()) { CString bmpfile = dlg.GetPathName(); CFile file(bmpfile,CFile::modeCreate|CFile::modeWrite); BITMAP bInfo; bmp.GetBitmap(&bInfo); //计算调色板大小 int panelsize = 0; if (bInfo.bmBitsPixel<24) //非真彩色 { panelsize = int(pow((double)2,bInfo.bmBitsPixel)*sizeof(RGBQUAD)); } //定义位图信息 BITMAPINFO* bMapInfo = (BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+panelsize); bMapInfo->bmiHeader.biBitCount = bInfo.bmBitsPixel; bMapInfo->bmiHeader.biClrImportant = 0; bMapInfo->bmiHeader.biCompression = 0; bMapInfo->bmiHeader.biHeight = bInfo.bmHeight; bMapInfo->bmiHeader.biPlanes = bInfo.bmPlanes; bMapInfo->bmiHeader.biSize = sizeof(BITMAPINFO); bMapInfo->bmiHeader.biSizeImage = bInfo.bmHeight*bInfo.bmWidthBytes; bMapInfo->bmiHeader.biWidth = bInfo.bmWidth; bMapInfo->bmiHeader.biXPelsPerMeter = 0; bMapInfo->bmiHeader.biYPelsPerMeter = 0; //获取位图的实际数据 char* pData = new char[bMapInfo->bmiHeader.biSizeImage]; int len = GetDIBits(DC2.m_hDC,bmp,0,bInfo.bmHeight,pData,bMapInfo,DIB_RGB_COLORS); BITMAPFILEHEADER bFileHeader; bFileHeader.bfType = 0x4D42; bFileHeader.bfReserved1 = 0; bFileHeader.bfReserved2 = 0; bFileHeader.bfSize = sizeof(BITMAPFILEHEADER); bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+panelsize; //向文件中写入位图数据 file.Write(&bFileHeader,sizeof(BITMAPFILEHEADER)); file.Write(&bMapInfo->bmiHeader,sizeof(BITMAPINFOHEADER)); file.Write(pData,bMapInfo->bmiHeader.biSizeImage+panelsize); file.Close(); delete pData; LocalFree(bMapInfo); } }
标签:
原文地址:http://www.cnblogs.com/profession/p/4241330.html