private Bitmap TransformImage()
{
//Get the scale & rotate transform matrix
Matrix matrix = GetTrasformationMatrix();
//Get an array of the bitmap boundaries after the matrix transformation
PointF[] bitmapRect = GetBitmapBoundary(matrix);
float minX = FindMinX(bitmapRect);
float minY = FindMinY(bitmapRect);
float maxX = FindMaxX(bitmapRect);
float maxY = FindMaxY(bitmapRect);
RectangleF boundRect = new RectangleF(
0, 0, Math.Abs(maxX - minX), Math.Abs(maxY - minY));
//create a new empty bitmap to hold rotated image
Bitmap transformedBitmap = new Bitmap((int)boundRect.Width, (int)boundRect.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(transformedBitmap);
g.TranslateTransform(-minX, -minY);
//For debugging:
//g.FillRectangle(Brushes.Azure, g.VisibleClipBounds);
//g.DrawPolygon(new Pen(Color.Red, 10), bitmapRect);
g.MultiplyTransform(matrix);
//draw the image onto transformed graphics
g.DrawImage(_bitmap, new PointF(0,0));
return transformedBitmap;
}