C# save System.Drawing.Graphics to .png

4k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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);
0
On

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);