-1

I need to save picturebox loaded png file with picturebox backgroud colour.

I've try with these.

But result is showing as a blank image.

help me..

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");
    }
  • You need to draw it. Check [Replacing transparent background with white color in PNG images](https://stackoverflow.com/a/27318979/10216583) –  May 22 '20 at 17:01
  • pbox.DrawToBitmap will do just that. Or you can g.drawimage onto a fresh image cleared to someColor with g coming fromBitmap - Also: Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. – TaW May 22 '20 at 17:13
  • got it. thanks friends. – Ranga mahesh May 23 '20 at 04:55

1 Answers1

0

Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics()); creates a new blank bitmap image with the resolution of pictureBox1's graphics object. you need to draw your picturebox in this bitmap. You can do this using the picturebox's DrawToBitmap function.

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");
    }
Adam
  • 562
  • 2
  • 15