Background turns black while saving a bitmap - C #

I'm currently trying to save a bitmap, but the background is changing to black.

I can "Save As" image perfectly. I can also “Save” the image. It was a lot more complicated because I had to rewrite the existing image.

However, when I “save” my image, the background turns black. And I have no idea what this causes.

Here is my code:

Bitmap tempImage = new Bitmap(DrawArea); DrawArea.Dispose(); if (extension == ".jpeg") tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); else tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); DrawArea = new Bitmap(tempImage); pictureBox1.Image = DrawArea; tempImage.Dispose(); 
+6
source share
2 answers

Create an empty bitmap. Create a graphic to record with this empty bitmap. Clear the bitmap and change its color to white. Then draw an image, then save the bitmap.

  Bitmap blank = new Bitmap(DrawArea.Width, DrawArea.Height); Graphics g = Graphics.FromImage(blank); g.Clear(Color.White); g.DrawImage(DrawArea, 0, 0, DrawArea.Width, DrawArea.Height); Bitmap tempImage = new Bitmap(blank); blank.Dispose(); DrawArea.Dispose(); if (extension == ".jpeg") tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); else tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); DrawArea = new Bitmap(tempImage); pictureBox1.Image = DrawArea; tempImage.Dispose(); 
+21
source

Try saving the image in PNG format, not JPEG.

-1
source

Source: https://habr.com/ru/post/902692/


All Articles