GDI+ 绘图总结

今日GDI+绘图用到的比较多,这里就总结下自己用到的GDI+的一些属性和方法。

//定义纸张
Bitmap bm = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//将画布定义为2D的
g.Clear(Color.White);//将画布底色定义为白色

//定义Text位置
StringFormat right = new StringFormat();
right.Alignment = StringAlignment.Far;
right.LineAlignment = StringAlignment.Center;

//字体大小
Font font = new Font("宋体", 9);
//线条
Pen thickpen = new Pen(Color.Black, 2f);

//背景色
Brush bs1 = new SolidBrush(Color.FromArgb(238, 238, 238));

//外边框
g.DrawRectangle(thickpen, new Rectangle(x,y, Width, Height));

//画线
g.DrawLine(Pen, x1, y1, x2, y2);

//填写文字
g.DrawString(str,Font,Brushes,new RectangleF(x,y,width,height),StringFormat)

//插入图片
Image image = Image.FromFile(@"C:\work\ClassLibrary1\Web\images\ControlImg\checked.jpg");
g.DrawImage(image, x,y ,ImageWidth , ImageHeight);
g.DrawString(Str, Font , Brushes, new RectangleF(x, y, width, height), left);

//控件图片做为背景
TextureBrush checkImg = new TextureBrush(Image.FromFile(@"C:\work\ClassLibrary1\Web\images\ControlImg\checked.jpg"));
checkImg.TranslateTransform(x,y);//要先将图片偏移,然后再移回来
g.FillRectangle(checkImg, new Rectangle(x,y,width,height));

原文地址:https://www.cnblogs.com/lx1988729/p/2606874.html