Thread: c# rotate image

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    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

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    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
    Last edited by stumon; 03-19-2010 at 10:19 AM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    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);
    }
    Last edited by HLMetroid; 03-19-2010 at 04:19 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by wert View Post
    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);
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    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();
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merge multiple image file into one image
    By mr_empty in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2009, 02:12 PM
  2. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  3. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  4. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM