C# Bitmap rotate with transparent image

Viewed 214

I tried two png images as one on another. Those are rotating through a code as follows.

private Bitmap RotateImage(Bitmap bmp, float angle)
        {
            Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
            rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

            using (Graphics g = Graphics.FromImage(rotatedImage))
            {
                g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
                g.RotateTransform(angle);
                g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
                g.DrawImage(bmp, new Point(0, 0));            }

            return rotatedImage;
        }

But, even select the background of the picture as transparent, shows the background color of the form as the background color of the image of the front image. I tried to add a code to paint control as

e.Graphics.DrawImage(bmp, 50, 50, 100, 100);

But when the function returns the new bitmap. It's not working. How do I do this? Current Result.

Here's the complete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap bmp;
        Bitmap bmp1;
        Bitmap bmp2;
        public float angle = 10;
        public float rangle = 0;
        PaintEventArgs e;

        private void btn_Rotate_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private Bitmap RotateImage(Bitmap bmp, float angle)
        {
            Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
            rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

            using (Graphics g = Graphics.FromImage(rotatedImage))
            {
                // Set the rotation point to the center in the matrix
                g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
                // Rotate
                g.RotateTransform(angle);
                // Restore rotation point in the matrix
                g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
                // Draw the image on the bitmap
                g.DrawImage(bmp, new Point(0, 0));

                bmp.MakeTransparent();
            }

            return rotatedImage;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // C:\Users\samsng\Desktop\left_right.png
            try
            {
                bmp = (Bitmap)Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Pics/123.png"));
                bmp1 = (Bitmap)Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Pics/3.png"));
                bmp2 = (Bitmap)Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Pics/4.png"));

                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox2.Image = bmp;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = bmp1;                
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox3.Image = bmp2;
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("File Not Found Exception");
            }
            lblNum.Visible = false;
        }
        int time = 01;
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = 1;
            if (bmp != null)
            {
                pictureBox1.Image = RotateImage(bmp1, angle);
                pictureBox3.Image = RotateImage(bmp2, rangle);
                pictureBox2.Image = RotateImage(bmp, angle);
                angle += 10;
                rangle -= 10;
            }
            i++;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lblNum.Visible = true;
            timer2.Start();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (time < 10)
            {
                lblNum.Text = "0" + time.ToString();
            }
            else
            {
                lblNum.Text = time.ToString();
            }
            time++;
            if (time == 99)
            {
                time = 01;
            }
        }

        private void pictureBox3_Paint(object sender, PaintEventArgs e)
        {
            //MessageBox.Show(e.ToString());
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.DrawImage(bmp1, 50, 50, 100, 100);
        }
    }
}
1 Answers

You have two PictureBox controls laid out as follows:

SOQ68916508A

Hosting controls by a PictureBox does not mean it becomes by default their parent because it is not a container control like a Panel or GroupBox. The parent of the hosted controls remains the parent of the hosting PictureBox itself which is the Form in your case. That's why the inner PictureBox fills the transparent area with the color of its Parent, the Form. Meaning, you need to explicit set the Parent property of the inner PictureBox to the outer one.

Here's the relevant code in your Form with the necessary changes.

public partial class Form1 : Form
{
    private Bitmap bmp, bmp1, bmp2;
    private float angle;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        try
        {
            bmp = Image.FromFile(Path.Combine(Application.StartupPath, 
                @"Pics\123.png")) as Bitmap;
            bmp1 = Image.FromFile(Path.Combine(Application.StartupPath, 
                @"Pics\3.png")) as Bitmap;
            bmp2 = Image.FromFile(Path.Combine(Application.StartupPath, 
                @"Pics\4.png")) as Bitmap;

            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.Image = new Bitmap(bmp);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = new Bitmap(bmp1);
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.Image = new Bitmap(bmp2);

            // The fix
            pictureBox3.Location = pictureBox1.PointToClient(pictureBox3
                .PointToScreen(Point.Empty));
            pictureBox3.Parent = pictureBox1;
        }
        catch (FileNotFoundException)
        {
            MessageBox.Show("File Not Found Exception");
        }
    }

    // You should clean up!
    protected override void OnFormClosed(FormClosedEventArgs e)
    {
        base.OnFormClosed(e);

        bmp?.Dispose();
        bmp1?.Dispose();
        bmp2?.Dispose();

        pictureBox1.Image?.Dispose();
        pictureBox2.Image?.Dispose();
        pictureBox3.Image?.Dispose();
    }

    private void btn_Rotate_Click(object sender, EventArgs e)
    {
        timer1.Enabled = !timer1.Enabled;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // You should dispose of the current images before setting new ones.
        pictureBox1.Image?.Dispose();
        pictureBox2.Image?.Dispose();
        pictureBox3.Image?.Dispose();

        pictureBox1.Image = RotateImage(bmp1, angle);
        pictureBox3.Image = RotateImage(bmp2, -angle);
        pictureBox2.Image = RotateImage(bmp, angle);
        angle += 10;
        if (angle >= 360) angle = 0;
    }

    private Bitmap RotateImage(Bitmap bmp, float angle)
    {
        Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
        rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
            g.RotateTransform(angle);
            g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
            g.DrawImage(bmp, Point.Empty);
        }

        return rotatedImage;
    }
}

Here's the result.

SOQ68431065B

Related