VC2005中将Picture控件显示图片保存为BMP,JPG等格式

1.在stdafx.h头文件中加入

  #include <atlimage.h>

2.保存图片

  方法一:  

  HBITMAP hBitmap = NULL;
//创建位图段
BITMAPINFO bmi;
LPBYTE pBits;
ZeroMemory(
&bmi,sizeof(bmi));
//m_bmpShow为Picture Control控件变量名称
CDC *pShowDC = m_bmpShow.GetDC();

//获取Picture Control控件的宽度和高度
//CRect m_rcShow
m_bmpShow.GetWindowRect(&m_rcShow);

bmi.bmiHeader.biSize
= sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth
= m_rcShow.Width();
bmi.bmiHeader.biHeight
= m_rcShow.Height();
bmi.bmiHeader.biPlanes
= 1;
bmi.bmiHeader.biBitCount
= 24;
bmi.bmiHeader.biCompression
= BI_RGB;
hBitmap
= CreateDIBSection(pShowDC->m_hDC,&bmi,DIB_RGB_COLORS,(void **)&pBits,0,0);
//创建兼容dc并选择位图段
CDC dcMem;
dcMem.CreateCompatibleDC(pShowDC);
dcMem.SelectObject(hBitmap);
dcMem.BitBlt(
0,0,m_rcShow.Width(),m_rcShow.Height(),pShowDC,0,0,SRCCOPY);
m_bmpShow.ReleaseDC(pShowDC);
if( hBitmap )
{
CImage img;
img.Attach(hBitmap);
img.Save(_T(
"f:\\1.bmp"));
   img.Save(_T(
"f:\\1.jpg"));
  //其它文件格式同理
DeleteObject(hBitmap);
AfxMessageBox(_T(
"OK!!"));
}

 

      方法二:

 

//m_bmpShow为Picture Control控件变量名称
CDC *pdc = m_bmpShow.GetDC();
CImage imag;
//获取Picture Control控件的宽度和高度
CRect rcClient;
m_bmpShow.GetWindowRect(
&rcClient);
imag.Create(rcClient.Width(),rcClient.Height(),
32);

::BitBlt(imag.GetDC(),
0,0,rcClient.Width(),rcClient.Height(),pdc->m_hDC,0,0,SRCCOPY);

imag.Save(_T(
"f:\\2.bmp"));
imag.Save(_T(
"f:\\21.jpg"));
ReleaseDC(pdc);
imag.ReleaseDC();
AfxMessageBox(_T(
"OK!"));

 

参考:http://blog.sina.com.cn/s/blog_56e19aa70100c59k.html

   http://blog.sina.com.cn/s/blog_56e19aa70100c5a0.html

 

 

posted @ 2011-01-04 15:49  Chjun  阅读(1525)  评论(0编辑  收藏  举报