public bool RotateImage( string sourceImagePath, int rotationDegree )
        {
            try
            {
                string fileExt = Path.GetExtension( sourceImagePath ).ToLowerInvariant().Trim( '.' );
                // create file name of the temp file
                string tempFilePath = getTempFileName( sourceImagePath, fileExt );

                using( Bitmap inputImage = new Bitmap( sourceImagePath ) )
                {
                    EncoderParameters encParams = null;
                    var encoder = getEncoder( fileExt, out encParams );

                    // calculate width and height of the new image
                    float iW = (float)inputImage.Width;
                    float iH = (float)inputImage.Height;

                    Matrix whRotation = new Matrix();
                    whRotation.Rotate( rotationDegree );
                    // rotate every vertex of our "image rectangle"
                    var tmpDims = new PointF[] { new PointF(0,0), new PointF( iW, 0 ), new PointF( iW, iH ), new PointF( 0, iH ) };
                    whRotation.TransformVectors( tmpDims );
                    // find extends
                    iW = Math.Abs( tmpDims.Max( x => x.X ) - tmpDims.Min( x => x.X ) );
                    iH = Math.Abs( tmpDims.Max( x => x.Y ) - tmpDims.Min( x => x.Y ) );

                    using( Bitmap tempBmp = new Bitmap( (int)Math.Ceiling( iW ), (int)Math.Ceiling( iH ) ) )
                    {
                        // rotate image
                        tempBmp.SetResolution( inputImage.HorizontalResolution, inputImage.VerticalResolution );
                        using( Graphics g = Graphics.FromImage( tempBmp ) )
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                            // rotate at the center
                            g.TranslateTransform( tempBmp.Width/2, tempBmp.Height/2 );
                            g.RotateTransform( rotationDegree );
                            g.TranslateTransform( -tempBmp.Width / 2, -tempBmp.Height / 2 );
                            g.DrawImage( inputImage,
                                new Point( ( tempBmp.Width - inputImage.Width ) / 2,
                                    ( tempBmp.Height - inputImage.Height ) / 2 ) );
                        }
                        tempBmp.Save( tempFilePath, encoder, encParams );
                    }
                }
                // now replace images
                File.Delete( sourceImagePath );
                File.Move( tempFilePath, sourceImagePath );
                return true;
            }
            catch
            {
                return false;
            }
        }
Exemple #2
0
        private PointF[] ScaleGesture(PointF[] Input, int Width, int Height)
        {
            // Create generic list of points to hold scaled stroke
            List<PointF> ScaledStroke = new List<PointF>();

            // Get total width and height of gesture
            float fGestureOffsetLeft = Input.Min(i => i.X);
            float fGestureOffsetTop = Input.Min(i => i.Y);
            float fGestureWidth = Input.Max(i => i.X) - fGestureOffsetLeft;
            float fGestureHeight = Input.Max(i => i.Y) - fGestureOffsetTop;

            // Get each scale ratio
            double dScaleX = (double)Width / fGestureWidth;
            double dScaleY = (double)Height / fGestureHeight;

            // Scale on the longest axis
            if (fGestureWidth >= fGestureHeight)
            {
                // Scale on X axis
                // Clear current scaled stroke
                ScaledStroke.Clear();

                foreach (PointF currentPoint in Input)
                    ScaledStroke.Add(new PointF((float)((currentPoint.X - fGestureOffsetLeft) * dScaleX), (float)((currentPoint.Y - fGestureOffsetTop) * dScaleX)));

                // Calculate new gesture width and height
                _ScaledSize = new Size((int)Math.Floor(fGestureWidth * dScaleX), (int)Math.Floor(fGestureHeight * dScaleX));
            }
            else
            {
                // Scale on X axis
                // Clear current scaled stroke
                ScaledStroke.Clear();

                foreach (PointF currentPoint in Input)
                    ScaledStroke.Add(new PointF((float)((currentPoint.X - fGestureOffsetLeft) * dScaleY), (float)((currentPoint.Y - fGestureOffsetTop) * dScaleY)));

                // Calculate new gesture width and height
                _ScaledSize = new Size((int)Math.Floor(fGestureWidth * dScaleY), (int)Math.Floor(fGestureHeight * dScaleY));
            }

            return ScaledStroke.ToArray();
        }
Exemple #3
0
 private static float FindMinX(PointF[] bitmapRect)
 {
     return bitmapRect.Min(p => p.X);
 }