コード例 #1
0
        /// <summary> Get skew angle of image, how much degree have to be rotated to its text will be straight </summary>
        /// <param name="bitmapSource">image for which will be skew angle computed</param>
        /// <returns>Angle, how much degrees is image rotated from normal position</returns>
        /// <remarks>This is only method that uses GDI+ and does not use BitmapSource</remarks>
        public static double GetDeskewAngle(BitmapSource bitmapSource)
        {
            double skewangle;

            using (MemoryStream ms = new MemoryStream())
            {
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                encoder.Save(ms);
                ms.Position = 0;

                System.Drawing.Bitmap bmpIn = new System.Drawing.Bitmap(ms);
                Deskew sk = new Deskew(bmpIn);
                skewangle = sk.GetSkewAngle();
                bmpIn.Dispose();
            }

            return(skewangle);
        }
コード例 #2
0
        /// <summary> Deskew the image (rotate), so its text will be straight </summary>
        /// <param name="bitmapSource">source image that will be deskewed</param>
        /// <returns>Rotated BitmapSource</returns>
        /// <remarks>This is only method that uses GDI+</remarks>
        public static BitmapSource DeskewImage(BitmapSource bitmapSource, double skewAngle)
        {
            System.Drawing.Bitmap bmpOut;
            BitmapSource          output;

            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            BitmapFrame      frame   = BitmapFrame.Create(bitmapSource);

            encoder.Frames.Add(frame);

            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                ms.Position = 0;

                System.Drawing.Bitmap bmpIn = new System.Drawing.Bitmap(ms);
                Deskew sk = new Deskew(bmpIn);
                bmpOut = sk.RotateImage(bmpIn, -skewAngle);

                bmpIn.Dispose();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                bmpOut.Dispose();

                output = LoadFullSize(ms);
            }
            //copy pixels to new bitmapSource because of strange .NET native memory leak
            int stride = output.PixelWidth * output.Format.BitsPerPixel / 8;

            byte[] bitmapArray = new byte[output.PixelHeight * stride];
            output.CopyPixels(bitmapArray, stride, 0);
            return(BitmapSource.Create(output.PixelWidth, output.PixelHeight, output.DpiX, output.DpiY, output.Format,
                                       output.Palette, bitmapArray, stride));
        }