Saving image to file

前端 未结 4 2031
花落未央
花落未央 2020-12-05 13:08

I am working on a basic drawing application. I want the user to be able to save the contents of the image.

\"ent

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 13:59

    If you are drawing on the Graphics of the Control than you should do something draw on the Bitmap everything you are drawing on the canvas, but have in mind that Bitmap needs to be the exact size of the control you are drawing on:

      Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width,myControl.ClientRectangle.Height);
      Graphics gBmp = Graphics.FromImage(bmp);
      gBmp.DrawEverything(); //this is your code for drawing
      gBmp.Dispose();
      bmp.Save("image.png", ImageFormat.Png);
    

    Or you can use a DrawToBitmap method of the Control. Something like this:

    Bitmap bmp = new Bitmap(myControl.ClientRectangle.Width, myControl.ClientRectangle.Height);
    myControl.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width,bmp.Height));
    bmp.Save("image.png", ImageFormat.Png);
    

提交回复
热议问题