Bitmap.GetGraphics Method

Gets the Graphics object associated with this Bitmap.

Namespace: Aurigma.GraphicsMill
Assembly: Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)

Syntax

C#
public Graphics GetGraphics()

Return Value

A Graphics object associated with this Bitmap.

Remarks

Several calls of this method will return the same object.

You can freely dispose the Graphics returned with this method, although it is not necessary. If you dispose this graphics object, next call of the GetGraphics() method will create new instance of this class.

Note

GDI cannot handle bitmaps with an alpha channel when it draws anything on it. As the high byte of 4-byte color must be zero in GDI, each pixel drawn with GDI becomes transparent. So now, if you try to call any drawing methods for the bitmaps with an alpha channel when using GDI, the UnsupportedPixelFormatException exception will be thrown.

Examples

C#
using (var bitmap = new Bitmap(100, 60, PixelFormat.Format24bppRgb, RgbColor.White))
{
    using (Graphics graphics = bitmap.GetGraphics())
    {
        var bluePen = new Pen(RgbColor.Blue, 8);
        graphics.DrawLine(bluePen, 10, 55, 90, 5);

        var redPen = new Pen(RgbColor.Red, 8);
        graphics.DrawLine(redPen, 10, 5, 90, 55);

        bitmap.Save(@"Images\Output\pen.png");
    }
}

See Also

Reference