GingerJack
GingerJack

Reputation: 3134

How to get Graphic's object with out using a control in c# using GDI+?

I need to create a drawing with a circle and text embeded to it .This drawing is outputted as Image file (Jpeg/Jpg/svg/Png) using c# GDI+ .

Since i dont want to display the UI directly on a form ,how do i get the graphic's object to start drawing.

Thanks in advance.

Upvotes: 1

Views: 659

Answers (2)

Jamiec
Jamiec

Reputation: 136154

You create a Bitmap, and get the graphics object from it to draw on:

Bitmap myBitmap = new Bitmap(@"C:\MyImg.bmp");
Graphics g = Graphics.FromImage(myBitmap);

Note that the Bitmap does not need to be created on disk, it can be created in memory too!

Upvotes: 3

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

You can create a new Bitmap passing the size for the image:

using (Bitmap myBitmap = new Bitmap(100, 100)) {
    using (Graphics g = Graphics.FromImage(myBitmap)) {
        // do your drawing here
    }

    myBitmap.Save(@"C:\path\for\image.bmp");
}

Optionally you can set the ImageFormat for the image when saving

myBitmap.Save(@"C:\path\for\image.png", ImageFormat.Png);

Upvotes: 4

Related Questions