PixelFormat4bppIndexed PixelFormat8bppIndexed 每像素4位,索引色 每像素8位,索引色 PixelFormat16bppARGB1555 每像素16位,α分量1位、RGB分量各5位 PixelFormat16bppGrayScale PixelFormat16bppRGB555 PixelFormat16bppRGB565 PixelFormat24bppRGB PixelFormat32bppARGB PixelFormat32bppPARGB PixelFormat32bppRGB PixelFormat48bppRGB PixelFormat64bppARGB PixelFormat64bppPARGB 每像素16位,灰度 每像素16位,RGB分量各5位,另1位未用 每像素16位,RB分量各5位、G分量6位 每像素24位,RGB分量各8位 每像素32位,αRGB分量各8位 每像素32位,αRGB分量各8位,RGB分量预乘α分量 每像素24位,RGB分量各8位,另8位未用 每像素48位,RGB分量各16位 每像素64位,αRGB分量各16位 每像素64位,αRGB分量各16位,RGB分量预乘α分量 2.常用方法
Bitmap类的专有方法有:
// 克隆
Bitmap *Clone(INT x, INT y, INT width, INT height, PixelFormat format); Bitmap *Clone(const Rect &rect, PixelFormat format);
Bitmap *Clone(REAL x, REAL y, REAL width, REAL height, PixelFormat format); Bitmap *Clone(const RectF &rect, PixelFormat format); // 创建
static Bitmap *FromBITMAPINFO(const BITMAPINFO *gdiBitmapInfo,
VOID *gdiBitmapData);
static Bitmap *FromDirectDrawSurface7(IDirectDrawSurface7* surface); static Bitmap *FromFile(const WCHAR *filename,
BOOL useEmbeddedColorManagement = FALSE);
static Bitmap *FromHBITMAP(HBITMAP hbm, HPALETTE hpal); static Bitmap *FromHICON(HICON hicon);
46
static Bitmap *FromResource(HINSTANCE hInstance, const WCHAR *bitmapName); static Bitmap *FromStream(IStream *stream, BOOL useEmbeddedColorManagement); // 获取
Status GetHBITMAP(const Color &colorBackground, HBITMAP *hbmReturn); Status GetHICON(HICON *hicon); // 像素
Status GetPixel(INT x, INT y, Color *color); Status SetPixel(INT x, INT y, const Color &color); // 设置
Status SetResolution(REAL xdpi, REAL ydpi);
Status LockBits(const Rect *rect, UINT flags, PixelFormat format,
BitmapData *lockedBitmapData);
Status UnlockBits(BitmapData *lockedBitmapData);
13.4.4 基本操作
GDI+中图像的基本操作有绘制、缩放、裁剪、旋转、翻转、仿射、文件动画等,前两个操作GDI中也有。
1.绘制图像
图形类Graphics有18个同名的重载方法用于绘制图像,其中常用的有如下4个(它们都有对应的浮点数版):
Status DrawImage(Image *image, INT x, INT y); // 不缩放(坐标对) Status DrawImage(Image *image, const Point &point); // 不缩放(点) // 可缩放(坐标对与宽高)
Status DrawImage(Image *image, INT x, INT y, INT width, INT height); Status DrawImage(Image *image, const Rect &rect); // 可缩放(矩形) 例如(绘制):
Graphics graph(pDC->m_hDC); Image img(L\张东健.bmp\
47
graph.DrawImage(&img, 0, 0);
2.缩放
上面图形类Graphics的后两个图像绘制方法(及其实数版),可以缩放图像(与GDI的StretchBlt函数的功能类似)。例如(按客户区大小缩放,参见图13-33):
Graphics graph(pDC->m_hDC); Image img(L\金泰熙.bmp\CRect rect;
GetClientRect(&rect);
graph.DrawImage(&img, 0, 0, rect.Width(), rect.Height());
图13-33 按客户区大小缩放
图13-34 按指定倍数缩放
又例如(按指定[实数]倍数zoomMultiple缩放,参见图13-34):
REAL zoomMultiple = 1.5;
Graphics graph(pDC->m_hDC); Image img(L\金泰熙.bmp”);
graph.DrawImage(&img, 0.0f, 0.0f, zoomMultiple *
img.GetWidth(), zoomMultiple * img.GetHeight());
还可以使用Graphics类的设置插值模式方法:
Status SetInterpolationMode(InterpolationMode interpolationMode);
来控制缩放的质量。其中的输入参数为枚举类型InterpolationMode:
typedef enum {
48
InterpolationModeInvalid = QualityModeInvalid, // 无效插值 InterpolationModeDefault = QualityModeDefault, // 默认插值 InterpolationModeLowQuality = QualityModeLow, // 低质插值 InterpolationModeHighQuality = QualityModeHigh, // 高质插值 InterpolationModeBilinear = QualityModeHigh + 1, // 双线性插值 InterpolationModeBicubic = QualityModeHigh + 2, // 双三次插值 InterpolationModeNearestNeighbor = QualityModeHigh + 3, // 最邻近插值 InterpolationModeHighQualityBilinear = QualityModeHigh + 4, // 高质双线性插值 InterpolationModeHighQualityBicubic = QualityModeHigh + 5 // 高质双三次插值 } InterpolationMode; 例如(参见图13-35):
Graphics graph(pDC->m_hDC); Image img(L\张东健.bmp”);
//graph.SetInterpolationMode(InterpolationModeNearestNeighbor); //graph.SetInterpolationMode(InterpolationModeBilinear);
graph.SetInterpolationMode(InterpolationModeHighQualityBilinear); graph.DrawImage(&img, 0.0f, 0.0f, 3.23f *img.GetWidth(),
3.23f * img.GetHeight());
最邻近插值
双线性插值
高质双线性插值
图13-35 插值模式
3.剪裁与局部缩放
可以使用图形类Graphics的图像绘制方法:
49
Status DrawImage(Image *image, INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight, Unit srcUnit); // 不缩放(整数版) Status DrawImage(Image *image, REAL x, REAL y, REAL srcx, REAL srcy,
REAL srcwidth, REAL srcheight, Unit srcUnit); // 不缩放(实数版)
Status DrawImage(Image* image, const RectF& destRect, REAL srcx, REAL srcy,
REAL srcwidth, REAL srcheight, Unit srcUnit, const ImageAttributes* imageAttributes = NULL, DrawImageAbort callback = NULL, VOID* callbackData = NULL); // 可缩放(实数版)
Status DrawImage(Image *image, RectF &destRect, RectF &sourceRect, // 该方法库中无
Unit srcUnit, ImageAttributes *imageAttributes = NULL); // 可缩放(实数版)
来绘制图像的指定区域,即将图像剪裁后再输出。
例如(其中,cx、cy、cw、ch为剪裁区域的左上角坐标和宽高,zoomMultiple 为缩放倍数,参见图13-36):
图13-36 剪裁及其缩放
左上为原始图像,左中为剪裁区域,左下为剪裁后输出的图像 右上与下中为剪裁图随客户区缩放,右下为倍数放大(10倍) Graphics graph(pDC->m_hDC); Image img(L\张东健.bmp”);
graph.DrawImage(&img, 0, 0, cx, cy, cw, ch, UnitPixel); // 不缩放 CRect rect;
50