Graphics Class
This page is specific to:.NET Framework Version:
.NET Framework Class Library
Graphics Class

Encapsulates a GDI+ drawing surface. This class cannot be inherited.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
Syntax

'Usage

Dim instance As Graphics

'Declaration

Public NotInheritable Class Graphics _
    Inherits MarshalByRefObject _
    Implements IDeviceContext, IDisposable
Remarks

The Graphics class provides methods for drawing objects to the display device. A Graphics is associated with a specific device context.

You can obtain a Graphics object by calling the Control..::.CreateGraphics method on an object that inherits from System.Windows.Forms..::.Control, or by handling a control's Control..::.Paint event and accessing the Graphics property of the System.Windows.Forms..::.PaintEventArgs class. You can also create a Graphics object from an image by using the FromImage method. For more information about creating a Graphics object, see How to: Create Graphics Objects for Drawing.

You can draw many different shapes and lines by using a Graphics object. For more information about how to draw lines and shapes, see the specific DrawGraphicalElement method for the line or shape you want to draw. These methods include DrawLine, DrawArc, DrawClosedCurve, DrawPolygon, and DrawRectangle. For more information about how to draw lines and shapes, see Using a Pen to Draw Lines and Shapes and Using a Brush to Fill Shapes.

You can also draw images and icons by using the DrawImage and DrawIcon methods, respectively. For more information about how to draw images with a Graphics object, see Working with Images, Bitmaps, Icons, and Metafiles.

Examples

The following code example is designed for use with Windows Forms and requires a PaintEventArgs object. The PaintEventArgs object is named e and is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file. The file is named SampImag.jpg and is located in the folder of the example.

  • Creates a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen by using a Graphics object.

Private Sub DrawImagePointF(ByVal e As PaintEventArgs)

    ' Create image.
    Dim newImage As Image = Image.FromFile("SampImag.jpg")

    ' Create point for upper-left corner of image.
    Dim ulCorner As New PointF(100.0F, 100.0F)

    ' Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner)
End Sub


Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.Drawing..::.Graphics
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Other Resources

Community Content

[Graphics.Save()] what ?
Added by:NeuroCypher
Hello,

We don't know what is the "State" of graphics.
In the example we understand the restore method set the transform again but it does not restore the graphics (the red rectangle is kept).

It should be useful to know what properties the Save methode saves.
How to convert Graphics to bitmap?
Added by:BenHead

How to convert Graphics to bitmap, for example:

Dim b As Bitmap
Dim g As Graphics
b = g.???

[BenHead - 12-Aug-2009] Graphics will draw directly on the Bitmap, so your next line would be:

g = Graphics.FromImage(b)

...then any drawing operations you perform on "g" will be reflected on "b". No further assignment is required.

[tfl - 09 06 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at 
  http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Coordinate limits
Added by:vernarim
It seems to me that there is a limit on which you may use as input coordinates.
Among several tests, I've found that you shouldn't use values over 0x7FFFFF and under -0x7FFFFF. In fact, it seems the negative way more "tolerant" than the positive one.
By using 0x800000, some function gets crazy and draws something unexpected.
By going over the upper limit, the value is interpreted as negative.
Here is a small Windows.Forms test that emphasize the behavior:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

//try here also 0x800000 and above
int limit = 0x7FFFFF;

//this draws always correct having a reasonably small spike-height.
//keep it as reference.
Form1.DrawSpike(
e.Graphics,
Color.DarkGreen,
10,
150);

//this should draw the spike downward
Form1.DrawSpike(
e.Graphics,
Color.Blue,
160,
limit);

//this should draw the spike upward
Form1.DrawSpike(
e.Graphics,
Color.Red,
310,
-limit);
}



/**
* The expected behavior of this routine is
* to draw both a spike and a rectangle.
******************************************/
private static void DrawSpike(
Graphics g,
Color color,
int offset,
int spike)
{
var height = (int)Math.Abs(spike);

using (var pen = new Pen(Color.Red, 2f))
{
//draws an upsided-down spike, whose height is specified
g.DrawLine(pen, offset, 100, offset + 50, spike);
g.DrawLine(pen, offset + 50, spike, offset + 100, 100);

//draws a spike-centered rectangle
g.DrawRectangle(pen, offset + 40, 100 - height / 2, 20, height);
}
}
}

All that should arise from the native library GDI+.
© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View