保存位图时背景变黑 - C# [英] Background Turns Black When Saving Bitmap - C#

查看:129
本文介绍了保存位图时背景变黑 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试保存位图图像,但背景变为黑色.

I am currently trying to save a bitmap image, but the background is changing to black.

我可以完美地另存为"图像.我也可以保存"图像.这要困难得多,因为我必须覆盖现有的图像.

I can "Save As" the image perfectly fine. I can also "Save" the image as well. Which was much more difficult because I had to overwrite the existing image.

但是,当我保存"我的图像时,背景变黑了.我不知道是什么原因造成的.

However, when I "save" my image the background is turning black. And I have no idea what is causing it.

这是我的代码:

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

推荐答案

创建一个空白位图.创建一个图形对象以使用该空白位图书写.清除位图并将其颜色更改为白色.然后绘制图像然后保存位图.

Create a blank bitmap. Create a graphics object to write on with that blank bitmap. Clear the bitmap and change its color to white. Then draw the 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();

这篇关于保存位图时背景变黑 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆