例如我用
dc.MoveTo(0, 0);
dc.LineTo(10, 10);
我画的这条线如何保存成BMP或者JPG格式?
dc.MoveTo(0, 0);
dc.LineTo(10, 10);
我画的这条线如何保存成BMP或者JPG格式?
解决方案 »
- 不太理解这两句C++ 语言 请大家帮忙看一下
- 如何申请一个保存指针的数组呢?
- VS2008里面怎么找不到MSCHART控件了?
- 【请教】单文档多视图中数据调用的问题
- 如何取得游戏聊天窗口的文本.
- 请教一个问题,HDC在多线程下会出问题吗?
- 高分求救VC中用ADO连接Oracle!
- WaitForSingleObject某个线程,导致线程立马死了。。。
- 如何让程序在桌面图标与桌面背景之间运行,有代码,但有问题
- 在纯的Windows API 中是否可以在菜单的WM_COMMAND中用CreateWindows创建按钮?
- TCP局域网传送数据不丢失,广域网传送数据丢失问题
- visual studio c++中mfc应用程序入门(望高手指点迷津)
void WriteBMPFile(HBITMAP hBitMap, LPTSTR filename, HDC hDC)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp; // create the bitmapinfo header information if (!GetObject( hBitMap, sizeof(BITMAP), (LPTSTR)&bmp))
{
return;
} // Convert the color format to a count of bits.
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
// Allocate memory for the BITMAPINFO structure.
if (cClrBits != 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));
else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER)); // Initialize the fields in the BITMAPINFO structure. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1<<cClrBits); // If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB; // Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 * pbmi->bmiHeader.biHeight * cClrBits;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0; // now open file and save the data
pbih = (PBITMAPINFOHEADER) pbmi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage); if (!lpBits)
{
return;
} // Retrieve the color table (RGBQUAD array) and the bits
if (!GetDIBits(hDC, HBITMAP(hBitMap), 0, (WORD) pbih->biHeight, lpBits, pbmi, DIB_RGB_COLORS))
{
return;
} // Create the .BMP file.
hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
{
return;
}
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0; // Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD); // Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))
{
return;
} // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL)))
{
return;
} // Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
{
return;
} // Close the .BMP file.
if (!CloseHandle(hf))
{
return;
} // Free memory.
GlobalFree((HGLOBAL)lpBits);
}实例:
void CTESTView::OnDraw(CDC* pDC)
{
CTESTDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
CDC ImageDC;
ImageDC.CreateCompatibleDC(pDC);
CBitmap bmp,*oldbmp;
bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
ImageDC.SetBkColor(RGB(255,255,255));
oldbmp = ImageDC.SelectObject(&bmp);
ImageDC.FillSolidRect(rect, pDC->GetBkColor());
ImageDC.MoveTo(10, 10);
ImageDC.LineTo(300, 300);
WriteBMPFile((HBITMAP)bmp.GetSafeHandle(),"C:\\1.bmp",ImageDC.m_hDC);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&ImageDC,0,0,SRCCOPY);
ImageDC.SelectObject(oldbmp);
ImageDC.DeleteDC();
}
楼主建议多备点资料,比如到 http://www.vckbase.com 上面多看看,这个问题的答案就能得到了!
int nHeight = 100;
CImage newimg;
if(!newimg.CreateEx(nWidth, nHeight, 24, BI_RGB))
return; HDC hDC = newimg.GetDC();
int nPreMode = ::SetStretchBltMode(hDC, HALFTONE);
::MoveTo(hDC, 0, 0);
::LineTo(hDC, 10, 10); //在这里可以调用绘图命令在图片上绘图
::SetBrushOrgEx(hDC, 0, 0, NULL);
::SetStretchBltMode(hDC, nPreMode);
newimg.ReleaseDC(); newimg.Save("d:\\abc.jpg");//想保存成什么格式只要文件名的扩展名变为相应的名称即可
newimg.Destroy();
CDC memDC; // 内存CDC
CBitmap bitmap; // 内存位图
BITMAP infoBitmap; // 位图信息结构体
// 创建内存CDC
memDC.CreateCompatibleDC(pDC);
// 创建设备相关位图
bitmap.CreateCompatibleBitmap(pDC, width, height);
// 获得位图信息,估计不需要
bitmap.GetBitmap(&infoBitmap);
// 是否支持32位颜色
if (infoBitmap.bmBitsPixel != 32)
{
MessageBox(L"当前显示设备不支持32位颜色,程序自动退出!");
PostMessage(WM_QUIT);
} //初始化图片,估计你不需要
bitmap.SetBitmapBits(width*height * 4, m_picData); memDC.SelectObject(bitmap); // 在这里添加绘图操作。如memDC.MoveTo(0, 0);
//在这里调用1楼那个函数 // 拷贝内存CDC中的内容,估计你不需要
pDC->BitBlt(0, 0, 26, 26*17, &memDC, 0, 0, SRCCOPY);
ReleaseDC(pDC);
pDC->BitBlt(0, 0, width, height , &memDC, 0, 0, SRCCOPY);