位图图片的缩放 (2000年12月4日)
本站更新 分类: 作者:翻译:srw 2000-12-01 推荐: 阅读次数:600
(http://www.codesky.net)
--------------------------------------------------------------------------------
翻译:srw 2000-12-01
一些程序可以缩放图片,也就是说,这些程序可以显示缩放的图片。例如,一个应用程序可以提供可以提供一个可以
缩放的图片以便查看和编辑没一个像素。其实缩放图片是调用了StretchBlt这个函数。象BitBlt函数,StretchBlt函
数都可以把位图从一个DC拷贝到另外一个DC上。但是,与BitBlt这个函数不同的是,StretchBlt函数可以通过指定
图象的尺寸来缩放图片,如果源位图的尺寸大于目标位图的尺寸,则目标图片就是缩小了的,反之目标图片就是放大
了的。
如果目标位图尺寸小于源位图的尺寸,StretchBlt函数按照下面表格列出的缩放模式 去除颜色数据。
缩放模式:
BLACKONWHITE
对消除的像素和保留的像素执行逻辑AND 操作
WHITEONBLACK
对消除的像素和保留的像素执行逻辑OR 操作
COLORONCOLOR
直接去处颜色数据
HALFTONE
在目标位图中尽量保持源位图的色彩数据
可以通过调用SetStretchBltMode来缩放位图。
下面的例子取自一个应用程序,这个程序演示了如何显示一个原始的和放大一倍的位图(在这个程序中使用的是缺
省的缩放模式)。
hdcScaled = CreateCompatibleDC(hdcScreen);
hbmScaled = CreateCompatibleBitmap(hdcScreen,
GetDeviceCaps(hdcScreen, HORZRES) * 2,
GetDeviceCaps(hdcScreen, VERTRES) * 2);
if (hbmScaled == 0)
errhandler("hbmScaled", hwnd);
// Select the bitmaps into the compatible DC.
if (!SelectObject(hdcScaled, hbmScaled))
errhandler("Scaled Bitmap Selection", hwnd);
case WM_COMMAND: // message: command from application menu
switch(wParam)
{
case IDM_SCALEX1:
if (fBlt)
{
fScaled = FALSE;
hdcWin = GetDC(hwnd);
BitBlt(hdcWin,
0,0,
bmp.bmWidth, bmp.bmHeight,
hdcCompatible,
0,0,
SRCCOPY);
ReleaseDC(hwnd, hdcWin);
}
break;
case IDM_SCALEX2:
if (fBlt)
{
fScaled = TRUE;
StretchBlt(hdcScaled,
0, 0,
bmp.bmWidth * 2, bmp.bmHeight * 2,
hdcCompatible,
0, 0,
bmp.bmWidth, bmp.bmHeight,
SRCCOPY);
hdcWin = GetDC(hwnd);
BitBlt(hdcWin,
0,0,
bmp.bmWidth, bmp.bmHeight,
hdcScaled,
0,0,
SRCCOPY);
ReleaseDC(hwnd, hdcWin);
}
break;
附原文:
Scaling an Image
Some applications scale images ? that is, they display zoomed or reduced views of an image. For
example, a drawing application may provide a zoom feature that enables the user to view and edit a
drawing on a pixel-by-pixel basis.
Applications scale images by calling the StretchBlt function. Like the BitBlt function, StretchBlt
copies bitmap data from a bitmap in a source DC into a bitmap in a target DC. However, unlike the
BitBlt function, StretchBlt scales the image based on the specified dimensions of the source and
target rectangles. If the source rectangle is larger than the target rectangle, the resultant image
will appear to have shrunk; if the source rectangle is smaller than the target rectangle, the
resultant image will appear to have expanded.
If the target rectangle is smaller than the source rectangle, StretchBlt removes color data from the
image according to a specified stretch mode as shown in the following table.
Stretch Mode Method
BLACKONWHITE Performs a logical AND operation on the color data for the eliminated pixels and the
color data for the remaining pixels.
WHITEONBLACK Performs a logical OR operation on the color data for the eliminated pixels and the
color data for the remaining pixels.
COLORONCOLOR Eliminates the color data of the deleted pixels completely.
HALFTONE Approximates the original (source) color data in the destination.
You set the stretch mode by calling the SetStretchBltMode function.
The following example code is taken from an application that displays an image either at its
original size or a twice the original size. (This application uses the default stretch mode.)
hdcScaled = CreateCompatibleDC(hdcScreen);
hbmScaled = CreateCompatibleBitmap(hdcScreen,
GetDeviceCaps(hdcScreen, HORZRES) * 2,
GetDeviceCaps(hdcScreen, VERTRES) * 2);
if (hbmScaled == 0)
errhandler("hbmScaled", hwnd);
// Select the bitmaps into the compatible DC.
if (!SelectObject(hdcScaled, hbmScaled))
errhandler("Scaled Bitmap Selection", hwnd);
case WM_COMMAND: // message: command from application menu
switch(wParam)
{
case IDM_SCALEX1:
if (fBlt)
{
fScaled = FALSE;
hdcWin = GetDC(hwnd);
BitBlt(hdcWin,
0,0,
bmp.bmWidth, bmp.bmHeight,
hdcCompatible,
0,0,
SRCCOPY);
ReleaseDC(hwnd, hdcWin);
}
break;
case IDM_SCALEX2:
if (fBlt)
{
fScaled = TRUE;
StretchBlt(hdcScaled,
0, 0,
bmp.bmWidth * 2, bmp.bmHeight * 2,
hdcCompatible,
0, 0,
bmp.bmWidth, bmp.bmHeight,
SRCCOPY);
hdcWin = GetDC(hwnd);
BitBlt(hdcWin,
0,0,
bmp.bmWidth, bmp.bmHeight,
hdcScaled,
0,0,
SRCCOPY);
ReleaseDC(hwnd, hdcWin);
}
break;
? 1997 Microsoft Corporation. All rights reserved. Terms of Use.