示例#1
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);                                                                                                         //返回压缩图片的存储路径
     }
 }
示例#2
0
        protected void ShowThumbnail(string path, int width, int height)
        {
            CheckPath(path);
            FileStream fs  = new FileStream(FixPath(path), FileMode.Open);
            Bitmap     img = new Bitmap(Bitmap.FromStream(fs));

            fs.Close();
            fs.Dispose();
            int cropWidth = img.Width, cropHeight = img.Height;
            int cropX = 0, cropY = 0;

            double imgRatio = (double)img.Width / (double)img.Height;

            if (height == 0)
            {
                height = Convert.ToInt32(Math.Floor((double)width / imgRatio));
            }

            if (width > img.Width)
            {
                width = img.Width;
            }
            if (height > img.Height)
            {
                height = img.Height;
            }

            double cropRatio = (double)width / (double)height;

            cropWidth  = Convert.ToInt32(Math.Floor((double)img.Height * cropRatio));
            cropHeight = Convert.ToInt32(Math.Floor((double)cropWidth / cropRatio));
            if (cropWidth > img.Width)
            {
                cropWidth  = img.Width;
                cropHeight = Convert.ToInt32(Math.Floor((double)cropWidth / cropRatio));
            }
            if (cropHeight > img.Height)
            {
                cropHeight = img.Height;
                cropWidth  = Convert.ToInt32(Math.Floor((double)cropHeight * cropRatio));
            }
            if (cropWidth < img.Width)
            {
                cropX = Convert.ToInt32(Math.Floor((double)(img.Width - cropWidth) / 2));
            }
            if (cropHeight < img.Height)
            {
                cropY = Convert.ToInt32(Math.Floor((double)(img.Height - cropHeight) / 2));
            }

            Rectangle area    = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Bitmap    cropImg = img.Clone(area, System.Drawing.Imaging.PixelFormat.DontCare);

            img.Dispose();
            Image.GetThumbnailImageAbort imgCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

            _r.AddHeader("Content-Type", "image/png");
            cropImg.GetThumbnailImage(width, height, imgCallback, IntPtr.Zero).Save(_r.OutputStream, ImageFormat.Png);
            _r.OutputStream.Close();
            cropImg.Dispose();
        }