C# 关于画图Graphics Bitmap image

来源:互联网 发布:淘宝卖家中心手机版 编辑:程序博客网 时间:2024/05/09 21:20

关于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这样会死循环。图像的各种效果(底片、浮雕、黑白、滤镜)只是算法问题。
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 造价师资格证书丢了怎么办 行测做题速度太慢怎么办 普通话准考证丢了怎么办 信用卡还款逾期了怎么办 淮北建洗煤厂需要怎么办 建设工程不结算怎么办 考监理没有职称怎么办 公开招标两次招标失败怎么办 山东省植保员证怎么办 工伤没开病假单怎么办 邮储银行怎么办金卡 农机被交警查怎么办 广联达识别板筋负筋重叠怎么办 掌上品的钱怎么办 财务纠纷被起诉怎么办 牙齿补后疼痛怎么办 牙齿表面蛀了怎么办 虫牙全掉了怎么办 牙齿蛀光了怎么办 虫牙全部掉完了怎么办 不喜欢向人请教怎么办 单位不交公积金怎么办 电锯链条掉了怎么办 天津公积金怎么办外地转入 学籍档案涂抹了怎么办 气相点火失败怎么办 小米闹钟声音小怎么办 河北省监理员证怎么办 买的商铺烂尾了怎么办 钢表带被磨花了怎么办 资料员到期了怎么办 八大员挂靠社保怎么办 安许证三类人员不足了怎么办 考试准考证号写错了怎么办 科目一失约两次怎么办 钢筋送检两次不合格怎么办 公路原材料抽检不合格怎么办 混凝土回弹强度不合格怎么办 毕业证照片太丑怎么办 政审时找不到档案怎么办 劳动解除书开不出来怎么办