//Download by http://www.NewXing.com // WalkLtDemo设计实例.cpp : implementation of the CWalkLtDemoView class // #include "stdafx.h" #include "WalkLtDemo.h" #include "WalkLtDemoDoc.h" #include "WalkLtDemoView.h" #include "WalkLtDemoHelp.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //屏蔽 4244 编译警告 #pragma warning( disable : 4244 ) extern Rect MyClient; void menuItem2_Click() { Graphics &g=GetGraphics(); g.Clear(Color::Black); Bitmap bitmap(L"demo.bmp"); int iWidth = bitmap.GetWidth(); int iHeight = bitmap.GetHeight(); //初始化色彩变换矩阵 ColorMatrix colorMatrix= { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; ImageAttributes imageAtt; //从0到1进行修改色彩变换矩阵主对角线上的数值 //使三种基准色的饱和度渐增 for(float i=0.0f;i<=1.0f;i+=0.02f) { colorMatrix.m[0][0]=i; colorMatrix.m[1][1]=i; colorMatrix.m[2][2]=i; colorMatrix.m[3][3]=i; //设置色彩校正矩阵 imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //绘制图片 g.DrawImage( &bitmap, Rect(0, 0, iWidth, iHeight), 0,0, iWidth,iHeight, UnitPixel, &imageAtt); } MyDrawString(g, "下面演示淡出效果...", Font(L"Arial",12), SolidBrush(Color::White), Point(20,iHeight+20)); //从1到0进行修改色彩变换矩阵主对角线上的数值 //依次减少每种色彩分量 for(i=1.0f;i>=0.0f;i-=0.02f) { colorMatrix.m[0][0]=i; colorMatrix.m[1][1]=i; colorMatrix.m[2][2]=i; colorMatrix.m[3][3]=i; //设置色彩校正矩阵 imageAtt.SetColorMatrix(&colorMatrix, ColorMatrixFlagsDefault,ColorAdjustTypeBitmap); //绘制图片 g.DrawImage( &bitmap, Rect(0, 0, iWidth, iHeight), 0,0, iWidth,iHeight, UnitPixel, &imageAtt); } MyDrawString(g, "完毕", Font(L"Arial",12), SolidBrush(Color::White), Point(20,iHeight+40)); } // void GrayScale_Click() { Graphics &g=GetGraphics(); g.Clear(Color::White); Bitmap image(L"head.bmp"); int Width =image.GetWidth()-1; int Height =image.GetHeight()-1; //绘制原图 g.DrawImage(&image,0, 0); g.TranslateTransform((REAL)image.GetWidth(),0.f); /*image2、image3分别用来保存最大值法 和加权平均法处理的灰度图像*/ Bitmap *image2=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare); Bitmap *image3=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare); Color color; //使用平均值进行灰度处理 for(int i=Width; i>=0;i--) for( int j=Height; j>=0;j--) { image.GetPixel(i,j, &color); //求出平均三个色彩分量的平均值 int middle=(color.GetR()+color.GetG()+color.GetB())/3; Color colorResult(255,middle,middle,middle); image.SetPixel(i,j, colorResult); } //重新绘制灰度化图 g.DrawImage(&image, Rect(0, 0, Width, Height)); //在新位置显示最大值法进行灰度处理的结果 g.TranslateTransform((REAL)image.GetWidth(),0.f); //使用最大值法进行灰度处理 for(i=Width; i>=0;i--) { for(int j=Height; j>=0;j--) { image2->GetPixel(i,j, &color); int tmp=color.GetR()>color.GetG()? color.GetR():color.GetG(); int maxcolor=tmp>color.GetB()? tmp:color.GetB(); Color colorResult(255,maxcolor,maxcolor,maxcolor); //设置处理后的灰度信息 image2->SetPixel(i, j, colorResult); } } //重新绘制灰度化图 g.DrawImage(image2, Rect(0, 0, Width, Height)); //在第二行绘制图片 g.ResetTransform(); g.TranslateTransform(0.f, (REAL)image.GetHeight()); //使用加权平均法进行灰度处理 for(i=Width; i>=0;i--) { for(int j=Height; j>=0;j--) { image3->GetPixel(i, j, &color); int R=(int)(0.3f*color.GetR()); int G=(int)(0.59f*color.GetG()); int B=(int)(0.11f*color.GetB()); Color colorResult(255,R,G,B); //设置处理后的灰度信息 image3->SetPixel(i, j, colorResult); } } //重新绘制灰度化图 g.DrawImage(image3, Rect(0, 0, Width, Height)); g.TranslateTransform((REAL)image.GetWidth(),0.f); //灰度的还原演示,还原使用最大值法处理的灰度图像image2 for(i=Width; i>0;i--) { for(int j=Height; j>0;j--) { image2->GetPixel(i,j, &color); int R=color.GetR(); int G=color.GetG(); int B=color.GetB(); //分别对RGB三种色彩分量进行伪彩色还原 //进行红色分量的还原 if(R<127) R=0; if(R>=192) R=255; if(R<=191&&R>=128) R=4*R-510; /*进行绿色分量的还原,为了还原后的绿色分量再次参加比较, 这里设置一个变量YES表示G是否已经参加了比较*/ bool yes; yes=false; if(G<=191&&G>=128&&(!yes)) { G=255; yes=true; } if(G>=192&&(!yes)) { G=1022-4*G; yes=true; } if(G<=63&&(!yes)) { G=254-4*G; yes=true; } if(G<=127&&G>=67&&(!yes)) G=4*G-257; //进行蓝色分量的还原 if(B<=63) B=255; if(B>=128) B=0; if(B>=67&&B<=127) B=510-4*B; //还原后的伪彩色 Color colorResult(255,R,G,B); //将还原后的RGB信息重新写入位图 image2->SetPixel(i, j, colorResult); } } //重新绘制还原后的伪彩色位图 //重新绘制灰度化图 g.DrawImage(image2, Rect(0, 0, Width, Height)); delete image2; delete image3; } // void Inverse_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); graphics.ScaleTransform(0.7f,0.7f); Bitmap image(L"head.bmp"); int Width =image.GetWidth(); int Height =image.GetHeight(); Color colorTemp,color2; Color color; //绘制原图 graphics.DrawImage(&image, Rect(0, 0, Width, Height)); for(int i=0;i0;i--) { for( int j=Height-1; j>0;j--) { //获取相邻两个像素的R、G、B值 image.GetPixel(i, j, &color); image.GetPixel(i-1, j-1, &colorLeft); //计算与左上角像素的RGB分量之差 //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果 int r=max(67,min(255, abs(color.GetRed()-colorLeft.GetRed()+128))); int g=max(67,min(255, abs(color.GetGreen()-colorLeft.GetGreen()+128))); int b=max(67,min(255, abs(color.GetBlue()-colorLeft.GetBlue()+128))); Color colorResult(255,r,g,b); //将计算后的RGB值回写到位图 image.SetPixel(i, j,colorResult); } //绘制浮雕图 graphics.DrawImage(&image, Rect(Width+10, 0, Width, Height)); } //进行图片的雕刻处理 for(i=0; iGetPixel(j, i, &color); image2->GetPixel(j+1, i+1, &colorLeft); //计算与右下角像素的分量之差 //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果 int r=max(67,min(255,abs(color.GetRed()-colorLeft.GetRed()+128))); int g=max(67,min(255,abs(color.GetGreen()-colorLeft.GetGreen()+128))); int b=max(67,min(255,abs(color.GetBlue()-colorLeft.GetBlue()+128))); Color colorResult(255,r,g,b); image2->SetPixel(j, i,colorResult); } //绘制雕刻图 graphics.DrawImage(image2, Rect(Width*2+20, 0, Width, image.GetHeight())); } delete image2; } void CreatePenFromBrush_Click() { Graphics &g=GetGraphics(); g.Clear(Color::White); //构造线性渐变画刷 LinearGradientBrush LineargradientBrush(Rect(0,0,10,10), Color::Blue,Color::Red, LinearGradientModeBackwardDiagonal); //从线性渐变画刷中构造画笔 Pen pen(&LineargradientBrush); pen.SetWidth(10); //绘制矩形 g.DrawRectangle(&pen,10,10,100,100); //装入纹理图片 Bitmap image(L"butterfly.bmp"); //构造纹理画刷 TextureBrush tBrush(&image); //将画刷传入画笔的构造函数 Pen texturedPen(&tBrush,42); //设置贝塞尔曲线的起止点及控制点 Point p1(10, 100); Point c1= Point(100, 10); Point c2= Point(150, 150); Point p2= Point(200, 100); g.TranslateTransform(130,0); //绘制贝塞尔曲线 g.DrawBezier(&texturedPen, p1, c1, c2, p2); } //画笔的线形演示 void DashStyle_Custom_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 5); //设置文本输出对齐方式及字体 StringFormat fmt; fmt.SetAlignment(StringAlignmentNear); fmt.SetLineAlignment(StringAlignmentCenter); //字体 Font font(L"Arial",12); SolidBrush sBrush(Color::Black); graphics.TranslateTransform(0,30); int i=0; //分别使用常见的五种线型绘制直线 for(;i<5;i++) { //设置线型 pen.SetDashStyle((DashStyle)i); graphics.DrawLine(&pen, 10, 30*i, 260, 30*i); //输出当前线型的名称 CString s; s.Format(_T("%d"), i+1); MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt); s.ReleaseBuffer(); } //使用自定义义线型 float dashVals[]= { 5.0f, // 线长5个像素 2.0f, // 间断2个像素 15.0f, // 线长15个像素 4.0f // 间断4个像素 }; pen.SetDashPattern(dashVals, 4); pen.SetColor(Color::Red); graphics.DrawLine(&pen, 10, 30*i, 260, 30*i); CString s; s.Format(_T("%d"), i+1); MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt); } void Pen_Align_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Gray, 1.0f); Pen pen2(Color(155,255,0,0),10); int i=0; for(; i<5;i++) { pen2.SetAlignment((PenAlignment)i); graphics.DrawLine(&pen2, Point(0,10), Point(60,10)); graphics.TranslateTransform(70,0); } graphics.ResetTransform(); graphics.DrawLine(&pen,0,10,600,10); } void Pen_Tranform_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //构造一支宽度为5的红色画笔 Pen pen(Color::Red, 3.0f); //将画笔从垂直方向扩充6倍,水平方向保持不变 pen.ScaleTransform(1, 6); //使用未经旋转处理的画笔画圆 graphics.DrawEllipse(&pen, 0, 50, 80, 80); //60°旋转 graphics.TranslateTransform(100,0); pen.RotateTransform(60, MatrixOrderAppend); graphics.DrawEllipse(&pen, 0, 50, 80, 80); //120°旋转 graphics.TranslateTransform(100,0); pen.RotateTransform(60, MatrixOrderAppend); graphics.DrawEllipse(&pen, 0, 50, 80, 80); //180°旋转 graphics.TranslateTransform(100,0); pen.RotateTransform(60, MatrixOrderAppend); graphics.DrawEllipse(&pen, 0, 50, 80, 80); } //线帽演示 void Pen_LineCap_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //设置文本输出对齐方式及字体 StringFormat fmt; fmt.SetAlignment(StringAlignmentNear); fmt.SetLineAlignment(StringAlignmentCenter); //字体 Font font(L"Arial",12); SolidBrush sBrush(Color::Black); //创建宽度为15的画笔 Pen pen(Color::Black,15); //分别使用不同的线帽 LineCap lincap[]= { LineCapFlat, LineCapSquare, LineCapRound, LineCapTriangle, LineCapNoAnchor, LineCapSquareAnchor, LineCapRoundAnchor, LineCapDiamondAnchor, LineCapArrowAnchor, LineCapCustom, LineCapAnchorMask }; for (int i=0; icolumn_count-1) { column+=2; rol=0; } //创建临时画刷 HatchBrush brush_tmp(style, black, white); //填充矩形:设置宽度为WIDTH-20的目的是让矩形之间留出间隔 graphics.FillRectangle(&brush_tmp, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT); //绘制矩形边框 graphics.DrawRectangle(&pen, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT); //显示每种画刷风格的枚举名称 //计算文本输出区域 RectF layoutRect(rol*WIDTH, (column+1)*HEIGHT, WIDTH, HEIGHT); StringFormat format; //设置文本输出格式:水平、垂直居中 format.SetAlignment(StringAlignmentNear); format.SetLineAlignment(StringAlignmentCenter); //在矩形框中央输出枚举值 CString s; s.Format(_T("%d"), style); MyDrawString(graphics, s, myFont, redBrush, layoutRect, format); rol+=1; } } //设置绘制原点 void Brush_SetRenderingOrigin_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //设定画刷的前景色为黑色,背景色为白色 Color black=Color::Black; Color white=Color::White; HatchBrush hatchBrush(HatchStyleDarkDownwardDiagonal,black,white); //在竖直方向填充8个矩形,使用默认的画刷原点 for(int i=0;i<8;i++) { graphics.FillRectangle(&hatchBrush, 0, i*50, 100, 50); } //使用不同的绘制原点进行区域填充 for(i=0;i<8;i++) { //设置画刷原点(水平方向递增) graphics.SetRenderingOrigin(i, 0); graphics.FillRectangle(&hatchBrush, 100, i*50, 100, 50); } } //纹理画刷的不同加载方式 void Brush_Texture_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"宋体",12); //定义纹理画刷的不同填充区域 RectF rect1(10, 10, 200,200); RectF rect2(210, 10, 200, 200); RectF rect3(410, 10, 200, 200); //装入纹理图片 Bitmap image(L"nemo.bmp"); //构造纹理画刷1:使用默认的方式 TextureBrush tBrush(&image); //使用纹理画刷填充圆形区域 graphics.FillEllipse(&tBrush,rect1); //绘制圆周 graphics.DrawEllipse(&pen,rect1); MyDrawString(graphics, "图片原始大小", myFont, brush, PointF(40,220)); //构造纹理画刷2:只使用给定图片的部分区域 TextureBrush tBrush2(&image, Rect(55,35,55,35)); graphics.FillEllipse(&tBrush2,rect2); graphics.DrawEllipse(&pen,rect2); MyDrawString(graphics, "使用部分截图", myFont, brush, PointF(240,220)); //构造纹理画刷3:将使用图片的画刷进行缩放 TextureBrush tBrush3(&image); //对画刷进行50%的缩放 Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f); tBrush3.SetTransform(&mtr); graphics.FillEllipse(&tBrush3,rect3); graphics.DrawEllipse(&pen,rect3); MyDrawString(graphics, "比例缩小图片",myFont,brush, PointF(440,220)); } //使用图片排列方式 void Brush_Texture_WrapMode_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"Arial", 13); //装入纹理图片 Bitmap image(L"nemo.bmp"); //构造纹理画刷 TextureBrush tBrush(&image); //将画刷进行缩放 Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f); tBrush.SetTransform(&mtr); int i=0; //对图片不使用排列方式 tBrush.SetWrapMode(WrapModeClamp); graphics.FillRectangle(&tBrush, Rect(i*150,0,150,150)); graphics.DrawRectangle(&pen, Rect(i*150,0,150,150)); MyDrawString(graphics, "Clamp", myFont,brush, PointF(0,155)); i+=1; //对图片使用平铺排列方式 tBrush.SetWrapMode(WrapModeTile); graphics.FillRectangle(&tBrush, Rect(i*150+10,0,150,150)); graphics.DrawRectangle(&pen, Rect(i*150+10,0,150,150)); MyDrawString(graphics, "Tile",myFont,brush, PointF(170,155) ); //对图片使用水平翻转排列方式 i+=1; tBrush.SetWrapMode(WrapModeTileFlipX); graphics.FillRectangle(&tBrush, Rect(i*150+20,0,150,150)); graphics.DrawRectangle(&pen, Rect(i*150+20,0,150,150)); MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(320,155)); //对图片使用垂直翻转排列方式 tBrush.SetWrapMode(WrapModeTileFlipY); graphics.FillRectangle(&tBrush, Rect(0,180,150,150)); graphics.DrawRectangle(&pen, Rect(0,180,150,150)); MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,335)); //对图片使用水平、垂直同时翻转排列方式 tBrush.SetWrapMode(WrapModeTileFlipXY); graphics.FillRectangle(&tBrush, Rect(160,180,150,150)); graphics.DrawRectangle(&pen, Rect(160,180,150,150)); MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,335)); } //纹理画刷的变换 void Brush_TextureTransform_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //为三种不同的变换方式的画刷定义填充区域 RectF rect1= RectF (10, 10, 200,200); RectF rect2= RectF (210, 10, 200, 200); RectF rect3= RectF (410, 10, 200, 200); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"宋体", 12); //装入纹理图片 Bitmap image(L"nemo.bmp"); //构造纹理画刷 TextureBrush tBrush(&image); //将画刷旋转30度 tBrush.RotateTransform(30); graphics.FillEllipse(&tBrush,rect1); graphics.DrawEllipse(&pen,rect1); MyDrawString(graphics, "旋转30度",myFont,brush, PointF(40,220)); //重置变换矩阵:恢复到变化前的状态 tBrush.ResetTransform(); //将画刷在水平方向上扩大三倍 tBrush.ScaleTransform(3, 1); graphics.FillEllipse(&tBrush, rect2); graphics.DrawEllipse(&pen,rect2); MyDrawString(graphics, "横向扩充三倍",myFont,brush, PointF(240,220)); //平移变换 tBrush.ResetTransform(); //将画刷在水平方向上平移30个像素 tBrush.TranslateTransform(30, 0, MatrixOrderAppend); graphics.FillEllipse(&tBrush, rect3); graphics.DrawEllipse(&pen,rect3); MyDrawString(graphics, "横向平移30个像素",myFont,brush, PointF(440,220)); } //查询画刷的变换信息 void Brush_GetTextureMatrix_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Matrix matrix; float elements[6]; RectF rect1(10, 10, 200,200); Pen pen(Color::Black, 2); SolidBrush brush(Color::Black); Bitmap image(L"nemo.bmp"); TextureBrush tBrush(&image); //进行三种任意变换 tBrush.RotateTransform(30); tBrush.TranslateTransform(5,3); tBrush.ScaleTransform(0.5f,2.0f); //获取目前已经进行的画刷变换 tBrush.SetTransform(&matrix); //从矩形到数组 matrix.GetElements(elements); graphics.FillEllipse(&tBrush,rect1); graphics.DrawEllipse(&pen,rect1); //输出矩阵的元素 for(int j = 0; j <6; ++j) { CString s; s.Format(_T("%d %f 矩阵元素值"), j, elements[j]); AfxMessageBox(s); } } //线性渐变画刷程序 void Brush_LinearGradientBrush_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //定义一个由红色到蓝色渐变的画刷:水平变换区域的宽度为40 //竖直方向不进行色彩渐变 LinearGradientBrush linGrBrush( Point(0, 0), Point(40, 0), Color::Red, //起点色彩 Color::Blue); //止点色彩 //定义一个色彩呈对角线变换的区域,区域大小为40*40 LinearGradientBrush linGrBrush2( Point(0, 0), Point(40, 40), Color::Red, //起点色彩 Color::Blue); //止点色彩 //分别演示不同的线性渐变画刷对目标区域的不同填充效果 graphics.FillRectangle(&linGrBrush, 0, 0, 200, 200); graphics.FillRectangle(&linGrBrush2, 240, 0, 200, 200); Pen pen(Color::Gray,1); //在对角线方向上绘制单个画刷的填充区域 for(int i=0;i<5;i++) { graphics.DrawRectangle(&pen, 240+i*40,i*40,40,40); } } //控制线性渐变画刷的填充方式 void Brush_LinearArrange_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"Arial", 13); //定义一个色彩呈对角线变换的区域,区域大小为40*40 LinearGradientBrush linGrBrush( Point(0, 0), Point(40, 40), Color::Red, //起点色彩 Color::Blue); //止点色彩 int i=0; //对渐变画刷使用平铺排列方式(默认方式) linGrBrush.SetWrapMode(WrapModeTile); //填充一个大小为160的正方形 graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,0,160,160)); MyDrawString(graphics, "Tile", myFont, brush, Point(20,165)); //对渐变画刷使用水平翻转排列方式 i+=1; linGrBrush.SetWrapMode(WrapModeTileFlipX); //重置绘图平面原点 graphics.SetRenderingOrigin(160,0); graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,160,0,160)); MyDrawString(graphics, "TileFlipX", myFont, brush, Point(170,165)); //对渐变画刷使用垂直翻转排列方式 linGrBrush.SetWrapMode(WrapModeTileFlipY); //重置绘图平面原点 graphics.SetRenderingOrigin(0,200); graphics.FillRectangle(&linGrBrush, Rect(0,200,160,160)); graphics.DrawRectangle(&pen, Rect(0,200,200,160)); MyDrawString(graphics, "TileFlipY",myFont,brush, Point(0,375)); //对渐变画刷使用水平、垂直同时翻转排列方式 linGrBrush.SetWrapMode(WrapModeTileFlipXY); graphics.SetRenderingOrigin(160,200); graphics.FillRectangle(&linGrBrush, Rect(160,200,160,160)); graphics.DrawRectangle(&pen, Rect(160,200,160,160)); MyDrawString(graphics, "TileFlipXY",myFont,brush, Point(170,375)); //标注渐变画刷渐变区域的大小:画格子 Pen pen2(Color::Gray, 1); for(i=0;i<8;i++) graphics.DrawLine(&pen2,0,i*40,320,i*40); for(i=0;i<9;i++) graphics.DrawLine(&pen2,i*40,0,i*40,360); } //定义线性渐变画刷的渐变模式 void Brush_LinearGradientMode_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"Arial", 12); //定义一个水平渐变画刷,大小40*20 LinearGradientBrush linGrBrush1( Rect(0, 0, 40, 20), Color::Red, Color::Blue,LinearGradientModeHorizontal); //定义一个垂直渐变画刷 LinearGradientBrush linGrBrush2( Rect(0, 0, 40, 20), Color::Red, Color::Blue,LinearGradientModeVertical); //从右上到左下的渐变画刷 LinearGradientBrush linGrBrush3( Rect(0, 0, 40, 20), Color::Red, Color::Blue,LinearGradientModeForwardDiagonal); //从左上到左下的渐变画刷 LinearGradientBrush linGrBrush4( Rect(0, 0, 40, 20), Color::Red, Color::Blue,LinearGradientModeBackwardDiagonal); int i=0; //使用水平渐变的画刷填充区域 graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,0,160,160)); MyDrawString(graphics, "水平渐变",myFont,brush, PointF(20,165)); i+=1; //重置绘图平面原点 graphics.SetRenderingOrigin(160,0); //使用垂直渐变的画刷填充区域 graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,160,0,160)); MyDrawString(graphics, "垂直渐变",myFont,brush, PointF(170,165)); //重置绘图平面原点 graphics.SetRenderingOrigin(0,200); //使用从右上到左下渐变的画刷填充区域 graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160)); graphics.DrawRectangle(&pen, Rect(0,200,200,160)); MyDrawString(graphics, "左上->右下",myFont,brush, PointF(0,375)); graphics.SetRenderingOrigin(160,200); graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160)); //使用从左上到右下渐变的画刷填充区域 graphics.DrawRectangle(&pen, Rect(160,200,160,160)); MyDrawString(graphics, "右上->左下",myFont,brush, PointF(170,375)); //在不同的区域中标注定义渐变方向的矩形 Pen pen2(Color::Gray, 1); for(i=0;i<18;i++) graphics.DrawLine(&pen2,0,i*20,320,i*20); for(i=0;i<9;i++) graphics.DrawLine(&pen2,i* 40,0,i*40,360); } //渐变线偏转角度 void Brush_LinearAngle_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"Arial", 12); //定义一个渐变线偏转角度为30度的渐变画刷,大小40*20 LinearGradientBrush linGrBrush1( Rect(0, 0, 40, 20), Color::Red, Color::Blue,30.0f); //定义一个渐变线偏转角度为45度的渐变画刷 LinearGradientBrush linGrBrush2( Rect(0, 0, 40, 20), Color::Red, Color::Blue,45.0f); //定义一个渐变线偏转角度为90度的渐变画刷 LinearGradientBrush linGrBrush3( Rect(0, 0, 40, 20), Color::Red, Color::Blue,90.0f); //定义一个渐变线偏转角度为180度的渐变画刷 LinearGradientBrush linGrBrush4( Rect(0, 0, 40, 20), Color::Red, Color::Blue,180.0f); int i=0; //使用偏转角度为30度的渐变画刷填充区域 graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,0,160,160)); MyDrawString(graphics, "30度偏转",myFont,brush, PointF(20,165)); i+=1; //重置绘图平面原点 graphics.SetRenderingOrigin(160,0); //使用偏转角度为45度的渐变画刷填充区域 graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160)); graphics.DrawRectangle(&pen, Rect(i*160,160,0,160)); MyDrawString(graphics, "45度偏转",myFont,brush, PointF(170,165)); //重置绘图平面原点 graphics.SetRenderingOrigin(0,200); //使用偏转角度为90度的渐变画刷填充区域 graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160)); graphics.DrawRectangle(&pen, Rect(0,200,200,160)); MyDrawString(graphics, "90度偏转",myFont,brush, PointF(0,375)); graphics.SetRenderingOrigin(160,200); graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160)); //使用偏转角度为180度的渐变画刷填充区域 graphics.DrawRectangle(&pen, Rect(160,200,160,160)); MyDrawString(graphics, "180度偏转",myFont,brush, PointF(170,375)); //在不同的区域中标注定义渐变方向的矩形 Pen pen2(Color::Gray, 1); for(i=0;i<18;i++) graphics.DrawLine(&pen2,0,i*20,320,i*20); for(i=0;i<9;i++) graphics.DrawLine(&pen2,i* 40,0,i*40,360); } //多色渐变画刷 void Brush_LinearInterpolation_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //定义三种参与渐变的色彩 Color colors[] = { Color::Red, // 红色 Color::Green,//过渡色为绿色 Color::Blue // 蓝色 }; float positions[] = { 0.0f, // 由红色起 0.3f, // 绿色始于画刷长度的三分之一 1.0f // 到蓝色止 }; //构造一条从黑色到白色的渐变画刷 LinearGradientBrush linGrBrush( Point(0, 0), Point(180, 0), Color::Black,Color::White); //设置渐变画刷的多色渐变信息 //linGrBrush.InterpolationColors=clrBlend; linGrBrush.SetInterpolationColors(colors, positions, 3); //使用多色渐变画刷填充目标区域 graphics.FillRectangle(&linGrBrush, 0, 0, 180, 100); //使用普通的方法实现多色渐变 //由红到绿,长度60 LinearGradientBrush linGrBrush1( Point(0, 0), Point(60, 0), Color::Red, Color::Green); //由绿到蓝,长度120 LinearGradientBrush linGrBrush2( Point(60, 0), Point(181, 0), Color::Green, Color::Blue); //分别使用两个画刷填充两个相邻区域,形成多色渐变 graphics.FillRectangle(&linGrBrush1, 0, 120, 60, 100); graphics.FillRectangle(&linGrBrush2, 60, 120, 120, 100); } //自定义渐变过程:三角形 void Brush_LinearCustomize_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); SolidBrush brush(Color::Black); Font myFont(L"宋体", 12); //定义一个双色渐变画刷 LinearGradientBrush linGrBrush( Point(0, 0), Point(250, 0), Color::Red, Color::Blue); //绘制两条使用默认渐变方式填充的矩形,以用作比较 graphics.FillRectangle(&linGrBrush, 0, 0, 250, 15); graphics.FillRectangle(&linGrBrush, 500, 0, 250, 15); //依次改变合成点位置 int row=1; for(float i=0.0f;i<1.0f;i+=0.1f) { linGrBrush.SetBlendTriangularShape(i); graphics.FillRectangle(&linGrBrush, 0, row*15, 250, 15); row++; } MyDrawString(graphics, "改变合成点位置", myFont,brush, PointF(40,200)); //依次改变彩色合成因子 row=1; for(i=0.0f;i<1.0f;i+=0.1f) { //将合成的相对位置设置在整个区域的50%处 linGrBrush.SetBlendTriangularShape (0.5f,i); graphics.FillRectangle(&linGrBrush, 500, row*15, 250, 15); row++; } MyDrawString(graphics, "改变色彩合成因子", myFont,brush, PointF(540,200)); } //基于钟形曲线的渐变画刷 void Brush_LinearGradientBrush_BellShape_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Rect myRect(10, 10, 150, 75); //定义线性渐变画刷 LinearGradientBrush myLGBrush( myRect,Color::Blue,Color::Red,0.0f,true); //使用默认的线性渐变画刷填充椭圆 graphics.FillEllipse(&myLGBrush, myRect); //将渐变过程设置成基于钟形曲线的渐变 myLGBrush.SetBlendBellShape(.5f, 1.0f); //使用自定义渐变过程的画刷填充椭圆 graphics.TranslateTransform(160,0); graphics.FillEllipse(&myLGBrush, myRect); } void Brush_PathGradientBrush_Star_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Gray,1); SolidBrush pthGrBrush(Color::Red); SolidBrush blackbrush(Color::Blue); graphics.TranslateTransform(20,20); //构造五星的10个边的端点坐标 Point points[] = { Point(75, 0), Point(100, 50), Point(150, 50), Point(112, 75), Point(150, 150), Point(75, 100), Point(0, 150), Point(37, 75), Point(0, 50), Point(50, 50), Point(75, 0) }; // 创建路径 GraphicsPath path; //在路径中加入直线 path.AddLines(points, sizeof(points)/sizeof(Point)); //填充路径 graphics.FillPath(&pthGrBrush, &path); //绘制边界 graphics.DrawLines(&pen, points, sizeof(points)/sizeof(Point)); //绘制定义10个边的端点 for(int i=0;i<10;i++) //每个圆点的直径为10 graphics.FillEllipse(&blackbrush, points[i].X-5,points[i].Y-5,10,10); } //使用路径渐变画刷绘制五星 void Brush_PathGradientBrush_Star2_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //构造五星的10个边的端点坐标 Point points[] = { Point(75, 0), Point(100, 50), Point(150, 50), Point(112, 75), Point(150, 150), Point(75, 100), Point(0, 150), Point(37, 75), Point(0, 50), Point(50, 50), Point(75, 0) }; // 创建路径 GraphicsPath path; //在路径中添加直线 path.AddLines(points, sizeof(points)/sizeof(Point)); //创建路径渐变画刷 PathGradientBrush pthGrBrush(&path); //设置中心点色彩(终点色) pthGrBrush.SetCenterColor(Color::Red); //设置每个端点的色彩(终点色) Color colors[] = { Color::Black, Color::Green, Color::Blue, Color::White, Color::Black, Color::Green, Color::Blue, Color::White, Color::Black, Color::Green }; //设置路径渐变画刷的边缘色 int clrCount=sizeof(colors)/sizeof(Color); pthGrBrush.SetSurroundColors(colors, &clrCount); // 填充目标路径 graphics.FillPath(&pthGrBrush, &path); //将中心色及边界色都设置成随机色,查看渐变效果 for(int z=0;z<10;z++) { //在水平方向上平移绘图平面 graphics.TranslateTransform(200.0f, 0.0f); //设置中心点色彩为随机色 pthGrBrush.SetCenterColor(_MakeA_RGB(rand_Next(255)%155,rand_Next(255)%255,rand_Next(255)%255)); //使用原有的边缘色 pthGrBrush.SetSurroundColors(colors, &clrCount); graphics.FillPath(&pthGrBrush, &path); } } //使用多个路径渐变画刷 void Brush_Using_MorePathGradientBrush_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //定义一个六边形,边长为50 float fHalf = 50 * (float)sin(120.0f/360.0f*PI); PointF pts[] = { PointF( 50, 0), PointF( 50 * 1.5f, 0), PointF( 50, 0), PointF( 50 / 2, -fHalf), PointF(-50 / 2, -fHalf), PointF(-50, 0), PointF(-50 * 1.5f, 0), PointF(-50, 0), PointF(-50 / 2, fHalf), PointF( 50 / 2, fHalf) }; //构造六边形渐变画刷 PathGradientBrush pgbrush1(pts, 10); //在水平和垂直方向上平移六边形的顶点 for (int i = 0; i <10; i++) { pts[i].X += 50*1.5f; pts[i].Y += fHalf; } //根据改变坐标后的点重新生成路径渐变画刷 PathGradientBrush pgbrush2(pts, 10); //设置路径渐变画刷的翻转方式为平铺 pgbrush1.SetWrapMode(WrapModeTile); pgbrush2.SetWrapMode(WrapModeTile); //分别设置两个画刷的中心点色彩为红、绿色 pgbrush1.SetCenterColor(Color::Red); pgbrush2.SetCenterColor(Color::Green); //填充当前窗口 graphics.FillRectangle(&pgbrush1, 0, 0,MyClient.Width,MyClient.Height); //在上次未被填充的区域中再次填充当前窗口的空白部份 graphics.FillRectangle(&pgbrush2, 0, 0,MyClient.Width,MyClient.Height); } //路径渐变画刷的填充方式 void Brush_PathGradientBrush_WrapMode_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Blue, 2); SolidBrush brush(Color::Black); Font myFont(L"Arial", 12); //定义五星的边线坐标,为演示翻转效果,将五星的一角拉长 Point points[] = { Point(75, 0), Point(100, 50), Point(150, 50), Point(112, 75), Point(150, 150), Point(75, 100), Point(0, 190), Point(37, 75), Point(10, 50), Point(50, 50) }; GraphicsPath path; path.AddLines(points, sizeof(points)/sizeof(Point)); // 构造路径渐变画刷 PathGradientBrush pthGrBrush(&path); //设置中心点色彩(终点色) pthGrBrush.SetCenterColor(Color::Red); //设置每个端点的色彩(终点色) Color colors[]= { Color::Black, Color::Green, Color::Blue, Color::White, Color::Black, Color::Green, Color::Blue, Color::White, Color::Black, Color::Green }; int clrCount=sizeof(colors)/sizeof(Color); pthGrBrush.SetSurroundColors(colors, &clrCount); //缩小画刷 pthGrBrush.ScaleTransform(0.2f,0.2f); int i=0; //对渐变画刷使用平铺排列方式(默认方式) pthGrBrush.SetWrapMode(WrapModeTile); graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120)); graphics.DrawRectangle(&pen, Rect(i*120,0,120,120)); MyDrawString(graphics, "Tile",myFont,brush, PointF(20,125)); i+=1; pthGrBrush.SetWrapMode(WrapModeTileFlipX); graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120)); graphics.DrawRectangle(&pen, Rect(i*120,0,120,120)); MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(170,125)); //对渐变画刷使用垂直翻转排列方式 pthGrBrush.SetWrapMode(WrapModeTileFlipY); graphics.FillRectangle(&pthGrBrush, Rect(0,200,120,120)); graphics.DrawRectangle(&pen, Rect(0,200,120,120)); MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,325)); //对渐变画刷使用水平、垂直同时翻转排列方式 pthGrBrush.SetWrapMode(WrapModeTileFlipXY); graphics.FillRectangle(&pthGrBrush, Rect(120,200,120,120)); graphics.DrawRectangle(&pen, Rect(120,200,120,120)); MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,325)); //输出路径的约束矩形、中心点信息 RectF rect; //获取画刷的约束矩形对象 pthGrBrush.GetRectangle(&rect); PointF CenterPoint; //获取画刷的中心点信息 pthGrBrush.GetCenterPoint(&CenterPoint); CString tmp; //格式化字符串 tmp.Format("当前约束矩形的左上坐标为(%f,%f),宽度=%f 高度=%f\n当前渐变路径的中心点坐标为(%.2f,%.2f)", rect.X,rect.Y,rect.Height,rect.Width, CenterPoint.X,CenterPoint.Y); //输出中心点及约束矩形对象的信息 MyDrawString(graphics, tmp,myFont,brush, PointF(0,395)); } //更改路径渐变画刷的中心点 void Brush_PathGradientBrush_CenterPoint_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //构造一个圆形区域 GraphicsPath path; path.AddEllipse(0, 0, 200, 200); SolidBrush brush(_MakeA_RGB(155,Color::Red)); //构造一个圆形路径渐变画刷 PathGradientBrush pthGrBrush(&path); //设置中心点色彩 pthGrBrush.SetCenterColor(_MakeA_RGB(155,Color::White)); Color colors[] ={_MakeA_RGB(55, Color::Blue)}; //设置边缘色 int clrCount=1; pthGrBrush.SetSurroundColors(colors, &clrCount); //填充区域,使用单色画刷 graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200); //获取中心点色彩 PointF center(0,0); pthGrBrush.GetCenterPoint(¢er); //更改画刷的中心点沿圆周的上半部分平移 for(int i=0;i<200;i++) { center.X=(REAL)i; center.Y=10.f; pthGrBrush.SetCenterPoint(center); graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200); //标记当前中心点 graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f); } //更改画刷的中心点沿圆周的下半部分平移 for(i=200;i>0;i--) { center.X=(REAL)i; center.Y=190; pthGrBrush.SetCenterPoint(center); graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200); //标记当前中心点 graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f); } } //对路径渐变画刷使用多色渐变 void Brush_PathGradientBrush_InterpolationColors_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); SolidBrush brush(Color::Red); //设置三角形的三个点 Point points[]= { Point(100, 0), Point(200, 200), Point(0, 200) }; //创建一个三角形渐变画刷 PathGradientBrush pthGrBrush(points, 3); //-定义参与渐变的色彩 Color colors[]= { Color::Red, //红 Color::Green, //绿 Color::Blue // 蓝 }; //设置合成点的位置 float pos[] = { 0.0f, // 红色在区域边界为红色 0.4f, //在距离中心40%的位置处使用绿色 1.0f //中心点使用蓝色 }; //设置渐变的过渡色 pthGrBrush.SetInterpolationColors(colors, pos, 3); //填充区域 graphics.FillRectangle(&pthGrBrush, 0, 0, 300, 300); //标记中心点 PointF centerpoint(0,0);; pthGrBrush.GetCenterPoint(¢erpoint); graphics.FillEllipse(&brush, centerpoint.X-5.f, centerpoint.Y-5.f, 10.f, 10.f); } //更改路径渐变画刷的焦点缩放比例 void Brsuh_PathGradietBrush_Focus_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen pen(Color::Black,3); SolidBrush brush(Color::Black); Font myFont(L"Arial", 12); //指定三角形三点坐标,创建三角形渐变画 Point points[]= { Point(100, 0), Point(200, 200), Point(0, 200) }; PathGradientBrush pthGrBrush(points, 3); //指明渐变色 Color colors[] = {Color::Red, Color::Blue}; //指明色彩合成位置 float relativePositions[]= { 0.0f, // 红色做边界 1.0f // 蓝色为中心 }; //设置渐变色 pthGrBrush.SetInterpolationColors(colors, relativePositions, 2); //使用默认缩放因子进行填充 graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200); //获取默认缩放因子 PointF FocusScales(0,0); pthGrBrush.GetFocusScales(&FocusScales.X, &FocusScales.Y); //输出缩放信息 CString tmp; tmp.Format("水平:x=%.2f\n垂直:y=%.2f",FocusScales.X,FocusScales.Y); MyDrawString(graphics, tmp,myFont,brush, PointF(0,210)); //平移绘图平面 graphics.TranslateTransform(200,0); //更改缩放因子 FocusScales.X=0.6f; FocusScales.Y=0.6f; pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y); graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200); MyDrawString(graphics, "水平:x=0.6\n垂直:y=0.6", myFont,brush, PointF(0,210)); //水平缩放不等于垂直缩放时特例 graphics.TranslateTransform(200,0); //更改缩放因子 FocusScales.X=0.1f; FocusScales.Y=0.8f; pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y); graphics.FillRectangle(&pthGrBrush,0, 0, 200, 200); MyDrawString(graphics, "水平:x=0.1\n垂直:y=0.8", myFont,brush, PointF(0,210)); //水平缩放=垂直缩放=1.0 graphics.TranslateTransform(200,0); FocusScales.X=1.0f; FocusScales.Y=1.0f; pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y); graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200); MyDrawString(graphics, "水平:x=1.0\n垂直:y=1.0", myFont,brush, PointF(0,210)); //水平缩放不等于垂直缩放时特例:放大原有区域 graphics.TranslateTransform(200,0); FocusScales.X=2.0f; FocusScales.Y=1.5f; pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y); graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200); MyDrawString(graphics, "水平:x=2.0\n垂直:y=1.5", myFont,brush, PointF(0,210)); } //路径渐变画刷的变换 void Brush_PathGradientBrush_Transform_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); // 定义一个三角形路径渐变画刷 Point pts[]={Point(50, 0), Point(100, 100), Point(0, 100)}; PathGradientBrush pthGrBrush(pts, 3); //对画刷使用垂直翻转方式 pthGrBrush.SetWrapMode(WrapModeTileFlipY); //缩小画刷 pthGrBrush.ScaleTransform(0.5f, 0.5f); //填充当前窗口 graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height); //将画刷旋转90度 pthGrBrush.RotateTransform(90.0f, MatrixOrderAppend); //再次填充当前窗口 graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height); } void Brsuh_LinearGradientBrush_UsingGamma_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); LinearGradientBrush linGrBrush( Point(0, 10), Point(200, 10), Color::Red, Color::Blue); graphics.FillRectangle(&linGrBrush, 0, 0, 200, 50); linGrBrush.SetGammaCorrection(true); graphics.FillRectangle(&linGrBrush, 0, 60, 200, 50); } //简单的使用字体示意 void Font_UsingFontInGDIPlus_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //新建一个字体对话框 CFontDialog dlg; //允许对字体进行色彩设置 //dlg.ShowColor=true; //调用字体选择对话框 if(dlg.DoModal()!=IDOK) return; //获取在字体对话框中的字体信息 LOGFONT lf; dlg.GetCurrentFont(&lf); Font myFont(CWideCharString(dlg.GetFaceName()), lf.lfHeight); //根据字体色彩创建画刷 SolidBrush brush(dlg.GetColor()); //设置文本输出格式:在当前窗口中居中显示 StringFormat fmt; fmt.SetAlignment(StringAlignmentCenter); fmt.SetLineAlignment(StringAlignmentCenter); //输出文本 graphics.DrawString(L"输出文本", 4, &myFont, RectF(0,0,MyClient.Width,MyClient.Height),&fmt, &brush); } //枚举所有的字体系列 void Font_EnumAllFonts_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); SolidBrush solidBrush(Color::Black); FontFamily fontFamily(L"Arial"); Font font(&fontFamily, 8, FontStyleRegular, UnitPoint); //设置文本输出格式 StringFormat fmt; fmt.SetAlignment(StringAlignmentNear); fmt.SetLineAlignment(StringAlignmentNear); CString tmp=""; //获取所有已经安装的字体系列 InstalledFontCollection installedFontCollection; int ffcount=installedFontCollection.GetFamilyCount(); FontFamily *fontfamily = ::new FontFamily[ffcount]; installedFontCollection.GetFamilies(ffcount, fontfamily, &ffcount); int index=0; //访问fontfamily数组的每一个成员 for( int i=0; i200) ffcount=200; privateFontCollection.GetFamilies(ffcount, fontfamily, &ffcount); int index=0; //访问私有字体集合中的所有字体系列 for(int i=0; i ../ReadMe.txt TCHAR ModuleDirectory[_MAX_PATH]; ::GetModuleFileName(NULL, ModuleDirectory, _MAX_PATH); TCHAR *p=::_tcsrchr(ModuleDirectory, '\\'); *p=0; p=::_tcsrchr(ModuleDirectory, '\\'); *p=0; ::_tcscat(ModuleDirectory, "\\ReadMe.txt"); s_Filename=ModuleDirectory; FILE *stream = ::fopen(s_Filename, "rt"); if (!stream) return; CString str; //读取所有的文件内容 char buffer[1024]=""; while (!feof(stream)) { CString s; ::fgets(buffer, 1024, stream); s=buffer; s.TrimLeft(); s.TrimRight(); s+="\r\n"; str+=s; } //关闭文件 fclose(stream); //定义栏宽及栏与栏之间的间隔 int COLUMWIDTH=256; int SPACE=10; Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); SolidBrush brush(Color::Black); Font myFont(L"宋体", 10); int codepointsFitted=0; int linesFilled=0; //设置输出格式 StringFormat format; //禁用自动剪载 format.SetFormatFlags(StringFormatFlagsNoClip); //输出文本以单词结束 format.SetTrimming(StringTrimmingWord); CWideCharString strW(str); //从左至右按列(栏)输出文本 for (int x = 0; strW.GetLength()> 0&&xWiden(&greenPen); //绘图平面右移,绘制拓宽的路径 graphics.TranslateTransform(rect.Width+10,0); graphics.DrawPath(&bluePen, path2); //下移 graphics.TranslateTransform(-rect.Width,rect.Height); //使用间断线风格的绿色画笔拓宽路径 greenPen.SetDashStyle(DashStyleDash); path3->Widen(&greenPen); //填充路径 graphics.FillPath(&SolidBrush(Color::Red), path3); //对画笔进行缩放变换 Pen pentmp(Color::Blue); pentmp.ScaleTransform(3.0f, 20.0f); path4->Widen(&pentmp); //绘图平面右移 graphics.TranslateTransform(rect.Width,0); graphics.DrawPath(&bluePen, path4); delete path2; delete path3; delete path4; } void Path_WidenDemo_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Pen bluePen(Color::Blue,1); Pen greenPen(Color::Green, 20); GraphicsPath path; //添加一条直线 path.AddLine(10,20,100,20); GraphicsPath *path2=path.Clone(); PathData pathData; PathData pathData2; path.GetPathData(&pathData); int num1=path.GetPointCount(); //保存路径的定义点信息 CString msg; for(int i=0;iWiden(&greenPen); path2->GetPathData(&pathData2); int num2=path2->GetPointCount(); //绘图平面右移,绘制拓宽的路径 RectF rect; path.GetBounds(&rect); graphics.TranslateTransform(rect.Width*2,0); graphics.DrawPath(&bluePen, path2); graphics.ResetTransform(); //保存拓宽后路径的定义点信息 CString msg2; for(i=0;iB) graphics.TranslateTransform(width+10,0); //设置R->B色彩变换矩阵 imageAttributes.SetColorMatrix( &colorMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); graphics.DrawImage( &image, Rect(0, 10, width, height), 0, 0,width,height, UnitPixel, &imageAttributes); //清除已经采取的色彩变换 imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap); //重新加载另一变换矩阵Matrix2 //绿色绕着红色旋转 ColorMatrix colorMatrix2= { 1, 0, 0.0f, 0.0f, 0.0f, 0, (float)cos(r), (float)sin(r), 0.0f, 0.0f, 0.0f, -(float)sin(r), (float)cos(r), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; imageAttributes.SetColorMatrix( &colorMatrix2, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //在第二行输出 graphics.ResetTransform(); graphics.TranslateTransform(0,height+10); graphics.DrawImage(&image, Rect(0, 0, width, height), 0, 0,width, height,UnitPixel, &imageAttributes); //清除已经采取的色彩变换 imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap); //蓝色绕着红色旋 ColorMatrix colorMatrix3= { (float)cos(r), 0, -(float)sin(r), 0.0f, 0.0f, 0, 1, 0.0f, 0.0f, 0.0f, (float)sin(r), 0, (float)cos(r), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //重新加载另一变换矩阵Matrix3 imageAttributes.SetColorMatrix( &colorMatrix3, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); graphics.TranslateTransform(width+10,0); graphics.DrawImage( &image, Rect(0, 0, width, height), 0, 0, width, height,UnitPixel, &imageAttributes); } void ColorShear_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图片 Bitmap image(L"Colorinput.bmp"); ImageAttributes imageAttributes; int width = image.GetWidth(); int height = image.GetHeight(); //定义色彩变换矩阵 ColorMatrix colorMatrix= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //启用色彩变换矩阵 imageAttributes.SetColorMatrix( &colorMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //绘制源图 graphics.DrawImage(&image, 0, 0); //使用色彩变换矩阵输出图片 graphics.TranslateTransform(width+10,0); graphics.DrawImage(&image, Rect(0, 0, width, height), 0, 0,width, height,UnitPixel,&imageAttributes); } void ColorRemap_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载蓝色背景的图片 Bitmap image(L"Nemo_Blue.bmp"); ImageAttributes imageAttributes; int width = image.GetWidth(); int height = image.GetHeight(); //将蓝色替换成白色,以达到抠除的效果 ColorMap colorMap; colorMap.oldColor = Color(255, 0, 0, 255); colorMap.newColor = Color(255, 255,255,255); //设置色彩转换表 ColorMap remapTable[1]; remapTable[0]=colorMap; //设置图片的色彩信息 imageAttributes.SetRemapTable(1, remapTable, ColorAdjustTypeBitmap); //绘制原始图像 graphics.DrawImage(&image, 0, 0, width, height); //绘制已经抠除背景色的新图像 graphics.DrawImage(&image, Rect(width+10, 0, width, height), //目标区域 0, 0, // 源图左上角坐标 width, // 源图宽 height, // 源图宽 UnitPixel, //图片的色彩信息 &imageAttributes); } void SetRGBOutputChannel_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图片 Bitmap image(L"jieba.bmp"); //绘制源图 graphics.DrawImage(&image, 0, 0); int width = image.GetWidth(); int height = image.GetHeight(); ImageAttributes imageAttributes; //设置红色通道 ColorMatrix colorMatrix= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //启用色彩变换矩阵 imageAttributes.SetColorMatrix( &colorMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //使用色彩变换矩阵输出图片 graphics.TranslateTransform(width+10,0); graphics.DrawImage(&image, Rect(0, 0, width, height), 0, 0,width, height,UnitPixel,&imageAttributes); //清除已经采取的色彩变换 imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap); //设置绿色通道 ColorMatrix colorMatrix2= { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //启用色彩变换矩阵 imageAttributes.SetColorMatrix( &colorMatrix2, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //使用色彩变换矩阵输出图片 graphics.ResetTransform(); graphics.TranslateTransform(0,height+10); graphics.DrawImage(&image, Rect(0, 0, width, height), 0, 0,width, height,UnitPixel,&imageAttributes); //清除已经采取的色彩变换 imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap); //设置蓝色通道 ColorMatrix colorMatrix3= { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //启用色彩变换矩阵 imageAttributes.SetColorMatrix( &colorMatrix3, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //使用色彩变换矩阵输出图片 graphics.TranslateTransform(width+10,0); graphics.DrawImage(&image, Rect(0, 0, width, height), 0, 0,width, height,UnitPixel,&imageAttributes); } void Metafile_Click() { // Note: To record a metafile, you must construct a Graphics //object based on a Metafile object. The recording of the metafile //ends when that Graphics object is deleted or goes out of scope. //新建一个图元文件 HDC hdc =GetGraphics().GetHDC(); Metafile metaFile1(L"图元文件示例.emf", hdc); { //使用Metafile对象的地址做为绘图平面 Graphics graphics(&metaFile1); //定义一个由红到蓝的渐变色画刷 LinearGradientBrush RtoBBrush( Point(0, 10), Point(200, 10), Color::Red, Color::Blue); //定义一个由蓝到黄的渐变色画刷 LinearGradientBrush BtoYBrush( Point(0, 10), Point(200, 10), Color::Blue, Color::Yellow); Pen bluePen(Color::Blue); // 以下的操作是往屏幕上绘制一八卦图形 Rect ellipseRect(0, 0, 200, 200); Rect left(0, 50, 100, 100); graphics.DrawArc(&bluePen,left,180.0f,180.0f); Rect right(100, 50, 100, 100); graphics.FillPie(&RtoBBrush, ellipseRect,0.0f,180.0f); graphics.FillPie(&BtoYBrush, ellipseRect,180.0f,180.0f); graphics.FillPie(&RtoBBrush, left,180.0f,180.0f); graphics.FillPie(&BtoYBrush, right,0.0f,180.0f); //文本输出 SolidBrush solidBrush(Color::Black); FontFamily fontFamily(L"隶书"); Font font(&fontFamily, 27, FontStyleRegular, UnitPixel); MyDrawString(graphics, "图元文件示例", font, solidBrush, PointF(20.0f, 80.0f)); //到此,GDI+进行的只是往硬盘中存放图片信息的操作 } //将上面的绘图信息进行回放 Graphics playbackGraphics(hdc); playbackGraphics.Clear(Color::White); //打开并显示图元文件 Metafile metaFile2(L"图元文件示例.emf"); playbackGraphics.DrawImage(&metaFile2, Point(0,0)); //释放HDC playbackGraphics.ReleaseHDC(hdc); } void CroppingAndScaling_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图片 Bitmap image(L"nemo.bmp"); int width = image.GetWidth(); int height = image.GetHeight(); // 目标显示区域在源图大小的基础上放大1.4倍 RectF destinationRect( width+10, 0.0f, 1.4f* width, 1.4f* height); //绘制源图 graphics.DrawImage(&image, 0, 0); //在目标区域内输出位图 graphics.DrawImage( &image, destinationRect, 0.f, 0.f, // 原图左上角 0.65f*width, // 仅显示原图宽度的65%部分 0.65f*height, // 仅显示原图高度的65%部分 UnitPixel); } void UsingInterpolationMode_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //装入图片 Bitmap image(L"eagle.bmp"); int width = image.GetWidth(); int height = image.GetHeight(); //绘制源图 graphics.DrawImage( &image, Rect(0, 0, width, height), //目标区域 0, 0, //源图左上角坐标 width, //源图宽度 height, //源图高 UnitPixel); //绘图平面右移 graphics.TranslateTransform( width+10,0); //最临近插值法(低质量) graphics.SetInterpolationMode(InterpolationModeNearestNeighbor); graphics.DrawImage( &image, RectF(0.0f, 0.0f, 0.6f*width, 0.6f*height), //目标区域 0, 0, //源图左上角坐标 width, //源图宽度 height, //源图高 UnitPixel); //绘图平面右移 graphics.TranslateTransform( 0.6f*width+10,0); // 高质量双线性插值法 graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear); graphics.DrawImage( &image, RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域 0, 0, //源图左上角坐标 width, //源图宽度 height, //源图高 UnitPixel); //绘图平面右移 graphics.TranslateTransform(width*0.6f+10, 0.f); // 高质量双三次插值法 graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); graphics.DrawImage( &image, RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域 0, 0, //源图左上角坐标 width, //源图宽度 height, //源图高 UnitPixel); } void RotateFlip_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图片 Bitmap photo(L"nemo2.bmp"); //得到图片尺寸 int iWidth = photo.GetWidth(); int iHeight = photo.GetHeight(); //绘制原始图片 graphics.DrawImage(&photo, 10+photo.GetWidth()+2, 10, photo.GetWidth(), photo.GetHeight()); //水平翻转图片 photo.RotateFlip(RotateNoneFlipX); //旋转后的图片 graphics.DrawImage(&photo, 10, 10, photo.GetWidth(), photo.GetHeight()); } void ImageSkewing_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //定义图形的目标显示区域 Point destination[]= { Point(200, 20), //原始图像左上角映射后的坐标 Point(110, 100), //原始图像右上角映射后的坐标 Point(250, 30) //原始图像左下角映射后的坐标 }; Bitmap image(L"Stripes.bmp"); // 绘制原始图像 graphics.DrawImage(&image, 0, 0); // 绘制基于平行四边形映射后的图像 graphics.TranslateTransform(image.GetWidth(),0); graphics.DrawImage(&image, destination, 3); } void Cubeimage_Click() { int WIDTH=200; int LEFT=200; int TOP=200; Graphics &graphics=GetGraphics(); //使用蓝色做背景色清屏 graphics.Clear(Color::Blue); //设置插值模式:高质量双三次插值法 graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); //分别装入张贴在立方体三面的图片 Bitmap face(L"rose.bmp"); Bitmap top(L"flower.bmp"); Bitmap right(L"yujinxiang.bmp"); //重新定义用于张贴在正面的图片坐标 Point destinationFace[] = { Point(LEFT,TOP), Point(LEFT+WIDTH, TOP), Point(LEFT, TOP+WIDTH) }; //张贴正面图像 graphics.DrawImage(&face,destinationFace,3); //重新定义用于张贴在顶部的图片坐标 PointF destinationTop[]= { PointF(LEFT+WIDTH/2, TOP-WIDTH/2), PointF(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2), PointF(LEFT, TOP) }; //张贴顶部面图像 graphics.DrawImage(&top, destinationTop, 3); //重新定义用于张贴在右侧的图片坐标 Point destinationRight[]= { Point(LEFT+WIDTH, TOP), Point(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2), Point(LEFT+WIDTH,TOP+WIDTH) }; //张贴右侧面图像 graphics.DrawImage(&right, destinationRight, 3); } void ThumbnailImage_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //设置插值模式:高质量双三次插值法 graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); //加载欲查看缩略图的图片 Bitmap image(L"flower.bmp"); //获取当前窗口大小 Rect client(0,0, MyClient.Width,MyClient.Height); float width=image.GetWidth(); float height=image.GetHeight(); //获取指定大小的缩略图 Image *pThumbnail = image.GetThumbnailImage(40,40, NULL, NULL); //将缩略图作为画刷 TextureBrush picBrush(pThumbnail); //填充窗口 graphics.FillEllipse(&picBrush,client); } void Clone_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Bitmap image(L"head.bmp"); int Height=image.GetHeight(); int Width=image.GetWidth(); //定义将图片切分成四个部分的区域 RectF block[4]= { RectF(0,0,Width/2,Height/2), RectF(Width/2,0,Width/2,Height/2), RectF(0,Height/2,Width/2,Height/2), RectF(Width/2,Height/2,Width/2,Height/2) }; //分别克隆图片的四个部分 Bitmap *s[4]; s[0]=image.Clone(block[0],PixelFormatDontCare); s[1]=image.Clone(block[1],PixelFormatDontCare); s[2]=image.Clone(block[2],PixelFormatDontCare); s[3]=image.Clone(block[3],PixelFormatDontCare); //绘制图片的四个部分,各部分绘制时间间隔为1秒 graphics.DrawImage(s[0],0,0); //延时,以达到分块显示的效果 ::Sleep(1000); graphics.DrawImage(s[1],Width/2,0); ::Sleep(1000); graphics.DrawImage(s[3],Width/2,Height/2); ::Sleep(1000); graphics.DrawImage(s[2],0,Height/2); delete s[0]; delete s[1]; delete s[2]; delete s[3]; } void Picturescale_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //装入图片 Bitmap image(L"photo.bmp"); //定义图片的显示区域 Rect rect(0,0,image.GetWidth(),image.GetHeight()); graphics.DrawImage(&image,rect); //局部缩小的区域大小为80*80 graphics.TranslateTransform(image.GetWidth()+10,0); Rect smallrect(0,0,80,80); //局部缩小 graphics.DrawImage(&image,smallrect,80,10,106,112,UnitPixel); graphics.TranslateTransform(0,100); //局部放大的区域大小为80*80 Rect largerect(0,0,80,80); //绘制放大后的局部图像 graphics.DrawImage(&image,largerect,56,101,35,40,UnitPixel); } void ImageAttributesSetNoOp_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Bitmap image(L"ColorTable.bmp"); int width=image.GetWidth(); //绘制标准图片 graphics.DrawImage(&image,0,0); graphics.TranslateTransform(image.GetWidth()+10,0); ImageAttributes imAtt; //构造一个红色转换到绿色的变换矩阵 ColorMatrix brushMatrix= { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //设置色彩校正 imAtt.SetColorMatrix( &brushMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); //使用色彩校正绘制图片 graphics.DrawImage( &image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置 0, 0, image.GetWidth(), image.GetHeight(), //源位置 UnitPixel, &imAtt); //临时关闭色彩校正 imAtt.SetNoOp(ColorAdjustTypeBitmap); //不使用色彩校正绘制图片红色->红色 graphics.TranslateTransform(width+10,0); graphics.DrawImage( &image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置 0, 0, image.GetWidth(), image.GetHeight(), //源位置 UnitPixel, &imAtt); //撤消对色彩校正的关闭 imAtt.ClearNoOp(ColorAdjustTypeBitmap); //使用色彩校正绘制图片:红色->绿色 graphics.TranslateTransform(width+10,0); graphics.DrawImage( &image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置 0, 0, image.GetWidth(), image.GetHeight(), //源位置 UnitPixel, &imAtt); } void CreateMetaFile() { Graphics &metagraph=GetGraphics(); //新建一个图元文件 HDC hdc =metagraph.GetHDC(); Metafile metaFile1(CWideCharString("ddd.emf"), hdc); //使用Metafile对象的地址做为绘图平面 Graphics *graphics=Graphics::FromImage(&metaFile1); graphics->ScaleTransform(0.8f,0.8f); //在沿水平方向输出三个椭圆并填充 graphics->SetSmoothingMode(SmoothingModeHighQuality); //红色椭圆 graphics->DrawEllipse(&Pen(Color.Red,10), Rect(0, 0, 75, 95)); graphics->FillEllipse(&SolidBrush(Color::Red), Rect(0, 0, 75, 95)); //绿色椭圆 graphics->DrawEllipse(&Pen(Color::Green,10), Rect(40, 0, 75, 95)); graphics->FillEllipse(&SolidBrush(Color::Green), Rect(40, 0, 75, 95)); //蓝色椭圆 graphics->DrawEllipse(&Pen(Color.Blue,10), Rect(80, 0, 75, 95)); graphics->FillEllipse(&SolidBrush(Color::Blue), Rect(80, 0, 75, 95)); // 追加三种色彩的文本 FontFamily fontFamily(CWideCharString("Arial")); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); graphics->SetTextRenderingHint(TextRenderingHintAntiAlias); //红色文字 MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Red), PointF(-80.0f, 0.0f)); //绿色文字 MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Green), PointF(-80.0f, font.GetHeight(graphics))); //蓝色文字 MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Blue), PointF(-80.0f, font.GetHeight(graphics)*2)); //释放所有资源。 //graphics->Dispose(); //metaFile1.Dispose(); metagraph.ReleaseHDC(hdc); //metagraph.Dispose(); delete graphics; } void SetColorMatrices_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图元文件 Metafile image(L"ddd.emf"); Unit unit=UnitPixel; //获取图片区间 RectF rect; image.GetBounds(&rect, &unit); //不使用任何色彩校正输出图片 graphics.DrawImage(&image, 0.0f, 0.0f, rect.Width, rect.Height); ImageAttributes imAtt; //定义一个使红色分量递增1.5的矩阵 ColorMatrix defaultColorMatrix= { 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //定义一个使绿色分量递增1.5的矩阵 ColorMatrix defaultGrayMatrix= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //画笔的彩色色彩信息较校正矩阵:蓝色分量递增1.5的矩阵 ColorMatrix penColorMatrix= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; //画笔的灰度色矩阵:所有分量递增1.5的矩阵 ColorMatrix penGrayMatrix= { 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; // 设置默认的彩色及灰度校正矩阵. //ColorAdjustTypeDefault:修改所有的色彩信息 imAtt.SetColorMatrices( &defaultColorMatrix, &defaultGrayMatrix, ColorMatrixFlagsAltGray, ColorAdjustTypeDefault); //使用校正矩阵绘制图元文件:校正所有的色彩 graphics.TranslateTransform(image.GetWidth()+10,0); graphics.DrawImage( &image, Rect(0, 0, (int)rect.Width,(int)rect.Height), rect.X, rect.Y, rect.Width, rect.Height, UnitPixel, &imAtt); //设置画笔的彩色及灰度色彩校正矩阵 //ColorAdjustTypePen:修正画笔色彩 imAtt.SetColorMatrices( &penColorMatrix, &penGrayMatrix, ColorMatrixFlagsAltGray, ColorAdjustTypePen); //在第二行绘制 graphics.ResetTransform(); graphics.TranslateTransform(0,image.GetHeight()); //使用修正后的画笔绘制图片 graphics.DrawImage( &image, Rect(0, 0, (int)rect.Width,(int)rect.Height), rect.X, rect.Y, rect.Width, rect.Height, UnitPixel, &imAtt); graphics.TranslateTransform(image.GetWidth()+10,0); //清除在画笔上的所有变换 imAtt.ClearColorMatrix(ColorAdjustTypePen); graphics.DrawImage( &image, Rect(0, 0, (int)rect.Width,(int)rect.Height), rect.X, rect.Y, rect.Width, rect.Height, UnitPixel, &imAtt); } void SetOutputChannelColorProfile_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Bitmap image(L"car.bmp"); ImageAttributes imAtt; int width = image.GetWidth(); int height = image.GetHeight(); Rect rect(0, 0, width, height); //绘制原始图片 graphics.DrawImage(&image, rect); graphics.TranslateTransform(width,0); //设置色彩配置文件 imAtt.SetOutputChannelColorProfile( CWideCharString("kodak_dc.ICM"), ColorAdjustTypeBitmap); //使用色彩配置文件输出图片 graphics.DrawImage( &image, rect, 0, 0, width, height, UnitPixel, &imAtt); } void Gammaadjust_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //装入原始图片 Bitmap image(L"warrior.bmp"); int width = image.GetWidth(); int height = image.GetHeight(); // 设置字体信息 Font myFont(L"宋体", 12); //设置提示信息的显示区域 PointF origin(width+10, height+20); SolidBrush blackBrush(Color::Black); ImageAttributes imAtt; CString msg; //从0-3依次调整输出图片时所使用的Gamma值 for(float i=0.0f;i<3.0f;i+=0.1f) { //绘制原始图片 graphics.DrawImage(&image,0,0); //设置Gamma值 imAtt.SetGamma(i,ColorAdjustTypeBitmap); //使用修改后Gamma值进行图片输出 graphics.DrawImage( &image, Rect(width+10, 0, width, height), //目标区域 0, 0, width, height, //源区域 UnitPixel, &imAtt); msg.Format("正在修改Gamma值,Gamma=%.2f",i); //显示当前的Gamma值信息 MyDrawString(graphics, msg,myFont,blackBrush,origin); //延时一秒以便观看效果 ::Sleep(100); graphics.Clear(Color::White); imAtt.ClearGamma(); } } void SetOutputChannel_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //装入图片 Bitmap image(L"jieba.bmp"); //图片的高度 int width = image.GetWidth(); int height = image.GetHeight(); //绘制原始图片 graphics.DrawImage(&image, RectF(0, 0, width, height)); ImageAttributes imAtt; //设置色彩输出通道cyan imAtt.SetOutputChannel(ColorChannelFlagsC, ColorAdjustTypeBitmap); //右移,绘制图片 graphics.TranslateTransform(width, 0); graphics.DrawImage( &image, Rect(0, 0, width, height), 0, 0, width, height, UnitPixel, &imAtt); //设置色彩输出通道:magenta imAtt.SetOutputChannel(ColorChannelFlagsM, ColorAdjustTypeBitmap); //右移,绘制图片 graphics.TranslateTransform(width, 0); graphics.DrawImage( &image, Rect(0, 0, width, height), 0, 0, width, height, UnitPixel, &imAtt); //设置色彩输出通道:yellow imAtt.SetOutputChannel(ColorChannelFlagsY, ColorAdjustTypeBitmap); //右移,绘制图片 graphics.TranslateTransform(width, 0); graphics.DrawImage( &image, Rect(0, 0, width, height), 0, 0, width, height, UnitPixel, &imAtt); //设置色彩输出通道:black imAtt.SetOutputChannel(ColorChannelFlagsK, ColorAdjustTypeBitmap); //右移,绘制图片 graphics.TranslateTransform(width, 0); graphics.DrawImage( &image, Rect(0, 0, width, height), 0, 0, width, height, UnitPixel, &imAtt); } void Colorkey_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //装入前后背景图片 Bitmap forground(L"grid.bmp"); Bitmap background(L"snike.bmp"); int width=background.GetWidth(); int height=background.GetHeight(); Rect rect(0,0,width,height); //将红色设置成关键色 ImageAttributes imAtt; imAtt.SetColorKey( Color::Red, Color::Red, ColorAdjustTypeBitmap); //绘制背景 graphics.DrawImage(&background,0,0); //绘制前景 graphics.DrawImage( &forground, rect, 0, 0, forground.GetWidth(), forground.GetHeight(), UnitPixel, &imAtt); graphics.TranslateTransform(width+20,0); graphics.DrawImage(&background,0,0); //清除已经应用的关键色信息 imAtt.ClearColorKey(ColorAdjustTypeBitmap); ///将蓝色设置成关键色 imAtt.SetColorKey( Color::Blue, Color::Blue, ColorAdjustTypeBitmap); graphics.DrawImage(&forground, rect, 0, 0, forground.GetWidth(), forground.GetHeight(), UnitPixel, &imAtt); graphics.TranslateTransform(width+20,0); //绘制源图 graphics.DrawImage(&background,0,0); } void Setthreshold_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Bitmap image(L"box-2.bmp"); int Width=image.GetWidth(); int Height=image.GetHeight(); //绘制原始图片 graphics.DrawImage(&image, 10, 10, Width, Height); //将阈值从0到1依次运用 ImageAttributes imAtt; for(float i=0.0f;i<1.0f;i+=0.1f) { //设置输出图片时使用的阈值 imAtt.SetThreshold(i, ColorAdjustTypeBitmap); //绘制已经使用了阈值的图片 graphics.DrawImage(&image, Rect(10+Width, 10, Width, Height), 0, 0, Width, Height, UnitPixel, &imAtt); //延时 ::Sleep(1000); } } void AdjustedPalette_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); graphics.ScaleTransform(0.7f,0.7f); //加载图片 Bitmap image(L"lord-256.bmp"); //复制图片 Bitmap image2(L"lord-256.bmp"); //获取图片使用的调色板信息 int size=image.GetPaletteSize(); ColorPalette *palette= (ColorPalette *)new BYTE[size]; image.GetPalette(palette, size); //获取调色板所包含的色彩总数 int count=palette->Count; if(count<1) { AfxMessageBox("图片无调色板信息可用"); return; } //更改调色板中的每一种色彩信息 for(int i=0;iEntries[i]); int r=u.GetR()/2; int g=u.GetG()/2; int b=u.GetB()/2; if(r<1) r=0; if(g<1) g=0; if(b<1) b=0; palette->Entries[i]=u.MakeARGB(255, r,g,b); } //设置图像的新调色板 image.SetPalette(palette); //绘制原图 graphics.DrawImage(&image2,0,0); //绘制修改后的图片 graphics.DrawImage(&image,image.GetWidth()+10,0); delete palette; } void SetWrapMode_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); //加载图片 Bitmap image(L"yueru.bmp"); ImageAttributes imAtt; //设置图片排列方式为WrapModeClamp:图片不进行平铺 imAtt.SetWrapMode(WrapModeClamp,Color::Red); //缩小显示源图 graphics.DrawImage(&image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域 UnitPixel, &imAtt); graphics.TranslateTransform(image.GetWidth()+10,0); //设置图片排列方式为WrapModeTileFlipXY:图片在水平和垂直方向上同时翻转 imAtt.SetWrapMode(WrapModeTileFlipXY); graphics.DrawImage(&image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域 UnitPixel, &imAtt); graphics.TranslateTransform(image.GetWidth()+10,0); //设置图片排列方式为WrapModeTileFlipX:图片在水平上翻转 imAtt.SetWrapMode(WrapModeTileFlipX); graphics.DrawImage(&image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域 UnitPixel, &imAtt); graphics.TranslateTransform(image.GetWidth()+10,0); //设置图片排列方式为WrapModeTileFlipY:图片在垂直上翻转 imAtt.SetWrapMode(WrapModeTileFlipY); graphics.DrawImage(&image, Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域 UnitPixel, &imAtt); } void ListAllImageEncoders_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); SolidBrush brush(Color::Blue); FontFamily fontFamily(L"华文楷体"); Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel); //获取编码器信息 UINT num, size; GetImageEncodersSize(&num, &size); ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*)new BYTE[size]; GetImageEncoders(num, size, pImageCodecInfo); //输出编码信息 CString msg; for(UINT j = 0; j Count; msg.Format("在编码参数数组中有%d个EncoderParameter类\n每个类的详细信息为:\n", count); EncoderParameter *pEncoderParameter=pEncoderParameters->Parameter; /*分别查看EncoderParameters对象中的 每一个EncoderParameter对象的成员变量 GUID、NumberOfValues、Type*/ for(int i=0;iCount; outmsg.Format("在EncoderParameters中,有%d个 EncoderParameter对象。\n", count); EncoderParameter *pEncoderParameter=encodersarameters->Parameter; // 查看每一个EncoderParameter对象信息 for(int k = 0; k value)); AfxMessageBox(msg); delete pp; } void OnCanvas_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); graphics.ScaleTransform(0.7f,0.7f); Bitmap image(L"box-2.bmp"); int Width = image.GetWidth(); int Height = image.GetHeight(); Color color; graphics.DrawImage(&image, Rect(0, 0, Width, Height)); //产生随机数序列 for(int i=0;i=128) tmp=255; else tmp=0; colorTemp = Color(255,tmp,tmp,tmp); //将计算后的RGB值回写到位图 image.SetPixel(i, j, colorTemp); } //动态绘制滤镜效果图 graphics.DrawImage(&image, Rect(Width, 0, Width, Height)); } } //计算两点A、B之间的绝对距离 float fDistance(Point A,Point B) { double i=((A.X-B.X)*(A.X-B.X))+((A.Y-B.Y)*(A.Y-B.Y)); return (float)sqrt(i); } void Flashligt_Click() { Graphics &graphics=GetGraphics(); graphics.Clear(Color::White); Bitmap image(L"sports.bmp"); int Width = image.GetWidth(); int Height = image.GetHeight(); int A=Width/2; int B=Height/2; //Center:图片中心点,发亮此值会让强光中心发生偏移 Point Center= Point(A,B); //R:强光照射面的半径,即”光晕” int R=100; Color colorTemp,color2; Color color; graphics.DrawImage(&image, Rect(0, 0, Width, Height)); //依次访问每个像素 for(int x=0;xGetPixel(i, j, &colornow); image2->GetPixel(i-1, j-1, &colorLeft); float r=colornow.GetR()+(colornow.GetR() -colorLeft.GetR()*dep); r=min(255,max(0,r)); float g=colornow.GetG()+(colornow.GetG() -colorLeft.GetG()*dep); g=min(255,max(0,g)); float b=colornow.GetB()+(colornow.GetB()-colorLeft.GetB()*dep); b=min(255,max(0,b)); colorTemp = Color(255,(int)r,(int)g,(int)b); //将计算后的RGB值回写到位图 image2->SetPixel(i, j, colorTemp); } graphics.DrawImage(image2, Rect(Width*2, 0, Width, Height)); } delete image2; } void ShowAllEncoderParameters(ImageCodecInfo*, CString &msg); HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars); HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars); void ShowAllEncoder() { UINT num; // Number of image encoders UINT size; // Size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo; // How many encoders are there? // How big (in bytes) is the array of all ImageCodecInfo obects? GetImageEncodersSize(&num, &size); // Create a buffer large enough to hold the array of ImageCodecInfo // objects that will be returned by GetImageEncoders. pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); // GetImageEncoders creates an array of ImageCodecInfo objects // and copies that array into a previously allocated buffer. // The third argument, imageCodecInfos, is a pointer to that buffer. GetImageEncoders(num, size, pImageCodecInfo); // For each ImageCodecInfo object in the array, show all parameters. for(UINT j = 0; j < num; ++j) { CString msg; ShowAllEncoderParameters(&(pImageCodecInfo[j]), msg); AfxMessageBox(msg); } free(pImageCodecInfo); } VOID ShowAllEncoderParameters(ImageCodecInfo* pImageCodecInfo, CString &msg) { CONST MAX_CATEGORY_LENGTH = 50; CONST MAX_VALUE_TYPE_LENGTH = 50; WCHAR strParameterCategory[MAX_CATEGORY_LENGTH] = L""; WCHAR strValueType[MAX_VALUE_TYPE_LENGTH] = L""; CString s(pImageCodecInfo->MimeType); s+="\n"; msg+=s; // Create a Bitmap (inherited from Image) object so that we can call // GetParameterListSize and GetParameterList. Bitmap bitmap(1, 1); // How big (in bytes) is the encoder's parameter list? UINT listSize = 0; listSize = bitmap.GetEncoderParameterListSize(&pImageCodecInfo->Clsid); s.Format(" The parameter list requires %d bytes.\n", listSize); msg+=s; if(listSize == 0) return; // Allocate a buffer large enough to hold the parameter list. EncoderParameters* pEncoderParameters = NULL; pEncoderParameters = (EncoderParameters*)malloc(listSize); if(pEncoderParameters == NULL) return; // Get the parameter list for the encoder. bitmap.GetEncoderParameterList( &pImageCodecInfo->Clsid, listSize, pEncoderParameters); // pEncoderParameters points to an EncoderParameters object, which // has a Count member and an array of EncoderParameter objects. // How many EncoderParameter objects are in the array? s.Format(" There are %d EncoderParameter objects in the array.\n", pEncoderParameters->Count); msg+=s; // For each EncoderParameter object in the array, list the // parameter category, data type, and number of values. for(UINT k = 0; k < pEncoderParameters->Count; ++k) { EncoderParameterCategoryFromGUID( pEncoderParameters->Parameter[k].Guid, strParameterCategory, MAX_CATEGORY_LENGTH); ValueTypeFromULONG( pEncoderParameters->Parameter[k].Type, strValueType, MAX_VALUE_TYPE_LENGTH); s.Format(" Parameter[%d]\n", k); msg+=s; s.Format(" The category is %s.\n", CString(strParameterCategory)); msg+=s; s.Format(" The data type is %s.\n", strValueType); msg+=s; s.Format(" The number of values is %d.\n", pEncoderParameters->Parameter[k].NumberOfValues); msg+=s; } // for free(pEncoderParameters); } // ShowAllEncoderParameters HRESULT MyStringCchCopy(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc) { return wcsncpy(pszDest, pszSrc, cchDest)? Ok: E_FAIL; } HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars) { HRESULT hr = E_FAIL; if(guid == EncoderCompression) hr = MyStringCchCopy(category, maxChars, L"Compression"); else if(guid == EncoderColorDepth) hr = MyStringCchCopy(category, maxChars, L"ColorDepth"); else if(guid == EncoderScanMethod) hr = MyStringCchCopy(category, maxChars, L"ScanMethod"); else if(guid == EncoderVersion) hr = MyStringCchCopy(category, maxChars, L"Version"); else if(guid == EncoderRenderMethod) hr = MyStringCchCopy(category, maxChars, L"RenderMethod"); else if(guid == EncoderQuality) hr = MyStringCchCopy(category, maxChars, L"Quality"); else if(guid == EncoderTransformation) hr = MyStringCchCopy(category, maxChars, L"Transformation"); else if(guid == EncoderLuminanceTable) hr = MyStringCchCopy(category, maxChars, L"LuminanceTable"); else if(guid == EncoderChrominanceTable) hr = MyStringCchCopy(category, maxChars, L"ChrominanceTable"); else if(guid == EncoderSaveFlag) hr = MyStringCchCopy(category, maxChars, L"SaveFlag"); else hr = MyStringCchCopy(category, maxChars, L"Unknown category"); return hr; } // EncoderParameterCategoryFromGUID HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars) { HRESULT hr = E_FAIL; WCHAR* valueTypes[] = { L"Nothing", // 0 L"ValueTypeByte", // 1 L"ValueTypeASCII", // 2 L"ValueTypeShort", // 3 L"ValueTypeLong", // 4 L"ValueTypeRational", // 5 L"ValueTypeLongRange", // 6 L"ValueTypeUndefined", // 7 L"ValueTypeRationalRange"}; // 8 hr = MyStringCchCopy(strValueType, maxChars, valueTypes[index]); return hr; } // ValueTypeFromULONG