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

