Beispiel #1
0
        public static void ClipAndSaveFile(string sourcePath, string savePath, string saveName, FileSaveInfo saveInfo)
        {
            if (!File.Exists(sourcePath))
            {
                throw new Exception("文件不存在!");
            }
            else
            {
                int               level = 100;
                ImageCodecInfo    ici   = ImageCodecInfo.GetImageEncoders().SingleOrDefault(c => c.MimeType == "image/jpeg");
                EncoderParameters ep    = new EncoderParameters();
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, level);

                System.Drawing.Image bitmap = null;
                System.Drawing.Image source = null;
                try
                {
                    bitmap = new System.Drawing.Bitmap(saveInfo.SaveWidth, saveInfo.SaveHeight);
                    source = System.Drawing.Image.FromFile(sourcePath);
                    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                    //设定缩略图的生成质量
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                    g.Clear(System.Drawing.Color.Transparent);

                    g.DrawImage(source, new System.Drawing.Rectangle(0, 0, saveInfo.SaveWidth, saveInfo.SaveHeight), new System.Drawing.Rectangle(saveInfo.StartLeft, saveInfo.StartTop, saveInfo.Width, saveInfo.Height), System.Drawing.GraphicsUnit.Pixel);

                    CreatePath(savePath);

                    bitmap.Save(Path.Combine(savePath, saveName), ici, ep);
                }
                catch (Exception ex)
                {
                    //保存缩略图出错处理
                    throw ex;
                }
                finally
                {
                    if (source != null)
                    {
                        source.Dispose();
                    }
                    if (bitmap != null)
                    {
                        bitmap.Dispose();
                    }
                }
            }
        }
Beispiel #2
0
        public static void ThumbAndSaveFile(string sourcePath, string savePath, string saveName, FileSaveInfo saveInfo)
        {
            if (!File.Exists(sourcePath))
            {
                throw new Exception("文件不存在!");
            }
            else
            {
                Image source = null, img = null;
                try
                {
                    source = System.Drawing.Image.FromFile(sourcePath);
                    float bl = Math.Min((float)saveInfo.Width / source.Width, (float)saveInfo.Height / source.Height);

                    img = source.GetThumbnailImage((int)(source.Width * bl), (int)(source.Height * bl), null, new IntPtr());

                    CreatePath(savePath);

                    img.Save(Path.Combine(savePath, saveName), ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (source != null)
                    {
                        source.Dispose();
                    }
                    if (img != null)
                    {
                        img.Dispose();
                    }
                }
            }
        }