Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a rotated image using GDI+?

I'm trying to center a rotated image into a destination buffer using GDI+. The source buffer and the destination buffer are different sizes.

The source buffer is the size of the image data: (width, height).
The destination buffer is the size of the rectangle required to fit the entire rotated image: (rotatedWidth, rotatedHeight).

This is the code I'm trying:

// Calculate the size needed for the image to be rotated into   
int width = /* some width */;
int height = /* some height */;
System::Windows::Size destSize 
  = IPMathUtilities::CalculateRotatedImageSize(rotateAreaBoundingPoints, 
    rotationDegree, width, height);

//
// Create source bitmap object
Bitmap^ sourceBitmap = gcnew Bitmap(width, height, width * 2, 
  PixelFormat::Format16bppRgb555, ptrSourceBuffer);

//
// Create destination bitmap object
int destBufferSize = destSize.Width * destSize.Height * 2;
BYTE* pDestBuffer = new BYTE[destBufferSize];
memset(pDestBuffer, 0, destBufferSize);
Bitmap^ destBitmap = gcnew Bitmap(destSize.Width, destSize.Height, 
  destSize.Width * 2, PixelFormat::Format16bppRgb555, (IntPtr)pDestBuffer);

//
// Draw rotated source image in destination image
Graphics^ g = Graphics::FromImage(destBitmap);
g->TranslateTransform(-width/2, -height/2);
g->RotateTransform(rotationDegree, MatrixOrder::Append);
g->TranslateTransform(destSize.Width / 2.0, destSize.Height / 2.0, 
  MatrixOrder::Append);
g->DrawImage(sourceBitmap, 0, 0, width, height);

This almost works. It's close - I'm finding if height is greater than width, the left position of the rotated image is incorrect. Similarly, if width is greater than height, the top position of the rotated image is incorrect.

Some notes:

  • IPMathUtilities is a utility class I wrote.
  • I'm 100% positive that IPMathUtilities::CalculateRotatedImageSize() calculates the correct size of the rectangle required to fit the entire rotated image. 100% certain. I've tested it thoroughly and it works.
  • I recently asked a similar question: Why is iplRotate() not giving me correct results?. I ended giving up on IPL/IPP and trying GDI+.

Any ideas?

like image 372
Rob Avatar asked May 28 '09 18:05

Rob


1 Answers

After the rotate, first move the image back to the original position, then additionally by half the difference the new canvas is larger. If your calculation in CalculateRotatedImageSize is indeed correct, it should then fit exactly. Just tested this code and it seems to work:

g.TranslateTransform((float)(org.Width / -2), (float)(org.Height / -2));
g.RotateTransform(45, System.Drawing.Drawing2D.MatrixOrder.Append );
g.TranslateTransform((float)(org.Width / 2), (float)(org.Height / 2), System.Drawing.Drawing2D.MatrixOrder.Append);
g.TranslateTransform((float)((rotated.Width - org.Width) / 2), (float)((rotated.Height - org.Height) / 2), System.Drawing.Drawing2D.MatrixOrder.Append);

EDIT: sorry, of course

g.TranslateTransform((float)(org.Width / 2), (float)(org.Height / 2), System.Drawing.Drawing2D.MatrixOrder.Append);
g.TranslateTransform((float)((rotated.Width - org.Width) / 2), (float)((rotated.Height - org.Height) / 2), System.Drawing.Drawing2D.MatrixOrder.Append);

is really the same as

g.TranslateTransform((float)(rotated.Width / 2), (float)(rotated.Height / 2), System.Drawing.Drawing2D.MatrixOrder.Append);

which is just the code you posted. Seems to work fine for me though.

EDIT2: perhaps the error is just

g->DrawImage(sourceBitmap, 0, 0, width, height);

Try

g->DrawImage(sourceBitmap, 0, 0);

instead

like image 67
Ben Schwehn Avatar answered Oct 15 '22 01:10

Ben Schwehn