-
c# rotate image
hello
the title says everything, i want to rotate an image but it dont rotate around his center it rotates in a circle around the form.
Code:
for(int i=0;i<500;i++)
{
Image m=Image.FromFile("C:/p1.png");
using(Graphics g=this.CreateGraphics())
{
g.TranslateTransform(m.Width/2,m.Height/2);
g.RotateTransform(i);
g.TranslateTransform(-m.Width/2,-m.Height/2);
g.DrawImage(m,new Point(100,100));
}
}
if I put in DrawImage(m,0,0) i works great but i dont want to rotate the image in the corner.
sorry for the english but I think you understud what I ask :)
-
This might help. Its using the Image class' RotateFlip method. Otherwise, I dont really understand what you are trying to do. But i also gave you a link to the RotateFlipType Enumeration members. Figure out which one you want to use.
Image.RotateFlip Method (System.Drawing)
http://msdn.microsoft.com/en-us/libr...pe(VS.71).aspx
-
You can use the RotateAt() method of a matrix if you want to rotate around a specific point.
Here's a quick example on how you might go about doing it for an image (based on the MSDN sample):
Code:
System.Drawing.Point imageLoc = new System.Drawing.Point(200, 200);
System.Drawing.Bitmap image = new System.Drawing.Bitmap("yourImage.png");
// Rotation in degrees.
int rot = 180;
protected override void OnPaint(PaintEventArgs e)
{
System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix();
// Rotates around the images top left + centre
mat.RotateAt(rot, new PointF(imageLoc.X + (image.Width / 2), imageLoc.Y + (image.Height / 2)));
e.Graphics.Transform = mat;
e.Graphics.DrawImage(image, imageLoc);
}
-
Quote:
Originally Posted by
wert
Code:
for(int i=0;i<500;i++)
g.TranslateTransform(m.Width/2,m.Height/2);
g.RotateTransform(i);
g.TranslateTransform(-m.Width/2,-m.Height/2);
That looks wrong. Shouldn't it be this?
Code:
g.TranslateTransform(-m.Width/2,-m.Height/2);
g.RotateTransform(i);
g.TranslateTransform(m.Width/2,m.Height/2);
-
problem solved
Code:
Image m = Image.FromFile(@"C:\p1.png");
using (Graphics gfx = this.CreateGraphics())
{
for (int i = 0; i <= 360; i++)
{
Bitmap b = new Bitmap(m.Width, m.Height);
using (Graphics g = Graphics.FromImage(b))
{
g.TranslateTransform(m.Width / 2, m.Height / 2);
g.RotateTransform(i);
g.TranslateTransform(-m.Width / 2, -m.Height / 2);
g.DrawImage(m, 0, 0);
}
gfx.DrawImage(b, 100, 100);
b.Dispose();
}
}