博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 关于画图Graphics Bitmap image
阅读量:4116 次
发布时间:2019-05-25

本文共 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  //
  1. 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/

你可能感兴趣的文章
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>
驱动力3.0,动力全开~
查看>>