C#: GDI+使用示例

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace GDIPlus { public partial class Form1 : Form { public Form1() { InitializeComponent(); SetStyle(ControlStyles.Opaque, true); Bounds = new Rectangle(0, 0, 500, 300); } private void OnPaint(object sender, PaintEventArgs e) { //path demo //GraphicsPath path; //path = new GraphicsPath(new Point[]{ // new Point(10,10), // new Point(150,10), // new Point(200,150), // new Point(10,150), // new Point(200,160) // }, // new byte[] // { // (byte)PathPointType.Start, // (byte)PathPointType.Line, // (byte)PathPointType.Line, // (byte)PathPointType.Line, // (byte)PathPointType.Line // } //); //e.Graphics.DrawPath(Pens.Black, path); //region demo //Rectangle r1 = new Rectangle(10, 10, 50, 50); //Rectangle r2 = new Rectangle(40, 40, 50, 50); //Region r = new Region(r1); //r.Union(r2); //GraphicsPath path = new GraphicsPath(new Point[]{ // new Point(45,45), // new Point(145,55), // new Point(200,150), // new Point(75,150), // new Point(45,45) // }, // new byte[] // { // (byte)PathPointType.Start, // (byte)PathPointType.Bezier, // (byte)PathPointType.Bezier, // (byte)PathPointType.Bezier, // (byte)PathPointType.Line // } //); //r.Union(path); //e.Graphics.FillRegion(Brushes.Blue, r); //Pen and using demo //Graphics g = e.Graphics; //using(Pen blackPen = new Pen(Color.Black, 1)) //{ // if (ClientRectangle.Height / 10 > 0) // { // for (int i = 0; i < ClientRectangle.Height;i += ClientRectangle.Height / 10) // { // g.DrawLine(blackPen, new Point(0, 0), new Point(ClientRectangle.Width, i)); // //or //g.DrawLine(Pens.Black, new Point(0, 0), new Point(ClientRectangle.Width, i)); // } // } //} //brush demo //Graphics g = e.Graphics; //g.FillRectangle(Brushes.White, ClientRectangle); //g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 50, 50)); //Brush linearGradientBrush = new LinearGradientBrush( // new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45 // ); //g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50)); //linearGradientBrush.Dispose(); //g.FillEllipse(Brushes.Aquamarine, new Rectangle(60, 20, 50, 30)); //g.FillPie(Brushes.Chartreuse, new Rectangle(60, 60, 50, 50), 90, 210); //g.FillPolygon(Brushes.BlueViolet, new Point[]{ // new Point(110, 10),new Point(150, 10), // new Point(150, 40),new Point(120, 20), new Point(120, 60) //}); //font demo Graphics g = e.Graphics; int y = 0; g.FillRectangle(Brushes.White, ClientRectangle); Rectangle rect = new Rectangle(0, y, 400, Font.Height); g.DrawRectangle(Pens.Blue, rect); g.DrawString("This text is left justified.", Font, Brushes.Black, rect); y += Font.Height + 20; Font aFont = new Font("Arial", 16, FontStyle.Bold|FontStyle.Italic); rect = new Rectangle(0, y, 400, aFont.Height); g.DrawRectangle(Pens.Blue, rect); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far; g.DrawString("This text is right justified.", aFont, Brushes.Blue, rect, sf); y += aFont.Height + 20; aFont.Dispose(); Font cFont = new Font("Courier New", 12, FontStyle.Underline); rect = new Rectangle(0, y, 400, cFont.Height); g.DrawRectangle(Pens.Blue, rect); sf = new StringFormat(); sf.Alignment = StringAlignment.Center; g.DrawString("This text is centered and underlined", cFont, Brushes.Red, rect, sf); y += cFont.Height + 20; Font trFont = new Font("Times New Roman", 12); rect = new Rectangle(0, y, 400, trFont.Height * 3); g.DrawRectangle(Pens.Blue, rect); string longStrng = "this text is much longer, and drawn "; longStrng += "into a rectangle that is higher than "; longStrng += "one line, so that it will wrap, It is very easy to wrap text useing GDI+"; g.DrawString(longStrng, trFont, Brushes.Black, rect); trFont.Dispose(); } } }

你可能感兴趣的:(C#: GDI+使用示例)