如何将内存中的位图数据绘制在DC上
2014-02-10 13:45
369 查看
假如你定义了一个位图类,里面包含位图头,位图信息头,调色板,位图数据。然后你按照位图的格式将位图文件读入你的类中,现在你知道了位图的全部信息了。主要信息包含在位图信息头里面,数据则在位图数据缓冲里面。现在的问题是,在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); |
相关文章推荐
- 如何将内存中的位图数据绘制在DC上
- 如何将内存中的位图数据绘制在DC上
- 如何将内存中的位图数据绘制在DC上
- 如何根据内存数据生成位图
- 指令和数据都用二进制代码存放在内存中,从时空观角度回答CPU如何区分读出的代码是指令还是数据
- 内存有限的情况下 Spark 如何处理 T 级别的数据
- 数据在内存中如何存储?静态存储区 栈区 堆区 小端和大端存储方式
- 如何解决PHP里大量数据循环时内存耗尽的问题
- 整型数据在内存中如何存储?
- 【Qt开发】如何将内存图像数据封装成QImage V1
- 关于weka里面如何获得绘制ROC曲线的数据
- 【IOS 开发学习总结-OC-66】Quartz 2D绘图(4-3)——绘制曲线+在内存中绘图+绘制位图
- 数据在内存中是如何存储的
- 如何用1g内存服务器去爬取一个大型网站的嵌套数据(一步步解决out of memery)
- VS2005 中如何在DataGridView控件的数据列标题上绘制编号
- 有关内存DC和双缓冲位图的问题汇总
- [绘制相关] 用CImage 读取 内存中的图片数据
- 绘制内存位图
- 内存中数据输出到stdout, ffmpeg如何直接读取stdio
- Xcode如何查看内存中的数据