2014.3.3 图像旋转方法

方法一、利用命令行调用外部程序jpegr.exe

该方法效果最好,弊端是需要外带程序

Command Line

 

JPEG Lossless Rotator allows you to use command line for image transformation. The command format is:

 <Path to jpegr.exe> jpegr.exe -[r|l|u|v|h] [-s] <filename>

?r — to rotate the image right

?l — to rotate the image left

?u — to rotate the image 180°

?h — to flip the image horizontally

?v — to flip the image vertically

?s — to suppress all warning messages

 

Example (Command prompt screenshot):

 

c:\>jpegr.exe -l -s 2.jpg

 

 

Use quotes 引号 if your JPEG Lossless Rotator program file or image file paths contain spaces (see example in the next paragraph).

 

You can also run Automatic Rotation function using command line. The command format is:

 <Path to jpegr.exe> jpegr.exe -auto <Path to a folder with JPEG files>

 To include all subfolders, type -autosub instead of -auto. The program outputs messages as message boxes (not in command prompt), so you can use any command line, like “Run...” in Windows Start menu, by typing "C:\Program Files\JPEG Lossless Rotator\jpegr.exe" -auto "D:\My Photos\", for example.

 

Paths with white spaces may require to be placed in double quotes.

 

方法二、C#代码

//该代码有轻微图片差异,但所有信息都能保存

 

if (picpath == "") return;

 

            string Filename = picpath;

 

            string NewDescription = "";

            byte[] bDescription = new Byte[NewDescription.Length];

           

            // copy description into byte array

            for (int i = 0; i < NewDescription.Length; i++) bDescription[i] = (byte)NewDescription[i];

           

            string FilenameTemp;

            System.Drawing.Imaging.Encoder Enc = System.Drawing.Imaging.Encoder.Transformation;

            EncoderParameters EncParms = new EncoderParameters(1);

            EncoderParameter EncParm;

 

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            ImageCodecInfo ici = null;

            foreach (ImageCodecInfo codec in codecs)

            {

                if (codec.MimeType == "image/jpeg")

                    ici = codec;

            }

           

 

            // load the image to change

            Image Pic = Image.FromFile(Filename);

 

            // put the new description into the right property item

            PropertyItem[] PropertyItems= Pic.PropertyItems;

           

            PropertyItems[0].Id = 0x010e; // 0x010e as specified in EXIF standard

            PropertyItems[0].Type = 2;

            PropertyItems[0].Len = NewDescription.Length;

            PropertyItems[0].Value = bDescription;

           

            //如果要更改图像属性就将下行放开

            //Pic.SetPropertyItem(PropertyItems[0]);

 

            // we cannot store in the same image, so use a temporary image instead

            FilenameTemp = Filename + ".temp";

 

            // for lossless rewriting must rotate the image by 90 degrees!

            long Rotate = (long)EncoderValue.TransformRotate90;

            if (rotdir == "R") Rotate = (long)EncoderValue.TransformRotate270;

 

            EncParm = new EncoderParameter(Enc, Rotate);

            EncParms.Param[0] = EncParm;

 

            // now write the rotated image with new description

            Pic.Save(FilenameTemp, ici, EncParms);

 

            pictureBox1.Image.Dispose();

            pictureBox1.Image = Pic;

 

            try

            {

                System.IO.File.Delete(Filename);

                System.IO.File.Move(FilenameTemp, Filename);

            }

            catch

            {

                System.IO.File.Delete(FilenameTemp);

                MessageBox.Show(Filename+"正在使用中,无法替换成旋转后图片。");               

            }

 

//任意角度旋转图片,有损失

public Image KiRotate( float angle)

        {

            if (picpath == "") return null;

 

            Bitmap bmp = (Bitmap)Image.FromFile(picpath);

            int newW = bmp.Width;

            int newH = bmp.Height;

 

            //旋转

            angle = angle % 360;

 

            //弧度转换

            double radian = angle * Math.PI / 180.0;

            double cos = Math.Cos(radian);

            double sin = Math.Sin(radian);

 

            //原图的宽和高

            int w = newW;

            int h = newH;

            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));

            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

 

            //目标位图

            Bitmap dsImage = new Bitmap(W, H);

            dsImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

            Graphics g = Graphics.FromImage(dsImage);

            //g.InterpolationMode = InterpolationMode.Default;

            //g.SmoothingMode = SmoothingMode.Default;

 

            //计算偏移量

            Point Offset = new Point((W - w) / 2, (H - h) / 2);

 

            //构造图像显示区域:让图像的中心与窗口的中心点一致

            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);

            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);

 

            g.TranslateTransform(center.X, center.Y);

            g.RotateTransform(360 - angle);

 

            //恢复图像在水平和垂直方向的平移

            g.TranslateTransform(-center.X, -center.Y);

            g.DrawImage(bmp, rect);

 

            //重至绘图的所有变换

            g.ResetTransform();

            g.Save();

            g.Dispose();

            //保存旋转后的图片

            bmp.Dispose();

            pictureBox1.Image.Dispose();

 

            // ===处理JPG质量的函数===

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            ImageCodecInfo ici = null;

            foreach (ImageCodecInfo codec in codecs)

            {

                if (codec.MimeType == "image/jpeg")

                    ici = codec;

            }

 

            EncoderParameters ep = new EncoderParameters();

            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

 

 

            //存到流

            //MemoryStream ms = new MemoryStream();

            //newImage.Save(ms, ici, ep);

 

            //存到文件

            File.Delete(picpath);

            dsImage.Save(picpath, ici, ep);

 

            return dsImage;

       

       

        }

 

posted on 2016-10-15 18:33  mol1995  阅读(326)  评论(0编辑  收藏  举报

导航