Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-create-graphics-objects-for-drawing.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

8.2 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create Graphics Objects for Drawing Learn now to create a Graphic object that you need to draw lines and shapes, render text, or display and manipulate images with GDI+. 03/30/2017
csharp
vb
cpp
graphics [Windows Forms], creating
images [Windows Forms], creating
GDI+, creating images
162861f9-f050-445e-8abb-b2c43a918b8b

How to: Create Graphics Objects for Drawing

Before you can draw lines and shapes, render text, or display and manipulate images with GDI+, you need to create a xref:System.Drawing.Graphics object. The xref:System.Drawing.Graphics object represents a GDI+ drawing surface, and is the object that is used to create graphical images.

There are two steps in working with graphics:

  1. Creating a xref:System.Drawing.Graphics object.

  2. Using the xref:System.Drawing.Graphics object to draw lines and shapes, render text, or display and manipulate images.

Creating a Graphics Object

A graphics object can be created in a variety of ways.

To create a graphics object

PaintEventArgs in the Paint Event Handler

When programming the xref:System.Windows.Forms.PaintEventHandler for controls or the xref:System.Drawing.Printing.PrintDocument.PrintPage for a xref:System.Drawing.Printing.PrintDocument, a graphics object is provided as one of the properties of xref:System.Windows.Forms.PaintEventArgs or xref:System.Drawing.Printing.PrintPageEventArgs.

To obtain a reference to a Graphics object from the PaintEventArgs in the Paint event

  1. Declare the xref:System.Drawing.Graphics object.

  2. Assign the variable to refer to the xref:System.Drawing.Graphics object passed as part of the xref:System.Windows.Forms.PaintEventArgs.

  3. Insert code to paint the form or control.

    The following example shows how to reference a xref:System.Drawing.Graphics object from the xref:System.Windows.Forms.PaintEventArgs in the xref:System.Windows.Forms.Control.Paint event:

    Private Sub Form1_Paint(sender As Object, pe As PaintEventArgs) Handles _  
       MyBase.Paint  
       ' Declares the Graphics object and sets it to the Graphics object  
       ' supplied in the PaintEventArgs.  
       Dim g As Graphics = pe.Graphics  
       ' Insert code to paint the form here.  
    End Sub  
    
    private void Form1_Paint(object sender,
       System.Windows.Forms.PaintEventArgs pe)
    {  
       // Declares the Graphics object and sets it to the Graphics object  
       // supplied in the PaintEventArgs.  
       Graphics g = pe.Graphics;  
       // Insert code to paint the form here.  
    }  
    
    private:  
       void Form1_Paint(System::Object ^ sender,  
          System::Windows::Forms::PaintEventArgs ^ pe)  
       {  
          // Declares the Graphics object and sets it to the Graphics object  
          // supplied in the PaintEventArgs.  
          Graphics ^ g = pe->Graphics;  
          // Insert code to paint the form here.  
       }  
    

CreateGraphics Method

You can also use the xref:System.Windows.Forms.Control.CreateGraphics%2A method of a control or form to obtain a reference to a xref:System.Drawing.Graphics object that represents the drawing surface of that control or form.

To create a Graphics object with the CreateGraphics method

  • Call the xref:System.Windows.Forms.Control.CreateGraphics%2A method of the form or control upon which you want to render graphics.

    Dim g as Graphics  
    ' Sets g to a Graphics object representing the drawing surface of the  
    ' control or form g is a member of.  
    g = Me.CreateGraphics  
    
    Graphics g;  
    // Sets g to a graphics object representing the drawing surface of the  
    // control or form g is a member of.  
    g = this.CreateGraphics();  
    
    Graphics ^ g;  
    // Sets g to a graphics object representing the drawing surface of the  
    // control or form g is a member of.  
    g = this->CreateGraphics();  
    

Create from an Image Object

Additionally, you can create a graphics object from any object that derives from the xref:System.Drawing.Image class.

To create a Graphics object from an Image

  • Call the xref:System.Drawing.Graphics.FromImage%2A?displayProperty=nameWithType method, supplying the name of the Image variable from which you want to create a xref:System.Drawing.Graphics object.

    The following example shows how to use a xref:System.Drawing.Bitmap object:

    Dim myBitmap as New Bitmap("C:\Documents and Settings\Joe\Pics\myPic.bmp")  
    Dim g as Graphics = Graphics.FromImage(myBitmap)  
    
    Bitmap myBitmap = new Bitmap(@"C:\Documents and
       Settings\Joe\Pics\myPic.bmp");  
    Graphics g = Graphics.FromImage(myBitmap);  
    
    Bitmap ^ myBitmap = gcnew  
       Bitmap("D:\\Documents and Settings\\Joe\\Pics\\myPic.bmp");  
    Graphics ^ g = Graphics::FromImage(myBitmap);  
    

Note

You can only create xref:System.Drawing.Graphics objects from nonindexed .bmp files, such as 16-bit, 24-bit, and 32-bit .bmp files. Each pixel of nonindexed .bmp files holds a color, in contrast to pixels of indexed .bmp files, which hold an index to a color table.

Drawing and Manipulating Shapes and Images

After it is created, a xref:System.Drawing.Graphics object may be used to draw lines and shapes, render text, or display and manipulate images. The principal objects that are used with the xref:System.Drawing.Graphics object are:

To use the Graphics object you have created

See also