如何将内存中的位图数据绘制在DC上
2015-06-24 09:04
375 查看
假如你定义了一个位图类,里面包含位图头,位图信息头,调色板,位图数据。然后你按照位图的格式将位图文件读入你的类中,现在你知道了位图的全部信息了。主要信息包含在位图信息头里面,数据则在位图数据缓冲里面。现在的问题是,在Windows下面如何将一张位图画出来,而且现在是如何从数据缓存里面绘画出位图。
一般情况,我们都是直接绘制在dc里面,而不是绑定到子控件,让子控件自己绘画,比如picture控件之类的,我觉得提供绘制在dc里面的接口更具有广泛性。
现在我知道两种从内存数据绘制彩色位图的2种方法。第一种麻烦一点,第二种则相当直接。
方法一:
第一步,用CreateCompatibleDC创建跟目标dc的兼容性内存dc。
第二步,用CreateCompatibleBitmap创建跟目标dc的兼容性位图。
第三步,用SelectObject将第二步创建的兼容位图选入第一步创建的兼容dc中。
第四步,用SetDIBits设置兼容位图的数据缓冲。
第五步,用BitBlt将数据从兼容内存dc绘制到目标dc。
第六步,删除兼容位图和兼容dc。
代码如下,其中buffer代表位图数据缓冲。
方法二:直接调用StretchDIBits绘制位图。
该函数功能相当强悍,似乎专为从内存数据绘制位图到dc而生。
函数原型如下:
int StretchDIBits(
HDC hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
int nDestWidth, // width of destination rectangle
int nDestHeight, // height of destination rectangle
int XSrc, // x-coord of source upper-left corner
int YSrc, // y-coord of source upper-left corner
int nSrcWidth, // width of source rectangle
int nSrcHeight, // height of source rectangle
CONST VOID *lpBits, // bitmap bits
CONST BITMAPINFO *lpBitsInfo, // bitmap data
UINT iUsage, // usage options
DWORD dwRop // raster operation code
);
使用也相当简单,调用
即可了。
一般情况,我们都是直接绘制在dc里面,而不是绑定到子控件,让子控件自己绘画,比如picture控件之类的,我觉得提供绘制在dc里面的接口更具有广泛性。
现在我知道两种从内存数据绘制彩色位图的2种方法。第一种麻烦一点,第二种则相当直接。
方法一:
第一步,用CreateCompatibleDC创建跟目标dc的兼容性内存dc。
第二步,用CreateCompatibleBitmap创建跟目标dc的兼容性位图。
第三步,用SelectObject将第二步创建的兼容位图选入第一步创建的兼容dc中。
第四步,用SetDIBits设置兼容位图的数据缓冲。
第五步,用BitBlt将数据从兼容内存dc绘制到目标dc。
第六步,删除兼容位图和兼容dc。
代码如下,其中buffer代表位图数据缓冲。
1 | HDC hCompatibleDC = CreateCompatibleDC(hDc); |
2 | HBITMAP hCompatibleBitmap = CreateCompatibleBitmap(hDc, bitmapinfoheader.biWidth, |
3 | bitmapinfoheader.biHeight); |
4 | HBITMAP hOldBitmap = ( HBITMAP )SelectObject(hCompatibleDC, hCompatibleBitmap); |
5 | SetDIBits(hDc, hCompatibleBitmap, 0, bitmapinfoheader.biHeight, |
6 | buffer, (BITMAPINFO*)&bitmapinfoheader, DIB_RGB_COLORS); |
7 | BitBlt(hDc, nStartX, nStartY, bitmapinfoheader.biWidth, bitmapinfoheader.biHeight, |
8 | hCompatibleDC, 0, 0, SRCCOPY); |
9 | SelectObject(hCompatibleDC, hOldBitmap); |
10 | DeleteObject(hCompatibleDC); |
11 | DeleteObject(hCompatibleDC); |
该函数功能相当强悍,似乎专为从内存数据绘制位图到dc而生。
函数原型如下:
int StretchDIBits(
HDC hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
int nDestWidth, // width of destination rectangle
int nDestHeight, // height of destination rectangle
int XSrc, // x-coord of source upper-left corner
int YSrc, // y-coord of source upper-left corner
int nSrcWidth, // width of source rectangle
int nSrcHeight, // height of source rectangle
CONST VOID *lpBits, // bitmap bits
CONST BITMAPINFO *lpBitsInfo, // bitmap data
UINT iUsage, // usage options
DWORD dwRop // raster operation code
);
使用也相当简单,调用
1 | StretchDIBits(hDc, nStartX, nStartY, bitmapinfoheader.biWidth, |
2 | bitmapinfoheader.biHeight, 0, 0, bitmapinfoheader.biWidth, |
3 | bitmapinfoheader.biHeight, buffer, (BITMAPINFO*)&bitmapinfoheader, |
4 | DIB_RGB_COLORS, SRCCOPY); |
相关文章推荐
- Mysql 基于 Amoeba 的 读写分离
- idea的常用快捷键整理(备用)
- Ios应用开发(四)框架及带属性的字符串
- Eclipse配置C/C++开发环境
- 黑马程序员--iOS-OC基本语法
- mac 终端 常用命令
- 刮痧,拔罐加艾灸治好了膝盖痛
- iOS UI 视图
- HDU 1160(FatMouse's Speed)最长不降子序列nlogn+路径记录
- jquery实现鼠标拖拽排序功能
- 关于浏览器网页渲染基本结构图
- Cookie 操作类
- MySql 手动执行主从备份
- Android 实现标题重用
- 草虫
- NUMA架构的CPU
- C#中计算时间差
- Android学习 14-> 浅谈下活动Activity
- Android 开发环境配置图文教程(jdk+eclipse+android sdk)
- MsBuild 入门