示例#1
0
        /// <summary>
        /// Creates a copy of the given bitmap after rotation using the supplied intermediate resolution
        /// and Angle.  Positive Angle is Clockwise.  The resulting image has the same horizonatal
        /// and vertical resolution as the given page, but geometry may have changed (height and width).
        /// </summary>
        /// <param name="bmp">The Bitmap to rotate</param>
        /// <param name="intermediateResolution">The intermediate resolution to use.</param>
        /// <param name="rotationAngle">The angle to rotate the image</param>
        /// <returns></returns>
        public static Bitmap CreateCopyRotate(Bitmap bmp, float rotationAngle)
        {
            PageInfo bmpinfo = new PageInfo(bmp);

            PointF[] pts = new PointF[4];
            pts[0] = new PointF(0, 0);
            pts[1] = new PointF(bmpinfo.WidthInches, 0);
            pts[2] = new PointF(0, bmpinfo.HeightInches);
            pts[3] = new PointF(bmpinfo.WidthInches, bmpinfo.HeightInches);

            Matrix mx = new Matrix();

            mx.RotateAt(rotationAngle, new Point(0, 0), MatrixOrder.Append);
            mx.TransformPoints(pts);

            float width;
            float height;
            float dx;
            float dy;

            BitmapHelper.GetExtents(pts, out width, out height, out dx, out dy);

            //If the overall image is bigger than 5000 pixels in either direction, then lets scale it by adjusting the Intermediate resolution - We'll scale it to about 4000 px.
            float intermediateResolution = Math.Max(bmp.VerticalResolution, bmp.HorizontalResolution);

            if (Math.Max(bmpinfo.WidthPixels, bmpinfo.HeightPixels) > 5000)
            {
                intermediateResolution = (float)(int)(4000 / Math.Max(bmpinfo.HeightInches, bmpinfo.WidthInches));
            }

            mx.Translate((int)(dx * intermediateResolution), (int)(dy * intermediateResolution), MatrixOrder.Append);
            //Flip width and height
            Bitmap temp = BitmapHelper.CreateBitMap(width, height, intermediateResolution, intermediateResolution, PixelFormat.Format32bppArgb);
            {
                Graphics g = Graphics.FromImage(temp);
                g.ResetTransform();
                g.Transform = mx;
                g.DrawImage(bmp, new Point(0, 0));
                g.Dispose();
            }
            Bitmap ret = BitmapHelper.CreateBitMap(width, height, bmpinfo.HorizontalResolution, bmpinfo.VerticalResolution, PixelFormat.Format32bppArgb);

            {
                Graphics g = Graphics.FromImage(ret);
                g.ResetTransform();
                g.DrawImage(temp, new Point(0, 0));
                g.Dispose();
            }
            temp.Dispose();

            return(ret);
        }