예제 #1
0
        /// <summary>
        /// 生成缩略图重载方法4,返回缩略图的Image对象
        /// 必须先调用SetConstructer
        /// </summary>
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool GetReducedImage(double Percent, string targetFilePath)
        {
            try
            {
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ImageWidth  = Convert.ToInt32(ResourceImage.Width * Percent);
                ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);

                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);

                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);

                ReducedImage.Dispose();

                return(true);
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return(false);
            }
        }
예제 #2
0
        //<summary>
        //生成缩略图重载方法2,将缩略图文件保存到指定的路径
        //</summary>.
        //<param name=”Width”>缩略图的宽度</param>
        //<param name=”Height”>缩略图的高度</param>
        //<param name=”targetFilePath”>缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>
        //<returns>成功返回true,否则返回false</returns>
        public static bool GetReducedImage(int Width, int Height, string sourcePath, string targetFilePath)
        {
            try
            {
                Image ResourceImage = Image.FromFile(sourcePath);
                #region 过滤掉很小不用做缩略图的图片
                if (ResourceImage.Width < Width)
                {
                    ResourceImage.Save(@targetFilePath, ImageFormat.Jpeg);
                    if (System.IO.File.Exists(@targetFilePath))
                    {
                        File.SetAttributes(@targetFilePath, FileAttributes.Hidden);
                    }
                    ResourceImage.Dispose();
                    return(true);
                }
                #endregion
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(() => { return(false); });
                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                ReducedImage.Dispose();
                ResourceImage.Dispose();
                if (System.IO.File.Exists(@targetFilePath))
                {
                    File.SetAttributes(@targetFilePath, FileAttributes.Hidden);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #3
0
        /// <summary>   
        /// 生成缩略图重载方法4,返回缩略图的Image对象   
        /// </summary>   
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>     
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>   
        /// <returns>成功返回true,否则返回false</returns>   
        public static bool GetReducedImage(string sourceFilePath, double Percent, string targetFilePath)
        {
            try
            {
                var image = Image.FromFile(sourceFilePath);

                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                var width  = Convert.ToInt32(image.Width * Percent);
                var height = Convert.ToInt32(image.Width * Percent);

                ReducedImage = image.GetThumbnailImage(width, height, callb, IntPtr.Zero);

                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);

                ReducedImage.Dispose();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
예제 #4
0
        /// <summary>
        /// 按照固定宽度来缩放
        /// </summary>
        /// <param name="sourceFilePath"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <param name="targetFilePath"></param>
        /// <returns></returns>
        public static bool GetReducedImage(string sourceFilePath, int width, string targetFilePath)
        {
            try
            {
                var image = Image.FromFile(sourceFilePath);

                Image ReducedImage;

                var height = 0;

                if (image.Width > image.Height)
                {
                    height = Convert.ToInt32((image.Height * 1.0) / image.Width * width);
                }
                else
                {
                    height = width;
                    width  = Convert.ToInt32(image.Width * 1.0 / image.Height * width);
                }

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ReducedImage = image.GetThumbnailImage(width, height, callb, IntPtr.Zero);

                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);

                ReducedImage.Dispose();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
예제 #5
0
 public bool GetReducedImage(double Percent, string targetFilePath)
 {
     try
     {
         Bitmap   bt = new Bitmap(120, 120);                             //创建Bitmap实例
         Graphics g  = Graphics.FromImage(bt);                           //创建Graphics实例
         g.Clear(Color.White);                                           //设置画布背景颜色为白色
         Image ReducedImage;                                             //缩略图
         Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
         ImageWidth  = Convert.ToInt32(ResourceImage.Width * Percent);   //设置宽度
         ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);  //设置高度
         //获取所谓图
         ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
         if (ImageWidth > ImageHeight)                                                   //如果原图宽度大于高度
         {
             //缩放图片
             g.DrawImage(ReducedImage, 0, (int)(120 - ImageHeight) / 2, ImageWidth, ImageHeight);
         }
         else
         {
             g.DrawImage(ReducedImage, (int)(120 - ImageWidth) / 2, 0, ImageWidth, ImageHeight);
         }
         g.DrawRectangle(new Pen(Color.Gray), 0, 0, 119, 119);                   //绘制缩略图的边框
         bt.Save(@targetFilePath, ImageFormat.Jpeg);                             //保存缩略图
         bt.Dispose();                                                           //释放对象
         ReducedImage.Dispose();                                                 //释放对象
         return(true);
     }
     catch (Exception e)
     {
         ErrMessage = e.Message;
         return(false);
     }
 }
예제 #6
0
 /// <summary>
 /// 按大小压缩
 /// </summary>
 /// <param name="Width">压缩宽</param>
 /// <param name="Height">压缩高</param>
 /// <param name="targetFilePath">要压缩的图片路径</param>
 /// <returns>返回压缩后的图片路径</returns>
 public static string ReducedImage(int Width, int Height, string targetFilePath)
 {
     using (Image ResourceImage = Image.FromFile(targetFilePath))
     {
         Image ReducedImage;
         Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(delegate { return(false); });
         ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);                                            //按大小压缩
         string newImagePath = targetFilePath.Substring(0, targetFilePath.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + ".jpg"; //压缩图片的存储路径
         ReducedImage.Save(newImagePath, ImageFormat.Jpeg);                                                                            //保存压缩图片
         ReducedImage.Dispose();
         return(newImagePath);                                                                                                         //返回压缩图片的存储路径
     }
 }
예제 #7
0
 // 方法1,按大小
 /// <summary>
 /// 按大小缩放图片
 /// </summary>
 /// <param name="Width">缩放到的宽</param>
 /// <param name="Height">缩放到的高</param>
 /// <param name="targetFilePath">图片的名字</param>
 /// <returns>bool</returns>
 public bool ReducedImage(int Width, int Height, string targetFilePath)
 {
     try
     {
         Image ReducedImage;
         Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
         ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
         ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
         ReducedImage.Dispose();
         return(true);
     }
     catch (Exception e)
     {
         ErrorMessage = e.Message;
         return(false);
     }
 }
예제 #8
0
        /// <summary>
        /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
        /// 必须先调用SetConstructer
        /// </summary>
        /// <param name="Width">缩略图的宽度</param>
        /// <param name="Height">缩略图的高度</param>
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool GetReducedImage(int Width, int Height, string targetFilePath)
        {
            try
            {
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                string      fExt   = System.IO.Path.GetExtension(targetFilePath).ToLower();
                ImageFormat imagef = ImageFormat.Jpeg;
                switch (fExt)
                {
                case ".jpg":
                case ".jpeg":
                    imagef = ImageFormat.Jpeg;
                    break;

                case ".bmp":
                    imagef = ImageFormat.Bmp;
                    break;

                case ".png":
                    imagef = ImageFormat.Png;
                    break;

                default:
                    break;
                }
                ReducedImage.Save(@targetFilePath, imagef);

                ReducedImage.Dispose();

                return(true);
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return(false);
            }
        }
예제 #9
0
 // 方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
 public static bool ReducedImage(Stream stream, double Percent, string targetFilePath)
 {
     try
     {
         ErrorMessage  = "";
         ResourceImage = Image.FromStream(stream);
         Image ReducedImage;
         Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
         ImageWidth   = Convert.ToInt32(ResourceImage.Width * Percent);
         ImageHeight  = (ResourceImage.Height) * ImageWidth / ResourceImage.Width;//等比例缩放
         ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
         ReducedImage.Save(targetFilePath, ImageFormat.Jpeg);
         ReducedImage.Dispose();
         return(true);
     }
     catch (Exception e)
     {
         ErrorMessage = e.Message;
         return(false);
     }
 }
예제 #10
0
        /// <summary>
        /// 生成缩略图重载方法,将缩略图文件保存到指定的路径
        /// </summary>
        /// <param name="Width">缩略图的宽度</param>
        /// <param name="Height">缩略图的高度</param>
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool GetReducedImage(int Width, int Height, string targetFilePath)
        {
            try
            {
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);

                ReducedImage.Dispose();

                return(true);
            }
            catch (Exception ex)
            {
                CSharpUtils.LogHelper.Error(String.Format("GetReducedImage {0} {1} {2} 异常", Width, Height, targetFilePath), ex);
                return(false);
            }
        }