C# Picturebox加载PNG,保存Picturebox背景色。

问题描述 投票:-1回答:1

我需要保存picturebox加载的png文件与picturebox背景颜色。

我已经用这些方法试过了。

但结果是显示为一个空白的图像。

帮助我...

private void bunifuThinButton24_Click(object sender, EventArgs e)
    {
        Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
        bmp1.Save(@"C:\Users\...\New folder4\ xx.jpg");
    }
c# colors background picturebox
1个回答
0
投票

Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics()); 创建一个新的空白位图图像,分辨率为pictureBox1的图形对象,你需要在这个位图中绘制你的图片框。你可以使用picturebox的DrawToBitmap函数来实现。

void bunifuThinButton24_Click(object sender, EventArgs e)
    {
        Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());

        // fill in bitmap with the picturebox's backcolor
        using (Graphics g = Graphics.FromImage(bmp1))
            {
                using (Brush drawBrush = new SolidBrush(pictureBox1.BackColor))
                {
                    g.FillRectangle(drawBrush, new Rectangle(0, 0, bmp1.Width, bmp1.Height));
                }
            }

        // draw picturebox on bitmap
        pictureBox1.DrawToBitmap(bmp1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));

        bmp1.Save(@"C:\Users\...\New folder4\ xx.jpg");
    }
© www.soinside.com 2019 - 2024. All rights reserved.