Rotate image around the center itself

Asked

Viewed 191 times

3

I am developing a C# application and would like to know how to freely rotate a bitmap which is drawn in a pictureBox without rotating all _pictureBox_e so that the bitmap Rotacione around its own center. Follow my last attempt:

Bitmap irisref1;

private void pcbREFOLHOS_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.TranslateTransform(pcbREFOLHOS.Width / 2, pcbREFOLHOS.Height / 2);
    e.Graphics.RotateTransform(contador);
    e.Graphics.DrawImage(irisref1, new PointF((5 - atualdata1 - 2.5f) * 10, 0));
    contador++;
    //e.Graphics.DrawEllipse(new Pen(Color.White, 3), 0, 0, 150, 60);
}

In this way I did not obtain the expected result, because the image "orbits" around the specified point, but I would like the image to rotate around its own center. Can anyone help with this problem?

1 answer

1


Try implementing the following method:

public static Bitmap RotateImageN(Bitmap b, float angle)
{
    //Create a new empty bitmap to hold rotated image.
    Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
    //Make a graphics object from the empty bitmap.
    Graphics g = Graphics.FromImage(returnBitmap);
    //move rotation point to center of image.
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
    //Rotate.        
    g.RotateTransform(angle);
    //Move image back.
    g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
    g.DrawImage(b, 0,0,b.Width, b.Height);
    return returnBitmap;
}

Then you just need to provide the Bitmap and the angle you want to rotate the image, taking the result later.


More details on the issue Rotating image Around center C# available on Soen.

  • Thanks friend, it worked very well, however the image according to the runtime will overshadow. I am not able to stop it.

  • Strange, I was not aware of this "consequence". But this code doesn’t seem to me to be causing it... it has some code other than this to treat the image?

  • No, only the "pcbREFOLHOS Paint event".

  • And you still need this event? You can delete the content, right?

  • I was using the Paint event because the image will be rotating during the run time.

  • I tried to add a timer, to rotate and give "Invalidate()".

  • 1

    According to the indications on this issue of Soen Image in Picturebox Become Blurry While Rotating, should always use the original image, only changing the cumulative rotation. The Blur is due to an unavoidable effect of the rotation.

  • Now yes, 100% friend, thank you very much!!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.