Chona Pardo
Chona Pardo

Reputation: 25

C# save System.Drawing.Graphics to .png

I tried with the graphics.Save();

Graphics newImage = Graphics.FromImage(bmp2);
newImage.DrawImage(bmp, 87, 37, 0, 0);
newImage.Save();

but when i try to set the file name like in the Image.Save(@"HereGoesName.PNG"); method i get an error saying "No overload for the method 'Save' takes 1 arguments)"

and everywhere i searched says to do the following

 Bitmap bitmap = new Bitmap(Convert.ToInt32(1024), Convert.ToInt32(1024), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 Graphics g = Graphics.FromImage(bitmap);
 bitmap.Save(@"HereGoesName.PNG", ImageFormat.Png);

From what i understand this is to create a graphics from a bitmap in here and i am trying to do the oposite.

Upvotes: 0

Views: 4401

Answers (2)

MaxKlaxx
MaxKlaxx

Reputation: 763

This is simple: Your line Graphics newImage = Graphics.FromImage(bmp2); creates a graphics object referring to bmp2 image.

All drawing actions draw direct on your bmp2 Bitmap

So you can simply save your modified image with:

bmp2.Save(@"foo.png", ImageFormat.Png);

Upvotes: 2

Matias Cicero
Matias Cicero

Reputation: 26331

You need to call Save on the bitmap, not the graphics:

Graphics newImage = Graphics.FromImage(bmp2);
newImage.DrawImage(bmp, 87, 37, 0, 0);
bmp2.Save("HereGoesName.PNG", ImageFormat.Png);

Upvotes: 4

Related Questions