using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace SolidBrush对象 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Graphics ghs = this.CreateGraphics();//创建Graphics对象 Brush mybs = new SolidBrush(Color.Red); Rectangle rt = new Rectangle(10, 10, 20, 20); ghs.FillRectangle(mybs, rt); } private void button2_Click(object sender, EventArgs e) { Graphics ghs = this.CreateGraphics(); for(int i = 0; i < 6; i++) { HatchStyle hs = (HatchStyle)(2 + i); HatchBrush hb = new HatchBrush(hs, Color.Blue); Rectangle rtl = new Rectangle(10, 50 * i, 50 * i, 50); ghs.FillRectangle(hb, rtl); } } private void button3_Click(object sender, EventArgs e) { Point p1 = new Point(100, 100); Point p2 = new Point(200, 200); Graphics ghs = this.CreateGraphics(); LinearGradientBrush lgb = new LinearGradientBrush(p1, p2, Color.Black, Color.White); lgb.WrapMode = WrapMode.TileFlipX; ghs.FillRectangle(lgb, 100, 100, 200, 200); } private void button4_Click(object sender, EventArgs e) { //横向直线 Pen blackPen = new Pen(Color.Black, 3); Point p1 = new Point(10, 50); Point p2 = new Point(100, 50); Graphics ghs = this.CreateGraphics(); ghs.DrawLine(blackPen, p1, p2); } private void button5_Click(object sender, EventArgs e) { Pen blackPen = new Pen(Color.Black, 3); Point p1 = new Point(10, 10); Point p2 = new Point(10, 100); Graphics ghs = this.CreateGraphics(); ghs.DrawLine(blackPen, p1, p2); } private void button6_Click(object sender, EventArgs e) { Graphics graphics = this.CreateGraphics(); Pen myPen = new Pen(Color.Black, 3); graphics.DrawEllipse(myPen, 100, 100, 50, 100); } private void button7_Click(object sender, EventArgs e) { Graphics ghs = this.CreateGraphics(); Pen mypen = new Pen(Color.Black, 3); Rectangle myRectangle = new Rectangle(70, 20, 100, 60); ghs.DrawArc(mypen, myRectangle, 30, 120); } private void button8_Click(object sender, EventArgs e) { Graphics ghs = this.CreateGraphics(); Pen mypen = new Pen(Color.Black, 3); ghs.DrawPie(mypen, 50, 50, 120, 100, 210, 120); } private void button9_Click(object sender, EventArgs e) { Graphics ghs = this.CreateGraphics(); Pen mypen = new Pen(Color.Black, 3); Point p1 = new Point(80, 20); Point p2 = new Point(30, 40); Point p3 = new Point(160, 100); Point[] mypoints = { p1, p2, p3 }; ghs.DrawPolygon(mypen, mypoints); } private void button10_Click(object sender, EventArgs e) { string str = "C#和肥猫"; Font myFont = new Font("华文行楷",16); SolidBrush myBrush = new SolidBrush(Color.Black); Graphics myGraphics = this.CreateGraphics(); myGraphics.DrawString(str,myFont,myBrush,20,20); } private void button11_Click(object sender, EventArgs e) { //绘制图形 Image myImage = Image.FromFile("1.png"); Graphics myGraphics = this.CreateGraphics(); myGraphics.DrawImage(myImage, 50, 20); } private void button12_Click(object sender, EventArgs e) { //绘制公章 int diameter = 0;//圆直径 int brushWidth = 4; diameter = panel1.Width > panel1.Height ? panel1.Height : panel1.Width; Font Var_Font = new Font("Arial", 12, FontStyle.Bold);//字符串样式 Font star_Font = new Font("Arial", 30, FontStyle.Regular);//星号样式 string star_Str = "★"; Rectangle rect = new Rectangle(10, 10, 160, 160); rect = new Rectangle(brushWidth, brushWidth, diameter - brushWidth * 2, diameter - brushWidth * 2); Graphics g = this.panel1.CreateGraphics(); g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿 g.Clear(Color.White);//设置Panel1控件的背景色 Pen myPen = new Pen(Color.Red, brushWidth); g.DrawEllipse(myPen, rect); SizeF var_Size = new SizeF(rect.Width, rect.Width); //画星 var_Size = g.MeasureString(star_Str, star_Font); g.DrawString(star_Str, star_Font, myPen.Brush, new PointF((rect.Width / 2F) + brushWidth - var_Size.Width / 2F, rect.Height / 2F - var_Size.Width / 2F)); //画章 var_Size = g.MeasureString("专用章", Var_Font); g.DrawString("专用章", Var_Font, myPen.Brush, new PointF((rect.Width / 2F) + brushWidth - var_Size.Width / 2F, rect.Height / 2F + var_Size.Height * 2)); string tempStr = "大猫科技有限公司"; int len = tempStr.Length; float angle = 180 + (180 - len * 20) / 2; for (int i = 0; i < len; i++) { g.TranslateTransform((diameter + brushWidth / 2) / 2, (diameter + brushWidth / 2) / 2); g.RotateTransform(angle); Brush myBrush = Brushes.Red; g.DrawString(tempStr.Substring(i, 1), Var_Font, myBrush, 60, 0); g.ResetTransform(); angle += 20; } } private void button13_Click(object sender, EventArgs e) { //绘制曲线 Graphics ghs = this.CreateGraphics(); Pen myPen = new Pen(Color.Black, 1); int beginX = 50; int beginY = 50; int height = 35; int width = 50; Point pointX1 = new Point(beginX, beginY); Point pointY1 = new Point(beginX + 210, beginY); Point pointX2 = new Point(beginX, beginY - 45); Point pointY2 = new Point(beginX, beginY + 45); ghs.DrawLine(myPen, pointX1, pointY1); ghs.DrawLine(myPen, pointX2, pointY2); //贝塞尔线条 ghs.DrawBezier(myPen, beginX, beginY, beginX + 15, beginY - height, beginX + 40, beginY - height, beginX + width,beginY); } //缩放图片 public bool ThumbnailCallback() { return true; } public bool GetReducedImage(double Percent,string filePath) { try { Graphics ghs = this.CreateGraphics(); ghs.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿 ghs.Clear(Color.White);//设置Panel1控件的背景色 Bitmap bt = new Bitmap(200, 200); Graphics g = Graphics.FromImage(bt); g.Clear(Color.White); Image reduceImage; Image resourceImage = new Bitmap(@"1.jpg"); Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); int ImageWidth = Convert.ToInt32(resourceImage.Width * Percent); int ImageHeight = Convert.ToInt32(resourceImage.Height * Percent); g.DrawImage(resourceImage, 0, 0, ImageWidth, ImageHeight); bt.Save(@"4.jpg", ImageFormat.Jpeg); reduceImage = resourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); if(ImageWidth >ImageHeight) { g.DrawImage(reduceImage, 0, (int)(200 - ImageHeight) / 2, ImageWidth, ImageHeight); } else { g.DrawImage(reduceImage, 0, (int)(200 - ImageWidth) / 2, ImageWidth, ImageHeight); } g.DrawRectangle(new Pen(Color.Gray), 0, 0, 199, 199); bt.Save(@"3.jpg", ImageFormat.Jpeg); bt.Dispose(); reduceImage.Dispose(); return true; } catch(Exception e) { MessageBox.Show(e.Message); return false; } } private void button14_Click(object sender, EventArgs e) { GetReducedImage(1, "1.jpg"); } private void Form1_Load(object sender, EventArgs e) { } //浮雕显示 private void button16_Click(object sender, EventArgs e) { try { Image resourceImage = new Bitmap(@"1.jpg"); Bitmap mybp = new Bitmap(resourceImage); for(int i = 0; i < mybp.Width -1;i++) { for(int j = 0; j < mybp.Height -1;j++) { Color Color1 = mybp.GetPixel(i, j); Color Color2 = mybp.GetPixel(i + 1, j + 1); int red = Math.Abs(Color1.R - Color2.R + 128); int green = Math.Abs(Color1.G - Color2.G + 128); int blue = Math.Abs(Color1.B - Color2.B+128); if (red > 225) red = 225; if (red < 0) red = 0; if (green > 225) green = 225; if (green < 0) green = 0; if (blue > 225) blue = 225; if (blue < 0) blue = 0; mybp.SetPixel(i, j, Color.FromArgb(red, green, blue)); } } this.BackgroundImage=mybp; } catch { } } //旋转图像 private void button15_Click(object sender, EventArgs e) { Bitmap mybp = new Bitmap(200, 200); Image resourceImage = new Bitmap(@"1.jpg"); Graphics g = this.panel1.CreateGraphics(); float angle = 0; while(angle < 360) { TextureBrush mybrush = new TextureBrush(resourceImage); this.panel1.Refresh();//使工作区无效 mybrush.RotateTransform(angle);//已制定角度旋转图像 g.FillRectangle(mybrush, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);//绘制旋转后图像 angle += 10f; System.Threading.Thread.Sleep(50);//使线程休眠50毫秒 } } } }