如何在对话框上显示一张图片
2011-11-17 18:14
281 查看
如何在对话框上显示一张图片
在对话框上显示一张图片,可能是一个比较常用的功能,在MFC中提供了OnPaint()绘图函数,下面结合这个函数,提供 几种在对话框上显示一张图片的代码。首先假设你有一张24位的图片,放在e:\下,然后建立一个dialog based的MFC程序。利用picture control显示
拖动一个picture control到dialog上,ID为IDC_PICTURE,然后在OnPaint()函数下的else条件下加入如下代码:
HBITMAP hBitmap; //get the picture control which is on the dialog CStatic *pStatic = (CStatic*)GetDlgItem(IDC_PICTURE); //load the file hBitmap = (HBITMAP)LoadImage(NULL, "E:\\2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); pStatic->ModifyStyle(0xF, SS_BITMAP); //show the file on the picture control pStatic->SetBitmap(hBitmap);
利用CImage类显示
加上头文件:#include <atlimage.h>
在OnPaint()函数下的else条件下加入如下代码:
CImage img; img.Load("E:\\2.BMP"); img.Draw(this->GetDC()->GetSafeHdc(), 0, 0, img.GetWidth(), img.GetHeight(), 0, 0, img.GetWidth(), img.GetHeight());
利用BitBlt()函数显示
在OnPaint()函数下的else条件下加入如下代码:
CDC memdc; CClientDC dc(this); void* flag = LoadImage(NULL, "E:\\2.BMP", IMAGE_BITMAP, 0,0,LR_LOADFROMFILE); //CBitmap defindes a series of operation on image files CBitmap *bitmap = CBitmap::FromHandle((HBITMAP)flag); //BITMAP is bmp file BITMAP temp; bitmap->GetBitmap(&temp); //temp.bmBits = (unsigned char*)malloc(temp.bmHeight*temp.bmWidth*3); //bitmap->GetBitmapBits(temp.bmHeight*temp.bmWidth, temp.bmBits); //get the height and the width int heigth =temp.bmHeight; int width =temp.bmWidth; memdc.CreateCompatibleDC(&dc); memdc.SelectObject(bitmap); //dc.FillSolidRect((GetSystemMetrics(SM_CXSCREEN)-m_Length)/2, (GetSystemMetrics(SM_CYSCREEN)-m_Width)/2, m_Length,m_Width,RGB(0,0,255)); //dc.BitBlt( 0,0, width, heigth, &memdc, 0, 0, SRCCOPY); //dc.StretchBlt(50, 50, 300, 300, &memdc, 0, 0, heigth, width, SRCCOPY); //BitBlt() is faster than StretchBlt() dc.BitBlt( 0,0, width, heigth, &memdc, 0, 0, SRCCOPY); DeleteObject(bitmap);
利用SetDIBitsToDevice函数显示
在OnPaint()函数下的else条件下加入如下代码:
CClientDC dc(this);<br/> void* flag = LoadImage(NULL, "E:\\2.BMP", IMAGE_BITMAP, 0,0,LR_LOADFROMFILE); //CBitmap defindes a series of operation on image files CBitmap *bitmap = CBitmap::FromHandle((HBITMAP)flag); //BITMAP is bmp file BITMAP temp; bitmap->GetBitmap(&temp); int height = temp.bmHeight; int widht = temp.bmWidth; temp.bmBits = (unsigned char*)malloc(temp.bmHeight*temp.bmWidth*4); bitmap->GetBitmapBits(height*widht*4, temp.bmBits); BITMAPINFO * info = new BITMAPINFO; info->bmiHeader.biBitCount = temp.bmBitsPixel; info->bmiHeader.biClrImportant = 0; info->bmiHeader.biHeight = temp.bmHeight; info->bmiHeader.biWidth = temp.bmWidth; info->bmiHeader.biPlanes = temp.bmPlanes; info->bmiHeader.biClrUsed = 0; info->bmiHeader.biCompression = 0; info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); info->bmiHeader.biSizeImage = height*widht*3; info->bmiHeader.biXPelsPerMeter = 0; info->bmiHeader.biYPelsPerMeter = 0; SetDIBitsToDevice(dc, 0, 0, widht, height, 0,0, 0, height, temp.bmBits, info, DIB_RGB_COLORS);
经过测试,在显示速度上,第四种方法是最快的,其次是第三种,前两种速度好像差不多。 若建立的是MFC single document, 找到view类下的onDraw()函数,图片显示是类似的。
http://iiec.cqu.edu.cn/wiki/index.php/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%AF%B9%E8%AF%9D%E6%A1%86%E4%B8%8A%E6%98%BE%E7%A4%BA%E4%B8%80%E5%BC%A0%E5%9B%BE%E7%89%87
相关文章推荐
- 一个TIF文件包含多个文件,如何显示每一张图片
- HTML如何使用一张图片做背景只显示一次
- android TV端如何读取微信网页版二维码显示生成一张图片
- JS中如何让图片慢慢消失,然后自动再显示另一张图片?
- Android如何从图库获取一张图片并显示
- 教你如何HTML显示一张图片的一部分
- Android如何先显示一张图片,延时几秒后,再自动跳转到另一个页
- 教你如何HTML显示一张图片的一部分
- 如何把一张图片上的几个小图片按自己的要求有规律的显示出来
- 如何在UIimageview里显示一张图片里的某一部分
- 如何实现文字加透明背景显示在一张图片的底部的效果
- 如何在UIimageview里显示一张图片里的某一部分
- [转帖]如何将一张图片写入数据库SQL,并能正确在C#网页中显示
- 一张图片多个链接,点击之后框框显示如何去掉
- iOS中如何显示一张图片
- 请问!急啊!抓取视频中的图片以后,如何显示在对话框上?
- 如何在UIimageview里显示一张图片里的某一部分
- 如何把多张图片合在一张图片里面显示?这里来个python + opencv的版本
- 如何在用户区显示一张图片
- WIN32 API 编程之如何显示一张图片?(