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?