Rotating an image
-
Sunday, April 29, 2007 3:00 PM
Fellows:
hi,
Consider a small image inside a picture box. ( not greater than 40x40 pixels)
How can I rotate this image and load it again in the same picbox?
The code I need is just for the rotating, the simplest code as possible to save runtime.
Like this:
Code Snippetbutton1_click(System::Object ^ sender, System::EventArgs^ e)
{
rotate 90 degrees
}
button2_click(System::Object ^ sender, System::EventArgs^ e)
{
rotate 180 degrees
}
button3_click(System::Object^ sender, System::EventArgs^ e)
{
rotate -90 degrees
}
button4_click(System::Object^ sender, System::EventArgs^ e)
{
rotate XXXXX degress
}
cordially,
All Replies
-
Sunday, April 29, 2007 9:22 PMModerator
I'll give you a C# sample that demonstrates the basic technique. Key ideas is that you don't use a PB but use the Paint event to show the image and use the Graphics.RotateTransform() method to rotate the image. Drop a Timer control on the form first, then paste this code. Alter the path to the image.
public partial class Form1 : Form {
private float mDegrees;
private Image mBmp;
public Form1() {
InitializeComponent();
mBmp = Bitmap.FromFile(@"c:\temp\test1.bmp");
timer1.Enabled = true;
timer1.Interval = 50;
timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.DoubleBuffered = true; // prevent flicker
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
private void timer1_Tick(object sender, EventArgs e) {
mDegrees += 3.0F;
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
int center = (int)Math.Sqrt(mBmp.Width* mBmp.Width + mBmp.Height* mBmp.Height);
e.Graphics.TranslateTransform(center, center);
e.Graphics.RotateTransform(mDegrees);
e.Graphics.DrawImage(mBmp, 0, 0);
}
}
If you really want to use a PictureBox, you'd use a temporary bitmap to make the transformation. -
Monday, April 30, 2007 2:18 PM
Okay,
I got it,
tks,
over();
-
Monday, April 07, 2008 4:18 PM
This actually works for this specific problem but what would be the code to rotate the bitmap (not the graphics object) ?. I display some images in the same graphics object and i want to rotate only one (or some) of them.
Can you help please?
-
Monday, April 07, 2008 5:58 PM
My answer to your question is move to OpenCV, all this activities of rotating, flipping, mirroring and related are already done and you save time.
BUT,
Before you decide, listen to more answers, specially the moderators' replies, and you increase your knowledge about Visual C++ programming.

