1

I'm working on a graphic application in C# windows form app. I have a form that I can draw on it. so I created a Graphic object from the form.

void StartPoint()
{
    Graphics graphic;
    graphic = PaintWindow.CreateGraphics();
}

I want to know how can I export this graphic as a png or jpg file after drawing something. before this, I searched for this question but I didn't find any useful. some people resolve this with printing that part of the screen:

graphic.CopyFromScreen(...);

this way is not useful for me because some times I need to transparent my background image. also, I tried Bitmap way :

    private void ExportBTN_Click(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(PaintWindow.Width, PaintWindow.Height, graphic);
        b.Save(...);
    }

but when I save, the image file is completely black. this is my application: enter image description here

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Have you tried saving the bitmap with `PNG` format like this : `b.Save(fileName, System.Drawing.Imaging.ImageFormat.Png)` ? Or you can check the answer from [here](https://stackoverflow.com/a/8317271/8949476) – Software Dev Aug 27 '20 at 16:12
  • Do you get all black if you draw with any other color than black? (This test will tell us if it is an alpha-channel related problem.) – UML Aug 27 '20 at 16:18
  • yes , I did png format. I saw that answer before. – Ali Mohammadi Aug 28 '20 at 11:25

1 Answers1

0

If you want to draw to an image you want to create your graphics from that image:

using(var myGraphics = Graphics.FromImage(myBitmap))
{ 
    // Do drawing 
}

You can then proceed to to use CopyFromScreen and other drawing methods to update the image and then save it.

JonasH
  • 28,608
  • 2
  • 10
  • 23