Search code examples
c#visual-studiosystem.drawing.graphics

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.


Solution

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