天天看點

VC2005中将Picture控件顯示圖檔儲存為BMP,JPG等格式

1.在stdafx.h頭檔案中加入

  #include <atlimage.h>

2.儲存圖檔

  方法一:  

<a></a>

  HBITMAP hBitmap = NULL;

//建立位圖段

BITMAPINFO bmi;

LPBYTE pBits;

ZeroMemory(&amp;bmi,sizeof(bmi));

//m_bmpShow為Picture Control控件變量名稱

CDC *pShowDC = m_bmpShow.GetDC();

//擷取Picture Control控件的寬度和高度

//CRect m_rcShow

m_bmpShow.GetWindowRect(&amp;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-&gt;m_hDC,&amp;bmi,DIB_RGB_COLORS,(void **)&amp;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!!"));

}

      方法二:

CDC *pdc = m_bmpShow.GetDC();

CImage imag;

CRect rcClient;

m_bmpShow.GetWindowRect(&amp;rcClient);

imag.Create(rcClient.Width(),rcClient.Height(),32);

::BitBlt(imag.GetDC(),0,0,rcClient.Width(),rcClient.Height(),pdc-&gt;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

繼續閱讀