. Net graphical operations (msdn)

Source: Internet
Author: User

This example shows how to draw a solid ellipse on a form.

Example
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);System.Drawing.Graphics formGraphics = this.CreateGraphics();formGraphics.FillEllipse(myBrush, new Rectangle(0,0,200,300));myBrush.Dispose();formGraphics.Dispose();
Compile code

This example requires:

  • A Windows application project.

The code must be inFormClass. This form instance is composedThis.

Reliable Programming

Always use any objects of system resources (suchBrushAndGraphicsObject) CallDispose.

This example shows how to draw a solid rectangle on a form.

Example
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);System.Drawing.Graphics formGraphics = this.CreateGraphics();formGraphics.FillRectangle(myBrush, new Rectangle(0,0,200,300));myBrush.Dispose();formGraphics.Dispose();
Compile code

This example requires:

  • A Windows application project.

The code must be inFormClass. This form instance is composedThis.

Reliable Programming

Always use any objects of system resources (suchBrushAndGraphicsObject) CallDispose.

This example shows how to draw a hollow ellipse and a rectangle on a form.

Example
private void DrawEllipse(){System.Drawing.Pen myPen;myPen = new System.Drawing.Pen(System.Drawing.Color.Red);System.Drawing.Graphics formGraphics = this.CreateGraphics();formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300));myPen.Dispose();formGraphics.Dispose();}private void DrawRectangle(){System.Drawing.Pen myPen;myPen = new System.Drawing.Pen(System.Drawing.Color.Red);System.Drawing.Graphics formGraphics = this.CreateGraphics();formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300));myPen.Dispose();formGraphics.Dispose();}
Compile code

This example requires:

  • A Windows application project.

The code must be inFormClass. This form instance is composedThis.

Reliable Programming

Always use any objects of system resources (suchPenAndGraphicsObject) CallDispose.

This example shows how to draw text vertically on a form.

Example
private void DrawVerticalText(){System.Drawing.Graphics formGraphics = this.CreateGraphics();string drawString = "Sample Text";System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);float x = 150.0f;float y = 50.0f;System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);drawFont.Dispose();drawBrush.Dispose();formGraphics.Dispose();}
Compile code

This example requires:

  • A Windows application project.

The code must be inFormClass. This form instance is composedThis.

Reliable Programming

Always use any objects of system resources (suchFontAndGraphicsObject) CallDispose.

Exceptions may occur in the following situations:

  • The Arial font is not installed.

This example shows how to print a preview copy of the current form.

Example
[System.Runtime.InteropServices.DllImport("gdi32.dll")]public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);private Bitmap memoryImage;private void CaptureScreen(){Graphics mygraphics = this.CreateGraphics();Size s = this.Size;memoryImage = new Bitmap(s.Width, s.Height, mygraphics);Graphics memoryGraphics = Graphics.FromImage(memoryImage);IntPtr dc1 = mygraphics.GetHdc();IntPtr dc2 = memoryGraphics.GetHdc();BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);mygraphics.ReleaseHdc(dc1);memoryGraphics.ReleaseHdc(dc2);}private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e){e.Graphics.DrawImage(memoryImage, 0, 0);}private void printButton_Click(System.Object sender, System.EventArgs e){CaptureScreen();printPreviewDialog1.Show();}
Compile code

This example requires:

  • The name is printdocument1 and contains the printpage event handlerPrintdocumentComponent.
  • The name is printpreviewdialog1.PrintpreviewdialogComponent, whose document attribute is set to printdocument1.
  • The name is printbutton and contains the Click Event HandlerButtonObject.

This sample code replaces an existing event handler. When you click printbutton, the print preview of the form is displayed.

Reliable Programming

Exceptions may occur in the following situations:

  • You are not authorized to access the printer.
  • You are not authorized to use unmanaged code.
  • No printer is installed.
  • The print preview dialog box has been disposed of before. This happens when the "print preview" dialog box is closed.
Security

To run this example, you must have the permission to run unmanaged code and access the printer.

This example shows how to printDataGridControl.

Example
private void printGrid_Click(System.Object sender, System.EventArgs e){printDocument1.Print();}private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e){PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));this.InvokePaint(dataGrid1, myPaintArgs);}
Compile code

This example requires:

  • The button control named printgrid and containing the click event handler.
  • TheDataGridControl.
  • The name is printdocument1 and contains the printpage event handlerPrintdocumentComponent.

This sample code replaces an existing event handler.

Reliable Programming

Exceptions may occur in the following situations:

  • You are not authorized to access the printer.
  • No printer is installed.

This example shows how to print a text file.

Example
System.IO.StreamReader fileToPrint;System.Drawing.Font printFont;private void printButton_Click(object sender, EventArgs e){string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);fileToPrint = new System.IO.StreamReader(printPath + @"\myFile.txt");printFont = new System.Drawing.Font("Arial", 10);printDocument1.Print();fileToPrint.Close();}private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){float yPos = 0f;int count = 0;float leftMargin = e.MarginBounds.Left;float topMargin = e.MarginBounds.Top;string line = null;float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);while (count < linesPerPage){line = fileToPrint.ReadLine();if (line == null){break;}yPos = topMargin + count * printFont.GetHeight(e.Graphics);e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());count++;}if (line != null){e.HasMorePages = true;}}
Compile code

This example requires:

  • A button control named printbutton and containing the click event handler.
  • The name is printdocument1 and contains the printpage event handlerPrintdocumentComponent.

This example also assumes that there is a text file named myfile.txt on your desktop. This sample code replaces an existing event handler.

Reliable Programming

File operations should be included in appropriateTry... catch... finallyBlock. Always callStreamreader. Close.

Exceptions may occur in the following situations:

  • You are not authorized to access the printer.
  • You are not authorized to access the file system.
  • No printer is installed.
  • The Arial font is not installed.
Security

To run this example, you must have access to the file system and printer.

This example shows how to print a copy of the current form.

Example
[System.Runtime.InteropServices.DllImport("gdi32.dll")]public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);private Bitmap memoryImage;private void CaptureScreen(){Graphics mygraphics = this.CreateGraphics();Size s = this.Size;memoryImage = new Bitmap(s.Width, s.Height, mygraphics);Graphics memoryGraphics = Graphics.FromImage(memoryImage);IntPtr dc1 = mygraphics.GetHdc();IntPtr dc2 = memoryGraphics.GetHdc();BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);mygraphics.ReleaseHdc(dc1);memoryGraphics.ReleaseHdc(dc2);}private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e){e.Graphics.DrawImage(memoryImage, 0, 0);}private void printButton_Click(System.Object sender, System.EventArgs e){CaptureScreen();printDocument1.Print();}
Compile code

This example requires:

  • The name is printdocument1 and contains the printpage event handlerPrintdocumentComponent.
  • The name is printbutton and contains the Click Event HandlerButtonObject.

This sample code replaces an existing event handler. ClickprintButtonThe form is printed.

Reliable Programming

Exceptions may occur in the following situations:

  • You are not authorized to access the printer.
  • You are not authorized to use unmanaged code.
  • No printer is installed.
Security

To run this example, you must have the permission to run unmanaged code and access the printer.

This example shows how to change the existingPenThe color of the object.

Example
myPen.Color = System.Drawing.Color.PeachPuff;
Compile code

This example requires:

  • NameMypenOfPenObject.
Reliable Programming

You should alwaysPenObject) CallDispose.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.