MFC Picture Control 自适应控件大小显示图片,失真和不失真

来源:互联网 发布:网络数据传输原理详解 编辑:程序博客网 时间:2024/06/05 06:03

StretchBlt和BitBlt都用在双缓冲视图中,用来显示一幅图像

一、StretchBlt

函数从源矩形中复制一个位图到目标矩形,必要时按目标设备设置的模式进行图像的拉伸或压缩。也即是将内存中的位图拷贝到屏幕上,并且可以根据屏幕画图区的大小来进行伸缩,适应响应的屏幕(或图像控件)


BOOL StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop );
x:逻辑单元x轴左上角坐标; (获取图像控件的DC后,该值一般设为 0 ) 
y:逻辑单元y轴左上角坐标; (获取图像控件的DC后,该值一般设为 0 ) 
nWidth: 设备矩形宽度;(即是图像控件的宽度,通过int rcWidth = rc.right - rc.left;来获得,rc为保存了图像控件矩形区坐标信息)
nHeight:设备矩形高度;  (即是图像控件的高度,通过int rcHeight = rc.bottom - rc.top;来获得)
pSrcDC: 源设备上下文;   (就是我们定义的内存DC,然后取地址)
xSrc :源矩形x轴左上角坐标;   (一般都是整幅图片拷贝,所以该值为 0 )
ySrc: 源矩形y轴左上角坐标;  (一般都是整幅图片拷贝,所以该值为 0 )
Specifies the x-coordinate (in logical units) of the upper-left corner of the source rectangle.
nSrcWidth:源矩形宽度;(如果我们定义了一个结构体BITMAP类型m_bmp保存位图信息,那么可以用m_bmp.bmWidth获取宽度)
nSrcHeight:源矩形高度 ( 如果我们定义了一个结构体BITMAP类型m_bmp保存位图信息,那么可以用m_bmp.bmHeight获取宽度)
dwRop:指定要进行的光栅操作。(这个有很多选择,我们一般选择SRCCOPY:将源矩形区域直接拷贝到目标矩形区域。)

二、BitBlt函数
该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境。

CDC::BitBlt 

BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );

BitBlt函数和上面的STretchBlt函数的参数基本上差不多,只是少了倒数第二个和倒数第三个参数:

nSrcWidth:源矩形宽度; 以及,nSrcHeight:源矩形高度 

前面四个参数也是和目标矩形(也即是我们的图像控件区域)相关的,如果是图像控件的话,起点坐标也是(0,0),终点坐标就是矩形区的宽度和高度;

对于源矩形区域(指内存DC中的位图),只需要给出拷贝的起点即可,

BitBlt函数不会对原来的位图进行拉伸或压缩,只会根据我们给定的nWidth,nHeight 值来决定该有多少部分进行显示


CRect rect;

CWnd *pWin = GetDlgItem(IDC_pic11);
pWin->GetClientRect(rect);
//GetClientRect(rect);
DWORD nWidth = rect.Width();
DWORD nHeight = rect.Height();
CDC *dc = pWin->GetDC();
//CDC *dc = GetDC();
//dc->Rectangle(rect);
CDC memDc; //定义一个内存显示设备对象
CBitmap memBitmap;//定义一个位图对象
memDc.CreateCompatibleDC(dc);//建立与屏幕显示兼容的内存显示设备
memBitmap.CreateCompatibleBitmap(dc, pBitmap.bmWidth, pBitmap.bmHeight);//建立一个与屏幕显示兼容的位图,位图的大小可选用窗口客户区的大小
CBitmap *pOldBit = memDc.SelectObject(&memBitmap);////将位图选入到内存显示设备中,只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
memDc.FillSolidRect(0, 0, nWidth, nHeight, RGB(255, 255, 255));
int i, j;
int pitch = bmp.bmpWidth % 4;
//绘图操作等在这里实现
for (i = 0; i < bmp.bmpHeight; i++)
{
int realpitch = i*pitch;
for (j = 0; j < bmp.bmpWidth; j++)
{
memDc.SetPixel(j, i, RGB(
bmp.pBmpBuf[((bmp.bmpHeight - i - 1)*bmp.bmpWidth + j) * 3 + 2 + realpitch],
bmp.pBmpBuf[((bmp.bmpHeight - i - 1)*bmp.bmpWidth + j) * 3 + 1 + realpitch],
bmp.pBmpBuf[((bmp.bmpHeight - i - 1)*bmp.bmpWidth + j) * 3 + realpitch]));
}
}
dc->SetStretchBltMode(STRETCH_HALFTONE);//设置缩放方式
if (pBitmap.bmWidth < nWidth && pBitmap.bmHeight < nHeight)
{
dc->BitBlt(0, 0, nWidth, nHeight, &memDc, 0, 0, SRCCOPY);//将内存中的图拷贝到屏幕上进行显示
}
else
{
float xScale = (float)nWidth / (float)pBitmap.bmWidth;
float yScale = (float)nHeight / (float)pBitmap.bmHeight;
float scaleIndex = (xScale <= yScale ? xScale : yScale);
//将内存中的图复制到屏幕上显示
//dc->BitBlt(0, 0, nWidth, nHeight, &memDc, 0, 0, SRCCOPY);//将内存中的图拷贝到屏幕上进行显示
//dc->StretchBlt(0, 0, (int)nWidth * scaleIndex, (int)nHeight * scaleIndex, &memDc,0, 0, (int)pBitmap.bmWidth, (int)pBitmap.bmHeight, SRCCOPY);//不会失真
dc->StretchBlt(0, 0, (int)nWidth, (int)nHeight, &memDc, 0, 0, (int)pBitmap.bmWidth, (int)pBitmap.bmHeight, SRCCOPY);//会失真,但铺满整个控件大小
}
//绘图后的清理
memDc.SelectObject(pOldBit);

memBitmap.DeleteObject();

位图的大小不要和客户端一样了,不然会显示不全,在进行缩放之前一定要设置对应DC的缩放模式

阅读全文
1 0
原创粉丝点击