?? 位圖圖片的縮放 (2000年12月4日).txt
字號(hào):
位圖圖片的縮放 (2000年12月4日)
本站更新 分類: 作者:翻譯:srw 2000-12-01 推薦: 閱讀次數(shù):600
(http://www.codesky.net)
--------------------------------------------------------------------------------
翻譯:srw 2000-12-01
一些程序可以縮放圖片,也就是說,這些程序可以顯示縮放的圖片。例如,一個(gè)應(yīng)用程序可以提供可以提供一個(gè)可以
縮放的圖片以便查看和編輯沒一個(gè)像素。其實(shí)縮放圖片是調(diào)用了StretchBlt這個(gè)函數(shù)。象BitBlt函數(shù),StretchBlt函
數(shù)都可以把位圖從一個(gè)DC拷貝到另外一個(gè)DC上。但是,與BitBlt這個(gè)函數(shù)不同的是,StretchBlt函數(shù)可以通過指定
圖象的尺寸來縮放圖片,如果源位圖的尺寸大于目標(biāo)位圖的尺寸,則目標(biāo)圖片就是縮小了的,反之目標(biāo)圖片就是放大
了的。
如果目標(biāo)位圖尺寸小于源位圖的尺寸,StretchBlt函數(shù)按照下面表格列出的縮放模式 去除顏色數(shù)據(jù)。
縮放模式:
BLACKONWHITE
對(duì)消除的像素和保留的像素執(zhí)行邏輯AND 操作
WHITEONBLACK
對(duì)消除的像素和保留的像素執(zhí)行邏輯OR 操作
COLORONCOLOR
直接去處顏色數(shù)據(jù)
HALFTONE
在目標(biāo)位圖中盡量保持源位圖的色彩數(shù)據(jù)
可以通過調(diào)用SetStretchBltMode來縮放位圖。
下面的例子取自一個(gè)應(yīng)用程序,這個(gè)程序演示了如何顯示一個(gè)原始的和放大一倍的位圖(在這個(gè)程序中使用的是缺
省的縮放模式)。
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.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -