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.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.iplRotate()
not giving me correct results?. I ended giving up on IPL/IPP and trying GDI+.Any ideas?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With