How to get the bitmap/image from a Graphics object in C#?

前端 未结 7 1649
逝去的感伤
逝去的感伤 2021-02-20 07:34

I want to know what the intermediate state of the buffer where the Graphics object is drawing some stuff. How do I get hold of the bitmap or the image that it is drawing on?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 08:02

    Not 100% sure what you want here, but if you want to use the graphics class to draw, and then save to file, you have to obtain the Graphics object from a Bitmap file, and then save the bitmap after you are done. You can do that like this:

      Bitmap bitmap = new Bitmap(bWidth, bHeight);
      Graphics g = Graphics.FromImage(bitmap);
      //do all your operations on g here.
      bitmap.Save(fileName, imageFormat);
    

提交回复
热议问题