本文共 2854 字,大约阅读时间需要 9 分钟。
关于GDI+ 的使用,就对点,线,面的画的操作,图像剪裁,缩放等等操作,了解各种常用的方法和属性。
常用命名空间:System.Drawing;System.Drawing.Image;System.Drawing.Drawing2D;
Graphics类封装了一个GDI+绘图图面,提供将对象绘制到显示到设备的方法。Graphics叫画板,只不过这个画板中带了很多工具。但画图时你要定义画板的大小,颜色等等,还应给他一张画纸;
Graphics
1.创建Graphics基本方法:
Graphics g = this.CreateGraphics();
Graphics g = e;// Paint事件中的
Graphics g = Graphics.FromImage();//Graphics.Fromxx类的各种静态方法。
谁创建Graphics对象,就在谁上画。
2.画的方法:
g.Drawxx 的各种方法。
3.Graphics用的 画笔和画刷
pen 和 Font pen.PenType //属性 pen.DashStyle Font f = new Font( "宋体", 15, FontStyle.Bold | FontStyle.Italic );Brush //画刷 派生类: LinearGradientBrush//渐变画刷 SolidBrush//单色画刷 HatchBrush //用阴影样式 (机械制图时用的多) TextureBrush//画字 ImageBrush//图片画刷 VisualBrush// RadialGradientBrush DrawingBrush
4.图片处理
1. Graphics.SmoothingMode //消除锯齿常用 2. Graphics.InterpolationMode //图像缩放常用 3. Graphics.CompositingQuality //
clear()方法:
Graphics g.clear(Color.Blue);// 不是清除xx颜色,是清除背景并设置xx颜色。
Bitmap ,image 和 Icon
Bitmap bmp = new Bitmap(16, 16); Bitmap bmp1 = Bitmap.FromHbitmap(bmp.GetHbitmap()); Image image = Image.FromFile(@"C:\\temp.jpg"); bmp.MakeTransparent(Color.FromArgb(255, 0, 255));//把xx颜色设置透明色 for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { if (bmp.GetPixel(i, j) == Color.Blue)//获取像素设置 { bmp.SetPixel(i, j, Color.Red); } } }
ImageAttributes imageAttr = new ImageAttributes();//通过位图和图元文件颜色的信息设置颜色
imageAttr.SetColorKey(lowerColor,upperColor, ColorAdjustType.Default); e.Graphics.DrawImage(Image, rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);Stream IconStream = System.IO.File.OpenWrite(fileName);
Bitmap bitmap = new Bitmap(pbImage.Image); bitmap.SetResolution(32, 32); Icon icon = System.Drawing.Icon.FromHandle(bitmap.GetHicon()); icon.Save(IconStream);//比bitmap.save格式强点其他常用:
Clipboard.SetDataObject(this.pbSource.Image);//截图 IDataObject data = Clipboard.GetDataObject(); if (data.GetDataPresent(DataFormats.Bitmap)) image = (Bitmap)data.GetData(DataFormats.Bitmap); Color c = KnownColor.Control; Color c =SystemColors.Control Color c = Color.FromArgb(128, Color.Blue); //128为半透明颜色 this.Opacity = 0.5//窗体的透明度 System.Drawing.Drawing2D 命名空间下GraphicsPath //创建矢量图 Bitmap bmp = new Bitmap(220,220); Graphics g = Graphics.FromImage(bmp); Metafile mf = new Metafile(filePath,g.GetHdc()); //画图... g.Save(); g.Dispose(); mf.Dispose();
防止图片闪烁,双缓冲设置
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);尽量不要用窗体TransparencyKey否则闪烁和卡顿会使用闪烁更严重。 不要在Paint事件给各种 xx.Image 赋值,xx.Image会调用paint这样会死循环。图像的各种效果(底片、浮雕、黑白、滤镜)只是算法问题。
转载地址:http://unupi.baihongyu.com/