Csharp/C Sharp by API/System.Drawing/Graphics
Содержание
- 1 Graphics.Clear
- 2 Graphics.CopyFromScreen
- 3 Graphics.DashCap
- 4 Graphics.Dispose()
- 5 Graphics.DpiX
- 6 Graphics.DpiY
- 7 Graphics.DrawArc
- 8 Graphics.DrawBeziers
- 9 Graphics.DrawEllipse
- 10 Graphics.DrawIcon(Icon icon, int x, int y)
- 11 Graphics.DrawImage(Image i, int x, int y);
- 12 Graphics.DrawImage(Image img, int x, int y, int width, int height)
- 13 Graphics.DrawImage(Image, Points[])
- 14 Graphics.DrawImage(im, rec, recPart, GraphicsUnit.Pixel)
- 15 Graphics.DrawLine(Pen p, int x0,int y0, int x1, int y1)
- 16 Graphics.DrawLine(Pen,Point point1, Point point2)
- 17 Graphics.DrawLines(Pen pen, Point[] points)
- 18 Graphics.DrawPie
- 19 Graphics.DrawRectangle
- 20 Graphics.DrawRectangles
- 21 Graphics.DrawString
- 22 Graphics.EnumerateMetafileProc
- 23 Graphics.FillClosedCurve
- 24 Graphics.FillEllipse
- 25 Graphics.FillEllipse(Brush brush,int x, int y,int width,int height)
- 26 Graphics.FillPath(Brushes.AliceBlue, myPath);
- 27 Graphics.FillPolygon
- 28 Graphics.FillRectangle
- 29 Graphics.FillRectangles
- 30 Graphics.FillRegion
- 31 Graphics.FromHwnd()
- 32 Graphics.FromImage
- 33 Graphics.InterpolationMode
- 34 Graphics.MeasureString
- 35 Graphics.PageScale
- 36 Graphics.PageUnit
- 37 Graphics.PixelOffsetMode
- 38 Graphics.ReleaseHdc
- 39 Graphics.RotateTransform
- 40 Graphics.ScaleTransform
- 41 Graphics.SetClip
- 42 Graphics.SetClip(path, (CombineMode)miCombineMode.Index)
- 43 Graphics.SmoothingMode
- 44 Graphics.TextRenderingHint
- 45 Graphics.Transform
- 46 Graphics.TranslateClip
- 47 Graphics.TranslateTransform
- 48 Graphics.VisibleClipBounds
Graphics.Clear
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class RandomClear: Form {
public static void Main() { Application.Run(new RandomClear()); } public RandomClear() { Text = "Random Clear"; } protected override void OnPaint(PaintEventArgs pea) { Graphics graphics = pea.Graphics; Random rand = new Random(); graphics.Clear(Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))); }
}
</source>
Graphics.CopyFromScreen
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using System.Drawing.Drawing2D; public class Form1 : Form {
private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button cmdCapture; private System.Windows.Forms.PictureBox pictureBox1; public Form1() { InitializeComponent(); } private void cmdCapture_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(0, 0, 0, 0, bmp.Size); g.Dispose(); pictureBox1.Image = bmp; pictureBox1.Size = bmp.Size; } private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.cmdCapture = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.panel1.SuspendLayout(); ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.panel1.AutoScroll = true; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.Controls.Add(this.pictureBox1); this.panel1.Location = new System.Drawing.Point(8, 8); this.panel1.Size = new System.Drawing.Size(270, 233); this.cmdCapture.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.cmdCapture.Location = new System.Drawing.Point(169, 249); this.cmdCapture.Size = new System.Drawing.Size(110, 30); this.cmdCapture.Text = "Capture Screen"; this.cmdCapture.UseVisualStyleBackColor = true; this.cmdCapture.Click += new System.EventHandler(this.cmdCapture_Click); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Size = new System.Drawing.Size(100, 50); this.pictureBox1.TabStop = false; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(290, 288); this.Controls.Add(this.cmdCapture); this.Controls.Add(this.panel1); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Text = "Screen Capture"; this.panel1.ResumeLayout(false); ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
}
</source>
Graphics.DashCap
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class PenDashCaps: Form {
MenuItem miChecked; public static void Main() { Application.Run(new PenDashCaps()); } public PenDashCaps() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&Width"); int[] aiWidth = { 1, 2, 5, 10, 15, 20, 25 }; foreach (int iWidth in aiWidth) Menu.MenuItems[0].MenuItems.Add(iWidth.ToString(), new EventHandler(MenuWidthOnClick)); miChecked = Menu.MenuItems[0].MenuItems[0]; miChecked.Checked = true; } void MenuWidthOnClick(object obj, EventArgs ea) { miChecked.Checked = false; miChecked = (MenuItem) obj; miChecked.Checked = true; Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr, Convert.ToInt32(miChecked.Text)); pen.DashStyle = DashStyle.DashDotDot; foreach (DashCap dc in Enum.GetValues(typeof(DashCap))) { pen.DashCap = dc; grfx.DrawLine(pen, cx / 8, cy / 4, 7 * cx / 8, cy / 4); grfx.TranslateTransform(0, cy / 4); } }
}
</source>
Graphics.Dispose()
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class BlockOut: Form {
bool bBlocking, bValidBox; Point ptBeg, ptEnd; Rectangle rectBox; public static void Main() { Application.Run(new BlockOut()); } protected override void OnMouseDown(MouseEventArgs mea) { if (mea.Button == MouseButtons.Left) { ptBeg = ptEnd = new Point(mea.X, mea.Y); Graphics grfx = CreateGraphics(); grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd)); grfx.Dispose(); bBlocking = true; } } protected override void OnMouseMove(MouseEventArgs mea) { if (bBlocking) { Graphics grfx = CreateGraphics(); grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd)); ptEnd = new Point(mea.X, mea.Y); grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd)); grfx.Dispose(); Invalidate(); } } protected override void OnMouseUp(MouseEventArgs mea) { if (bBlocking && mea.Button == MouseButtons.Left) { Graphics grfx = CreateGraphics(); rectBox = Rect(ptBeg, new Point(mea.X, mea.Y)); grfx.DrawRectangle(new Pen(ForeColor), rectBox); grfx.Dispose(); bBlocking = false; bValidBox = true; Invalidate(); } } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; if (bValidBox) grfx.FillRectangle(new SolidBrush(ForeColor), rectBox); if (bBlocking) grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd)); } Rectangle Rect(Point ptBeg, Point ptEnd) { return new Rectangle(Math.Min(ptBeg.X, ptEnd.X), Math.Min(ptBeg.Y, ptEnd.Y), Math.Abs(ptEnd.X - ptBeg.X), Math.Abs(ptEnd.Y - ptBeg.Y)); }
}
</source>
Graphics.DpiX
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Bitmap bmp = new Bitmap("winter.jpg"); g.DrawImage(bmp, 0, 0); Console.WriteLine("Screen resolution: " + g.DpiX + "DPI"); Console.WriteLine("Image resolution: " + bmp.HorizontalResolution + "DPI"); Console.WriteLine("Image Width: " + bmp.Width); Console.WriteLine("Image Height: " + bmp.Height); SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution), bmp.Height * (g.DpiY / bmp.VerticalResolution)); Console.WriteLine("Display size of image: " + s); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Graphics.DpiY
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class DotsPerInch: Form {
public static void Main() { Application.Run(new DotsPerInch()); } public DotsPerInch() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.DrawString(String.Format("DpiX = {0}\nDpiY = {1}", grfx.DpiX, grfx.DpiY), Font, new SolidBrush(clr), 0, 0); }
}
</source>
Graphics.DrawArc
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class LineArcCombo: Form {
public static void Main() { Application.Run(new LineArcCombo()); } public LineArcCombo() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr, 25); grfx.DrawLine(pen, 25, 100, 125, 100); grfx.DrawArc (pen, 125, 50, 100, 100, -180, 180); grfx.DrawLine(pen, 225, 100, 325, 100); }
}
</source>
Graphics.DrawBeziers
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class BezierArt: Form {
public static void Main() { Application.Run(new BezierArt()); } public BezierArt() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,200, 200); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr); PointF[] aptf = new PointF[4]; int iNum = 100; for (int i = 0; i < iNum; i++) { double dAngle = 2 * i * Math.PI / iNum; aptf[0].X = cx / 2 + cx / 2 * (float) Math.Cos(dAngle); aptf[0].Y = 5 * cy / 8 + cy / 16 * (float) Math.Sin(dAngle); aptf[1] = new PointF(cx / 2, -cy); aptf[2] = new PointF(cx / 2, 2 * cy); dAngle += Math.PI; aptf[3].X = cx / 2 + cx / 4 * (float) Math.Cos(dAngle); aptf[3].Y = cy / 2 + cy / 16 * (float) Math.Sin(dAngle); grfx.DrawBeziers(pen, aptf); } }
}
</source>
Graphics.DrawEllipse
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class TryOneInchEllipse: Form {
public static void Main() { Application.Run(new TryOneInchEllipse()); } public TryOneInchEllipse() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.DrawEllipse(new Pen(clr), 0, 0, grfx.DpiX, grfx.DpiY); }
}
</source>
Graphics.DrawIcon(Icon icon, int x, int y)
<source lang="csharp">
/* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X
- /
using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace IconImageDraw_c {
/// <summary> /// Summary description for IconImageDraw. /// </summary> public class IconImageDraw : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public IconImageDraw() { // // Required for Windows Form Designer support // InitializeComponent(); this.MouseMove += new MouseEventHandler(this.DrawBox); this.MouseDown += new MouseEventHandler(this.StartBox); this.MouseUp += new MouseEventHandler(this.EndBox); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { // // IconImageDraw // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(492, 373); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MinimizeBox = false; this.Name = "IconImageDraw"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "IconImageDraw"; this.Load += new System.EventHandler(this.IconImageDraw_Load); } #endregion /// <summary> /// The main entry point for the application. /// </summary> /// //"R" holds the image, "Box" is the new image home currently being //drawn by holding the mouse down and dragging private Rectangle R = Rectangle.Empty; private Rectangle Box = Rectangle.Empty; private Image I = Image.FromFile("sample.jpg"); private Icon ThisIcon = new Icon("usa.ico"); private bool OK2Paint = false; [STAThread] static void Main() { Application.Run(new IconImageDraw()); } private void IconImageDraw_Load(object sender, System.EventArgs e) { } protected override void OnPaint ( PaintEventArgs e ) { //Always draw the icon e.Graphics.DrawIcon(ThisIcon, 1, 1); //Bail if rectangle is empty if ( R == Rectangle.Empty ) return; if ( !OK2Paint ) return; Pen P = new Pen(Brushes.Black, 3); e.Graphics.DrawRectangle(P, R); // Draw image based on rectangle. e.Graphics.DrawImage(I, R); P.Dispose(); } private void DrawBox ( System.Object sender , MouseEventArgs m ) { //Prints the x,y coordinates directly on the screen Graphics G = this.CreateGraphics(); Rectangle TextR = new Rectangle(10, this.Height-50, 100, 20 ); SolidBrush B = new SolidBrush(this.BackColor); G.FillRectangle(B, TextR); G.DrawString ( m.X.ToString() + ", " + m.Y.ToString(), new Font("Arial", 10), Brushes.Black, TextR, StringFormat.GenericDefault ); B.Dispose(); //Draw the box as the mouse drags if ( m.Button == MouseButtons.Left ) { if ( Box != Rectangle.Empty ) { Pen P = new Pen(new SolidBrush(this.BackColor),1); G.DrawRectangle ( P, Box ); P.Dispose(); } Box = new Rectangle ( R.X, R.Y, m.X - R.X, m.Y - R.Y ); G.DrawRectangle( Pens.Black, Box ); } } private void StartBox( System.Object sender , MouseEventArgs m ) { if ( m.Button == MouseButtons.Left ) { R.X=m.X; R.Y=m.Y; OK2Paint = false; } } private void EndBox( System.Object sender , MouseEventArgs m ) { R.Width = m.X - R.X; R.Height = m.Y - R.Y; OK2Paint = true; this.Refresh(); } }
}
</source>
Graphics.DrawImage(Image i, int x, int y);
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.IO; public class Form1 : System.Windows.Forms.Form {
[STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { string p = "icon.ico"; Icon ic = new Icon(p); this.Icon = ic; // Icon 1) Graphics g = e.Graphics; g.DrawIcon(ic, 0, 0); // Icon 2) Image i = ic.ToBitmap(); g.DrawImage(i, 50, 0); // Icon 3)
g.Dispose(); }
}
</source>
Graphics.DrawImage(Image img, int x, int y, int width, int height)
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class ImageReflection: Form {
Image image = Image.FromFile("Color.jpg"); public static void Main() { Application.Run(new ImageReflection()); } public ImageReflection() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { int cxImage = image.Width; int cyImage = image.Height; grfx.DrawImage(image, cx / 2, cy / 2, cxImage, cyImage); grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, cyImage); grfx.DrawImage(image, cx / 2, cy / 2, cxImage, -cyImage); grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, -cyImage); }
}
</source>
Graphics.DrawImage(Image, Points[])
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class ImageAtPoints: Form {
Image image = Image.FromFile("Color.jpg"); public static void Main() { Application.Run(new ImageAtPoints()); } public ImageAtPoints() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.DrawImage(image, new Point[] { new Point(cx / 2, 0), new Point(cx, cy / 2), new Point(0, cy / 2)}); }
}
</source>
Graphics.DrawImage(im, rec, recPart, GraphicsUnit.Pixel)
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ImageZoom {
/// <summary> /// Summary description for ImageZoom. /// </summary> public class ImageZoom1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.RadioButton radioButton4; private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckBox checkBox1; Image im = null; Image im2 = null; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public ImageZoom1() { // // Required for Windows Form Designer support // InitializeComponent(); this.Text = "Zooming"; this.label1.Text = ""; this.groupBox1.Text = "Zoom"; this.checkBox1.Text = "Paint"; this.radioButton1.Checked = false; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged); this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged); this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged); this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.radioButton4 = new System.Windows.Forms.RadioButton(); this.label2 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(200, 240); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.checkBox1, this.radioButton1, this.radioButton2, this.radioButton3, this.radioButton4}); this.groupBox1.Location = new System.Drawing.Point(232, 48); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(72, 128); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "groupBox1"; // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(8, 32); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(56, 24); this.checkBox1.TabIndex = 1; this.checkBox1.Text = "checkBox1"; this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(8, 64); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(16, 24); this.radioButton1.TabIndex = 0; this.radioButton1.Text = "radioButton1"; // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(40, 64); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(16, 24); this.radioButton2.TabIndex = 0; this.radioButton2.Text = "radioButton1"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(8, 96); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(16, 24); this.radioButton3.TabIndex = 0; this.radioButton3.Text = "radioButton1"; // // radioButton4 // this.radioButton4.Location = new System.Drawing.Point(40, 96); this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(16, 24); this.radioButton4.TabIndex = 0; this.radioButton4.Text = "radioButton1"; // // label2 // this.label2.Location = new System.Drawing.Point(328, 16); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(200, 240); this.label2.TabIndex = 0; this.label2.Text = "label1"; // // ImageZoom // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(536, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox1, this.label1, this.label2}); this.Name = "ImageZoom"; this.Text = "ImageZoom"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ImageZoom1()); } protected override void OnPaint(PaintEventArgs e) { ImageZoom(); } private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); } private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); } protected void ImageZoom() { Graphics g1 = Graphics.FromHwnd(this.label1.Handle); Graphics g2 = Graphics.FromHwnd(this.label2.Handle); Rectangle rec; Rectangle recPart; if (this.checkBox1.Checked) { if (im == null) ReadImage(); rec = new Rectangle(0, 0, label1.Width, label1.Height); g1.DrawImage(im, rec); // Center part: recPart = new Rectangle(im.Width/4, im.Height/4, im.Width/2, im.Height/2); if(this.radioButton1.Checked) // Left-Top part recPart = new Rectangle(0, 0, im.Width/2, im.Height/2); if(this.radioButton2.Checked) // Right-Top part recPart = new Rectangle(im.Width/2, 0, im.Width/2, im.Height/2); if(this.radioButton3.Checked) // Left-Down part recPart = new Rectangle(0, im.Height/2, im.Width/2, im.Height/2); if(this.radioButton4.Checked) // Right-Down part recPart = new Rectangle(im.Width/2, im.Height/2, im.Width/2, im.Height/2); g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel); } else { Clear(g1); Clear(g2); } g1.Dispose(); g2.Dispose(); } protected void ReadImage() { string path = @"szeret3.BMP"; im = Image.FromFile(path); this.radioButton1.Enabled = true; this.radioButton2.Enabled = true; this.radioButton3.Enabled = true; this.radioButton4.Enabled = true; } protected void Clear(Graphics g) { g.Clear(this.BackColor); g.Dispose(); im = null; im2 = null; this.radioButton1.Checked = false; this.radioButton2.Checked = false; this.radioButton3.Checked = false; this.radioButton4.Checked = false; this.radioButton1.Enabled = false; this.radioButton2.Enabled = false; this.radioButton3.Enabled = false; this.radioButton4.Enabled = false; } }
}
</source>
Graphics.DrawLine(Pen p, int x0,int y0, int x1, int y1)
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black); g.DrawLine(p, 0, 0, 100, 100); p.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Graphics.DrawLine(Pen,Point point1, Point point2)
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class frmScribble : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null; private bool mouseDown = false; private Point lastPoint = Point.Empty; private string color = "black"; private Graphics g; private Pen p; public frmScribble() { g = CreateGraphics(); p = new Pen(Color.FromName(color)); } protected override void OnMouseDown(MouseEventArgs e) { mouseDown = true; if (e.Button == MouseButtons.Right) { ContextMenu m = new ContextMenu(); m.MenuItems.Add(0, new MenuItem("black", new EventHandler(RightMouseButton_Click))); m.MenuItems.Add(1, new MenuItem("white", new EventHandler(RightMouseButton_Click))); m.MenuItems.Add(2, new MenuItem("red", new EventHandler(RightMouseButton_Click))); m.MenuItems.Add(3, new MenuItem("green", new EventHandler(RightMouseButton_Click))); m.MenuItems.Add(4, new MenuItem("blue", new EventHandler(RightMouseButton_Click))); m.Show(this, new Point(e.X, e.Y)); } } protected void RightMouseButton_Click(object sender, EventArgs e) { color = ((MenuItem)sender).Text; p = new Pen(Color.FromName(color)); } protected override void OnMouseUp(MouseEventArgs e) { mouseDown = false; } protected override void OnMouseMove(MouseEventArgs e) { if (lastPoint.Equals(Point.Empty)) lastPoint = new Point(e.X, e.Y); if (mouseDown) { Point pMousePos = new Point(e.X, e.Y); g.DrawLine(p, pMousePos, lastPoint); } lastPoint = new Point(e.X, e.Y); } [STAThread] static void Main() { Application.Run(new frmScribble()); }
}
</source>
Graphics.DrawLines(Pen pen, Point[] points)
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class WidePolyline: Form {
public static void Main() { Application.Run(new WidePolyline()); } public WidePolyline() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr, 25); grfx.DrawLines(pen, new Point[] { new Point( 25, 100), new Point(125, 100), new Point(125, 50), new Point(225, 50), new Point(225, 100), new Point(325, 100) }); }
}
</source>
Graphics.DrawPie
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class PieChart: Form {
int[] aiValues = { 50, 100, 25, 150, 100, 75 }; public static void Main() { Application.Run(new PieChart()); } public PieChart() { Text = "Pie Chart"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Rectangle rect = new Rectangle(50, 50, 200, 200); Pen pen = new Pen(clr); int iTotal = 0; float fAngle = 0, fSweep; foreach(int iValue in aiValues) iTotal += iValue; foreach(int iValue in aiValues) { fSweep = 360f * iValue / iTotal; DrawPieSlice(grfx, pen, rect, fAngle, fSweep); fAngle += fSweep; } } protected virtual void DrawPieSlice(Graphics grfx, Pen pen, Rectangle rect, float fAngle, float fSweep) { grfx.DrawPie(pen, rect, fAngle, fSweep); }
}
</source>
Graphics.DrawRectangle
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class OutlineClientRectangle: Form {
public static void Main() { Application.Run(new OutlineClientRectangle()); } public OutlineClientRectangle() { Text = "Client Rectangle"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.DrawRectangle(Pens.Red, 0, 0, cx - 1, cy - 1); }
}
</source>
Graphics.DrawRectangles
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class FourByFours: Form {
public static void Main() { Application.Run(new FourByFours()); } public FourByFours() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr); Brush brush = new SolidBrush(clr); grfx.DrawRectangles(pen, new Rectangle[] {new Rectangle(8, 2, 4, 4)}); }
}
</source>
Graphics.DrawString
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class TwoPaintHandlers {
public static void Main() { Form form = new Form(); form.Text = "Two Paint Handlers"; form.BackColor = Color.White; form.Paint += new PaintEventHandler(PaintHandler1); form.Paint += new PaintEventHandler(PaintHandler2); Application.Run(form); } static void PaintHandler1(object objSender, PaintEventArgs pea) { Form form = (Form)objSender; Graphics graphics = pea.Graphics; graphics.DrawString("First Paint Event Handler", form.Font, Brushes.Black, 0, 0); } static void PaintHandler2(object objSender, PaintEventArgs pea) { Form form = (Form)objSender; Graphics graphics = pea.Graphics; graphics.DrawString("Second Paint Event Handler", form.Font, Brushes.Black, 0, 100); }
}
</source>
Graphics.EnumerateMetafileProc
<source lang="csharp"> /* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X
- /
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace MetaEnum_c {
public class MetaEnum : System.Windows.Forms.Form { private System.ruponentModel.Container components = null; private System.Windows.Forms.ListBox LB; Metafile mf = new Metafile("mymeta.emf"); public MetaEnum() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } mf.Dispose(); base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.LB = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // LB // this.LB.Location = new System.Drawing.Point(208, 232); this.LB.Name = "LB"; this.LB.Size = new System.Drawing.Size(168, 121); this.LB.TabIndex = 1; // // MetaEnum // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(392, 373); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.LB}); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MetaEnum"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "MetaEnum"; this.Load += new System.EventHandler(this.MetaEnum_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new MetaEnum()); } private void MetaEnum_Load(object sender, System.EventArgs e) { } protected override void OnPaint(PaintEventArgs e) { Graphics G = e.Graphics; G.EnumerateMetafile( mf, new Point( 50, 50 ), new Graphics.EnumerateMetafileProc(this.MetafileCallback) ); } // Define callback method. private bool MetafileCallback( EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData) { LB.Items.Add(recordType); if ( dataSize > 0 ) { byte[] D = new byte[dataSize]; Marshal.Copy(data, D, 0, dataSize); mf.PlayRecord(recordType, flags, dataSize, D); } return true; } }
}
</source>
Graphics.FillClosedCurve
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class ClosedCurveFillModes: Form {
public static void Main() { Application.Run(new ClosedCurveFillModes()); } ClosedCurveFillModes() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Brush brush = new SolidBrush(clr); Point[] apt = new Point[5]; for (int i = 0; i < apt.Length; i++) { double dAngle = (i * 0.8 - 0.5) * Math.PI; apt[i] = new Point( (int)(cx *(0.25 + 0.24 * Math.Cos(dAngle))), (int)(cy *(0.50 + 0.48 * Math.Sin(dAngle)))); } grfx.FillClosedCurve(brush, apt, FillMode.Alternate); for (int i = 0; i < apt.Length; i++) apt[i].X += cx / 2; grfx.FillClosedCurve(brush, apt, FillMode.Winding); }
}
</source>
Graphics.FillEllipse
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class FourByFours: Form {
public static void Main() { Application.Run(new FourByFours()); } public FourByFours() { Text = "Four by Fours"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr); Brush brush = new SolidBrush(clr); grfx.FillEllipse(brush, new Rectangle(14, 8, 4, 4)); }
}
</source>
Graphics.FillEllipse(Brush brush,int x, int y,int width,int height)
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
public class Test : Form {
private RadioButton square = new RadioButton(); private RadioButton circle = new RadioButton(); private ComboBox color = new ComboBox(); private Color c = Color.Red; public Test( ) { Text = "Select Item"; square.Text = "Square"; circle.Text = "Circle"; color.Text = "Choose a color"; Size = new Size(400,250); int w = 20; square.Location = new Point(w, 30); circle.Location = new Point(w += 10 + square.Width, 30); color.Location = new Point(w += 10 + circle.Width, 30); color.Items.Add("Red"); color.Items.Add("Green"); color.Items.Add("Blue"); Controls.Add(square); Controls.Add(circle); Controls.Add(color); square.CheckedChanged += new EventHandler(Checked_Changed); circle.CheckedChanged += new EventHandler(Checked_Changed); color.SelectedIndexChanged += new EventHandler(Selected_Index); } protected override void OnPaint(PaintEventArgs e){ Graphics g = e.Graphics; Brush brush = new SolidBrush(c); if (square.Checked) g.FillRectangle(brush,100,100,100,100); else g.FillEllipse(brush,100,100,100,100); base.OnPaint( e ); } protected void Selected_Index(Object sender, EventArgs e){ if (color.SelectedItem.ToString() == "Red" ) c = Color.Red; else if (color.SelectedItem.ToString() == "Green") c = Color.Green; else c = Color.Blue; Invalidate(); } protected void Checked_Changed(Object sender, EventArgs e) { Invalidate(); } static void Main() { Application.Run(new Test()); }
}
</source>
Graphics.FillPath(Brushes.AliceBlue, myPath);
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { GraphicsPath myPath = new GraphicsPath(); private bool isImageClicked = false; public Form1() { InitializeComponent(); myPath.StartFigure(); myPath.AddLine(new Point(150, 10), new Point(120, 150)); myPath.AddArc(200, 200, 100, 100, 0, 90); Point[] points = {new Point(350, 325), new Point(250, 350), new Point(250, 250), new Point(350, 275)}; myPath.AddCurve(points); myPath.CloseFigure(); CenterToScreen(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = "Form1"; this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { Point mousePt = new Point(e.X, e.Y); if(myPath.IsVisible(mousePt)) { isImageClicked = true; this.Text = "You clicked the strange shape..."; } else { isImageClicked = false; this.Text = "Images"; } Invalidate(); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillPath(Brushes.AliceBlue, myPath); if(isImageClicked == true) { Pen outline = new Pen(Color.Black, 2); g.DrawPath(outline, myPath); } } } </source>
Graphics.FillPolygon
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class OwnerDrawButtons: Form {
readonly int cxImage, cyImage; readonly int cxBtn, cyBtn, dxBtn; readonly Button btnLarger, btnSmaller; public static void Main() { Application.Run(new OwnerDrawButtons()); } public OwnerDrawButtons() { ResizeRedraw = true; cxImage = 4 * Font.Height; cyImage = 4 * Font.Height; cxBtn = cxImage + 8; cyBtn = cyImage + 8; dxBtn = Font.Height; btnLarger = new Button(); btnLarger.Parent = this; btnLarger.Size = new Size(cxBtn, cyBtn); btnLarger.Click += new EventHandler(ButtonLargerOnClick); btnLarger.Paint += new PaintEventHandler(ButtonOnPaint); btnSmaller = new Button(); btnSmaller.Parent = this; btnSmaller.Size = new Size(cxBtn, cyBtn); btnSmaller.Click += new EventHandler(ButtonSmallerOnClick); btnSmaller.Paint += new PaintEventHandler(ButtonOnPaint); OnResize(EventArgs.Empty); } protected override void OnResize(EventArgs ea) { base.OnResize(ea); btnLarger.Location = new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2, (ClientSize.Height - cyBtn) / 2); btnSmaller.Location = new Point(ClientSize.Width / 2 + dxBtn / 2, (ClientSize.Height - cyBtn) / 2); } void ButtonLargerOnClick(object obj, EventArgs ea) { Console.WriteLine("clicked large"); } void ButtonSmallerOnClick(object obj, EventArgs ea) { Console.WriteLine("clicked small"); } void ButtonOnPaint(object obj, PaintEventArgs pea) { Button btn = (Button) obj; Graphics grfx = pea.Graphics; ControlPaint.DrawButton(grfx, 0, 0, cxBtn, cyBtn, (btn == (Button) GetChildAtPoint( PointToClient( MousePosition))) && btn.Capture ? ButtonState.Pushed : ButtonState.Normal); GraphicsState grfxstate = grfx.Save(); grfx.TranslateTransform((cxBtn - cxImage) / 2, (cyBtn - cyImage) / 2); DrawLargerButton(grfx, cxImage, cyImage); grfx.Restore(grfxstate); if (btn.Focused) ControlPaint.DrawFocusRectangle(grfx, new Rectangle((cxBtn - cxImage) / 2 + cxImage / 16, (cyBtn - cyImage) / 2 + cyImage / 16, 7 * cxImage / 8, 7 * cyImage / 8)); } void DrawLargerButton(Graphics grfx, int cx, int cy) { Brush brush = new SolidBrush(btnLarger.ForeColor); Pen pen = new Pen(btnLarger.ForeColor); grfx.TranslateTransform(cx / 2, cy / 2); for (int i = 0; i < 4; i++) { grfx.DrawLine(pen, 0, 0, cx / 4, 0); grfx.FillPolygon(brush, new Point[] { new Point(cx / 4, -cy / 8), new Point(cx / 2, 0), new Point(cx / 4, cy / 8)}); grfx.RotateTransform(90); } }
}
</source>
Graphics.FillRectangle
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 12); Font bf = new Font(f, FontStyle.Bold); StringFormat sf = new StringFormat(); float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f }; sf.SetTabStops(0.0f, ts); string s1 = "\tA\tAA\tAAA\tAAAA"; string s2 = "\tAAAA\tAAA\tAA\tA"; string s3 = "\tAAAAAAAA\tAAAAAAA\tAAAAAA\tAAAAA\n\tAAAA\tAAA\tAA\tAA"; g.DrawString(s1, bf, Brushes.Black, 20, 20, sf); g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf); g.DrawString(s3, f, Brushes.Blue, 20, 20 + bf.Height + f.Height, sf); f.Dispose(); bf.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Graphics.FillRectangles
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class FourByFours: Form {
public static void Main() { Application.Run(new FourByFours()); } public FourByFours() { Text = "Four by Fours"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr); Brush brush = new SolidBrush(clr); grfx.FillRectangles(brush, new Rectangle[] {new Rectangle(8, 8, 4, 4)}); }
}
</source>
Graphics.FillRegion
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddLine(10, 10, 10, 50); gp.AddBezier(10, 50, 120, 55, 25, 70, 30, 70); gp.AddLine(30, 70, 60, 70); gp.AddBezier(60, 70, 305, 70, 90, 55, 90, 50); gp.AddLine(90, 50, 90, 30); gp.StartFigure(); gp.AddLine(60, 110, 40, 160); gp.AddLine(40, 160, 60, 180); // Create a Region whose boundary is the above GraphicsPath Region reg = new Region(gp); g.FillRegion(Brushes.Green, reg); reg.Dispose(); gp.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Graphics.FromHwnd()
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace Cube {
/// <summary> /// Summary description for CubeImage. /// </summary> public class CubeImage : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public CubeImage() { // // Required for Windows Form Designer support // InitializeComponent(); this.Text = "Picture Cube"; // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { // // CubeImage // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 189); this.Name = "CubeImage"; this.Text = "CubeImage"; } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new CubeImage()); } protected override void OnPaint(PaintEventArgs pea) { CubeDBuf(); } private void CubeDBuf() { Graphics g; string path; int x = 100, y = 40; Point A = new Point( 10, 50); Point B = new Point(180, 50); Point C = new Point( 10, 170); Point a = new Point(A.X + x, A.Y - y); Point b = new Point(B.X + x, B.Y - y); Point Z = new Point(B.X, C.Y); Point[] p3Fro = {A, B, C}; Point[] p3Top = {a, b, A}; Point[] p3Rig = {B, b, Z}; Bitmap bm = new Bitmap(B.X +x, C.Y + y); g = Graphics.FromImage(bm); path = @"IndonHouses.bmp"; Image im1 = Image.FromFile(path); g.DrawImage(im1, p3Fro); path = @"Pyramids.BMP"; Image im3 = Image.FromFile(path); g.DrawImage(im3, p3Top); path = @"TadjMahal.bmp"; Image im2 = Image.FromFile(path); g.DrawImage(im2, p3Rig); g = Graphics.FromHwnd(this.Handle); g.DrawImage(bm, 1, 1); g.Dispose(); } }
}
</source>
Graphics.FromImage
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form {
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs p) { Graphics g = p.Graphics; } private void createManually() { Graphics g; g = this.CreateGraphics(); } private void createFromFile() { Graphics g; Bitmap b; b = new Bitmap("C:\\E.bmp"); g = Graphics.FromImage(b); } private void drawLine() { Graphics g; g = this.CreateGraphics(); Icon i = new Icon(@"C:\Desktop.ico"); g.DrawIcon(i, 150, 15); }
}
</source>
Graphics.InterpolationMode
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Bitmap bmp = new Bitmap("rama.jpg"); g.FillRectangle(Brushes.White, this.ClientRectangle); int width = bmp.Width; int height = bmp.Height; g.InterpolationMode = InterpolationMode.NearestNeighbor; g.DrawImage( bmp, new Rectangle(10, 10, 120, 120), // source rectangle new Rectangle(0, 0, width, height), // destination rectangle GraphicsUnit.Pixel); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Graphics.MeasureString
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms;
class SysInfoColumns: Form {
public static void Main() { Application.Run(new SysInfoColumns()); } public SysInfoColumns() { } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Brush brush = new SolidBrush(ForeColor); SizeF sizef; float cxCol, y = 0; int cySpace; sizef = grfx.MeasureString("ArrangeStartingPosition ", Font); cxCol = sizef.Width; cySpace = Font.Height; grfx.DrawString("ArrangeDirection", Font, brush, 0, y); grfx.DrawString(SystemInformation.ArrangeDirection.ToString(), Font, brush, cxCol, y); y += cySpace; grfx.DrawString("ArrangeStartingPosition", Font, brush, 0, y); grfx.DrawString( SystemInformation.ArrangeStartingPosition.ToString(), Font, brush, cxCol, y); y += cySpace; grfx.DrawString("BootMode", Font, brush, 0, y); grfx.DrawString(SystemInformation.BootMode.ToString(), Font, brush, cxCol, y); y += cySpace; grfx.DrawString("Border3DSize", Font, brush, 0, y); grfx.DrawString(SystemInformation.Border3DSize.ToString(), Font, brush, cxCol, y); y += cySpace; grfx.DrawString("BorderSize", Font, brush, 0, y); grfx.DrawString(SystemInformation.BorderSize.ToString(), Font, brush, cxCol, y); y += cySpace; grfx.DrawString("CaptionButtonSize", Font, brush, 0, y); grfx.DrawString(SystemInformation.CaptionButtonSize.ToString(), Font, brush, cxCol, y); }
}
</source>
Graphics.PageScale
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class ArbitraryCoordinates: Form {
public static void Main() { Application.Run(new ArbitraryCoordinates()); } public ArbitraryCoordinates() { Text = "Arbitrary Coordinates"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.PageUnit = GraphicsUnit.Pixel; SizeF sizef = grfx.VisibleClipBounds.Size; grfx.PageScale = Math.Min(sizef.Width / grfx.DpiX / 1000, sizef.Height / grfx.DpiY / 1000); grfx.DrawEllipse(new Pen(clr), 0, 0, 990, 990); }
}
</source>
Graphics.PageUnit
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class ArbitraryCoordinates: Form {
public static void Main() { Application.Run(new ArbitraryCoordinates()); } public ArbitraryCoordinates() { Text = "Arbitrary Coordinates"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.PageUnit = GraphicsUnit.Pixel; SizeF sizef = grfx.VisibleClipBounds.Size; grfx.DrawEllipse(new Pen(clr), 0, 0, 990, 990); }
}
</source>
Graphics.PixelOffsetMode
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class AntiAlias: Form {
public static void Main() { Application.Run(new AntiAlias()); } public AntiAlias() { Text = "Anti-Alias Demo"; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Pen pen = new Pen(ForeColor); grfx.PixelOffsetMode = PixelOffsetMode.Default; grfx.DrawLine(pen, 2, 2, 18, 10); }
}
</source>
Graphics.ReleaseHdc
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms;
class CreateMetafileMemory: Form {
MemoryStream ms = new MemoryStream(); public static void Main() { Application.Run(new CreateMetafileMemory()); } public CreateMetafileMemory() { ResizeRedraw = true; Graphics grfx = CreateGraphics(); IntPtr ipHdc = grfx.GetHdc(); Metafile mf = new Metafile(ms, ipHdc); grfx.ReleaseHdc(ipHdc); grfx.Dispose(); grfx = Graphics.FromImage(mf); grfx.FillEllipse(Brushes.Gray, 0, 0, 100, 100); grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100); grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120); grfx.Dispose(); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { ms.Position = 0; Metafile mf = new Metafile(ms); for (int y = 0; y < cy; y += mf.Height) for (int x = 0; x < cx; x += mf.Width) grfx.DrawImage(mf, x, y, mf.Width, mf.Height); }
}
</source>
Graphics.RotateTransform
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 24); g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0); g.RotateTransform(45); g.TranslateTransform(150, 0); g.DrawString("Rotate then Translate ", f, Brushes.Black, 0, 0); } public static void Main() { Application.Run(new Form1()); }
}
</source>
Graphics.ScaleTransform
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class MobyDick: Form {
public static void Main() { Application.Run(new MobyDick()); } public MobyDick() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.ScaleTransform(1, 3); grfx.DrawString("abc", Font, new SolidBrush(clr), new Rectangle(0, 0, cx, cy)); }
}
</source>
Graphics.SetClip
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class KeyholeClip: Form {
protected Image image; protected GraphicsPath path; public static void Main() { Application.Run(new KeyholeClip()); } public KeyholeClip() { ResizeRedraw = true; image = Image.FromFile("Color.jpg"); path = new GraphicsPath(); path.AddArc(80, 0, 80, 80, 45, -270); path.AddLine(70, 180, 170, 180); } protected override void OnPaint(PaintEventArgs pea) { pea.Graphics.SetClip(path); pea.Graphics.DrawImage(image, 0, 0, image.Width, image.Height); }
}
</source>
Graphics.SetClip(path, (CombineMode)miCombineMode.Index)
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class ClippingCombinations: Form {
string strCaption = "CombineMode = "; MenuItem miCombineMode; public static void Main() { Application.Run(new ClippingCombinations()); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } public ClippingCombinations() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&CombineMode"); EventHandler ehClick = new EventHandler(MenuCombineModeOnClick); for (int i = 0; i < 6; i++) { MenuItem mi = new MenuItem("&" + (CombineMode)i); mi.Click += ehClick; mi.RadioCheck = true; Menu.MenuItems[0].MenuItems.Add(mi); } miCombineMode = Menu.MenuItems[0].MenuItems[0]; miCombineMode.Checked = true; } void MenuCombineModeOnClick(object obj, EventArgs ea) { miCombineMode.Checked = false; miCombineMode = (MenuItem) obj; miCombineMode.Checked = true; Text = strCaption + (CombineMode)miCombineMode.Index; Invalidate(); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 2 * cx / 3, cy); grfx.SetClip(path); path.Reset(); path.AddEllipse(cx / 3, 0, 2 * cx / 3, cy); grfx.SetClip(path, (CombineMode)miCombineMode.Index); grfx.FillRectangle(Brushes.Red, 0, 0, cx, cy); }
}
</source>
Graphics.SmoothingMode
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class AntiAlias: Form {
public static void Main() { Application.Run(new AntiAlias()); } public AntiAlias() { Text = "Anti-Alias Demo"; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Pen pen = new Pen(ForeColor); grfx.SmoothingMode = SmoothingMode.None; grfx.DrawLine(pen, 2, 2, 18, 10); }
}
</source>
Graphics.TextRenderingHint
<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Drawing.Text; namespace GDI_Basics {
/// <summary> /// Summary description for SmoothingFonts. /// </summary> public class SmoothingFonts : System.Windows.Forms.Form { internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public SmoothingFonts() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Label3 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // Label3 // this.Label3.BackColor = System.Drawing.Color.White; this.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label3.Location = new System.Drawing.Point(272, 168); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(128, 16); this.Label3.TabIndex = 5; this.Label3.Text = " ClearTypeGridFit"; // // Label2 // this.Label2.BackColor = System.Drawing.Color.White; this.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label2.Location = new System.Drawing.Point(272, 100); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(128, 16); this.Label2.TabIndex = 4; this.Label2.Text = " AntiAliasGridFit"; // // Label1 // this.Label1.BackColor = System.Drawing.Color.White; this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Label1.Location = new System.Drawing.Point(272, 36); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(128, 16); this.Label1.TabIndex = 3; this.Label1.Text = " SingleBitPerPixelGridFit"; // // SmoothingFonts // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(440, 314); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label3, this.Label2, this.Label1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "SmoothingFonts"; this.Text = "SmoothingFonts"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingFonts_Paint); this.ResumeLayout(false); } #endregion
[STAThread] static void Main() { Application.Run(new SmoothingFonts()); } private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic); e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 20); e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 85); e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 150); } }
}
</source>
Graphics.Transform
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class MobyDick: Form {
public static void Main() { Application.Run(new MobyDick()); } public MobyDick() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.Transform = new Matrix(1, 1, -1, 1, 0, 0); grfx.DrawString("abc", Font, new SolidBrush(clr), new Rectangle(0, 0, cx, cy)); }
}
</source>
Graphics.TranslateClip
<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class KeyholeClipCentered : Form {
GraphicsPath path = new GraphicsPath(); Image image = Image.FromFile("Color.jpg"); public static void Main() { Application.Run(new KeyholeClipCentered()); } public KeyholeClipCentered() { ResizeRedraw = true; path.AddArc(80, 0, 80, 80, 45, -270); path.AddLine(70, 180, 170, 180); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.SetClip(path); RectangleF rectf = path.GetBounds(); int xOffset = (int)((cx - rectf.Width) / 2 - rectf.X); int yOffset = (int)((cy - rectf.Height) / 2 - rectf.Y); grfx.TranslateClip(xOffset, yOffset); grfx.DrawImage(image, xOffset, yOffset, image.Width, image.Height); }
}
</source>
Graphics.TranslateTransform
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;
class RotatedRectangles: Form {
public static void Main() { Application.Run(new RotatedRectangles()); } public RotatedRectangles() { Text = "Rotated Rectangles"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Pen pen = new Pen(clr); grfx.PageUnit = GraphicsUnit.Pixel; PointF[] aptf = { (PointF) grfx.VisibleClipBounds.Size }; grfx.PageUnit = GraphicsUnit.Inch; grfx.PageScale = 0.01f; grfx.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, aptf); grfx.TranslateTransform(aptf[0].X / 2, aptf[0].Y / 2); for (int i = 0; i < 6; i++) { grfx.DrawRectangle(pen, 0, 0, 200, 200); grfx.RotateTransform(10); } }
}
</source>
Graphics.VisibleClipBounds
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class DrawOnImage: Form {
Image image = Image.FromFile("Color.jpg"); string str = "www.nfex.ru"; public static void Main() { Application.Run(new DrawOnImage()); } public DrawOnImage() { ResizeRedraw = true; Graphics grfxImage = Graphics.FromImage(image); grfxImage.PageUnit = GraphicsUnit.Inch; grfxImage.PageScale = 1; SizeF sizef = grfxImage.MeasureString(str, Font); grfxImage.DrawString(str, Font, Brushes.White, grfxImage.VisibleClipBounds.Width - sizef.Width, 0); grfxImage.Dispose(); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { grfx.PageUnit = GraphicsUnit.Pixel; grfx.DrawImage(image, 0, 0); grfx.DrawString(str, Font, new SolidBrush(clr), grfx.DpiX * image.Width / image.HorizontalResolution, 0); }
}
</source>