Exemple #1
0
        public static void GenerateCompressImage(string sourceImagePath, string desImagePath, int compressionRate = 100)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(desImagePath));

            if (dirInfo.Exists == false)
            {
                dirInfo.Create();
            }

            if (new List <string> {
                ".PNG", ".GIF"
            }.Contains(Path.GetExtension(sourceImagePath).ToUpper()))
            {
                new FileInfo(sourceImagePath).CopyTo(desImagePath);
            }
            else
            {
                using (Bitmap imageFrom = new Bitmap(sourceImagePath))
                {
                    imageFrom.Save(desImagePath, PaintTools.GetEncoderByFormat(GetImageFormat(desImagePath)), PaintTools.GetEncoderParametersByNumber(compressionRate));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 生成缩略图,不留空白,等比缩放,边沿部分可能被裁切掉
        /// </summary>
        /// <param name="sourceImagePath"></param>
        /// <param name="desImagePath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="fileType">image/video</param>
        public static Tuple <int, int> GenerateImage2(string sourceImagePath, string desImagePath, int width, int height, string fileType = "image")
        {
            Bitmap imageFrom = new Bitmap(sourceImagePath);
            //Rotate180FlipNone   指定不进行翻转的 180 度旋转。
            //  Rotate180FlipX  指定后接水平翻转的 180 度旋转。
            //  Rotate180FlipXY 指定后接水平翻转和垂直翻转的 180 度旋转。
            //  Rotate180FlipY  指定后接垂直翻转的 180 度旋转。
            //  Rotate270FlipNone   指定不进行翻转的 270 度旋转。
            //  Rotate270FlipX  指定后接水平翻转的 270 度旋转。
            //  Rotate270FlipXY 指定后接水平翻转和垂直翻转的 270 度旋转。
            //  Rotate270FlipY  指定后接垂直翻转的 270 度旋转。
            //  Rotate90FlipNone    指定不进行翻转的 90 度旋转。
            //  Rotate90FlipX   指定后接水平翻转的 90 度旋转。
            //  Rotate90FlipXY  指定后接水平翻转和垂直翻转的 90 度旋转。
            //  Rotate90FlipY   指定后接垂直翻转的 90 度旋转。
            //  RotateNoneFlipNone  指定不进行旋转和翻转。
            //  RotateNoneFlipX 指定没有后跟水平翻转的旋转。
            //  RotateNoneFlipXY    指定没有后跟水平和垂直翻转的旋转。
            //  RotateNoneFlipY 指定没有后跟垂直翻转的旋转。
            // 源图宽度及高度
            int           imgSourceW = imageFrom.Width;
            int           imgSourceH = imageFrom.Height;
            EXIFextractor er         = new EXIFextractor(ref imageFrom, "\n");

            if (er.properties["Orientation"] != null && er.properties["Orientation"].ToString() == "6" || fileType == "video")//Orientation值为6即顺时针90
            {
                imgSourceW = imageFrom.Height;
                imgSourceH = imageFrom.Width;
                imageFrom.RotateFlip(RotateFlipType.Rotate90FlipNone);//Rotate270FlipNone
            }
            Tuple <int, int> wh = Tuple.Create(imgSourceW, imgSourceH);
            // 生成的缩略图在上述"画布"上的位置
            int X = 0;
            int Y = 0;

            int   cutW = 0, cutH = 0;
            float whPerSour = (float)imgSourceW / imgSourceH;
            float whPerDesc = (float)width / height;

            if (whPerSour >= whPerDesc)
            {
                cutH = imgSourceH;
                cutW = imgSourceH * width / height;
                X    = (imgSourceW - cutW) / 2;
            }
            else
            {
                cutW = imgSourceW;
                cutH = imgSourceW * height / width;
                Y    = (imgSourceH - cutH) / 2;
            }
            Bitmap bitmapCut = new Bitmap(width, height);

            if (Path.GetExtension(sourceImagePath).ToUpper().Contains("PNG"))
            {
                PaintTools.CutPhoto2(ref bitmapCut, imageFrom, new Rectangle(0, 0, width, height), X, Y, cutW, cutH);
            }
            else
            {
                PaintTools.CutPhoto(ref bitmapCut, imageFrom, new Rectangle(0, 0, width, height), X, Y, cutW, cutH);
            }
            bitmapCut.Save(desImagePath, PaintTools.GetEncoderByFormat(GetImageFormat(desImagePath)), PaintTools.GetEncoderParametersByNumber(95));
            imageFrom.Dispose();
            bitmapCut.Dispose();
            return(wh);
        }