首页 新闻 会员 周边 捐助

如何将图片按比例处理到指定大小,如果按比例缩放后宽或高大于指定的宽高,就对超出的宽或高裁切到指定大小

0
悬赏园豆:30 [已解决问题] 解决于 2012-12-10 14:19

不知道要如何处理,我是先进行比例缩放再进行裁切,但是裁切的时候会出现变形、图片裁切了其中一部分出来,导致显示出来没有按比例缩放的效果好

最佳损友。的主页 最佳损友。 | 初学一级 | 园豆:63
提问于:2012-11-29 17:44
< >
分享
最佳答案
1
    public class ThumbnailMaker
    {
        //private static ThumbnailMaker instance;
        //public static ThumbnailMaker Instance
        //{
        //    get
        //    {
        //        lock (typeof(ThumbnailMaker))
        //        {
        //            if (instance == null)
        //                instance = new ThumbnailMaker();
        //            return instance;
        //        }
        //    }
        //}


        public static void CreateDirectory(string path)
        {
            if (path == "") return;
            string head = path.Substring(0, path.IndexOf("\\"));  //d:
            string weibu = path.Substring(head.Length + 1);  //       \1\2
            string hpath = head;
            while (weibu.IndexOf("\\") != -1)
            {
                string p = hpath + "\\" + weibu.Substring(0, weibu.IndexOf("\\"));
                hpath = p;
                if (!Directory.Exists(p))
                    Directory.CreateDirectory(p);
                int ix = weibu.IndexOf("\\") + 1;
                weibu = weibu.Substring(ix);
            }

        }

        /// <summary>
        /// 制作图片的缩略图
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="width">缩略图的宽(像素)</param>
        /// <param name="height">缩略图的高(像素)</param>
        /// <param name="mode">缩略方式</param>
        /// <returns>缩略图</returns>
        /// <remarks>
        ///        <paramref name="mode"/>///            <para>HW:指定的高宽缩放(可能变形)</para>
        ///            <para>HWO:指定高宽缩放(可能变形)(过小则不变)</para>
        ///            <para>W:指定宽,高按比例</para>
        ///            <para>WO:指定宽(过小则不变),高按比例</para>
        ///            <para>H:指定高,宽按比例</para>
        ///            <para>HO:指定高(过小则不变),宽按比例</para>
        ///            <para>CUT:指定高宽裁减(不变形)</para>
        /// </remarks>
        public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailMode mode)
        {
            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;


            switch (mode)
            {
                case ThumbnailMode.UsrHeightWidth: //指定高宽缩放(可能变形)
                    break;
                case ThumbnailMode.UsrHeightWidthBound: //指定高宽缩放(可能变形)(过小则不变)
                    if (originalImage.Width <= width && originalImage.Height <= height)
                    {
                        return originalImage;
                    }
                    if (originalImage.Width < width)
                    {
                        towidth = originalImage.Width;
                    }
                    if (originalImage.Height < height)
                    {
                        toheight = originalImage.Height;
                    }
                    break;
                case ThumbnailMode.UsrWidth: //指定宽,高按比例
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case ThumbnailMode.UsrWidthBound: //指定宽(过小则不变),高按比例
                    if (originalImage.Width <= width)
                    {
                        return originalImage;
                    }
                    else
                    {
                        toheight = originalImage.Height * width / originalImage.Width;
                    }
                    break;
                case ThumbnailMode.UsrHeight: //指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case ThumbnailMode.UsrHeightBound: //指定高(过小则不变),宽按比例
                    if (originalImage.Height <= height)
                    {
                        return originalImage;
                    }
                    else
                    {
                        towidth = originalImage.Width * height / originalImage.Height;
                    }
                    break;
                case ThumbnailMode.Cut: //指定高宽裁减(不变形)
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            Image bitmap = new Bitmap(towidth, toheight);

            //新建一个画板
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
          
            //清空画布并以透明背景色填充
            //g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
                        new Rectangle(x, y, ow, oh),
                        GraphicsUnit.Pixel);
            g.Dispose();
            return bitmap;
        }

        /// <summary>
        /// 制作图片的缩略图
        /// </summary>
        /// <param name="originalStream">原图</param>
        /// <param name="thumbnailPath">保存缩略图的路径</param>
        /// <param name="width">缩略图的宽(像素)</param>
        /// <param name="height">缩略图的高(像素)</param>
        /// <param name="mode">缩略方式,参见<seealso cref="MakeThumbnail(Image, int, int, string)"/></param>
        public static void MakeThumbnail(Stream originalStream, string thumbnailPath, int width, int height, ThumbnailMode mode)
        {
            Image originalImage = Image.FromStream(originalStream);
            try
            {
                MakeThumbnail(originalImage, thumbnailPath, width, height, mode);
            }
            finally
            {
                originalImage.Dispose();
            }
        }

        /// <summary>
        /// 制作图片的缩略图
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="thumbnailPath">保存缩略图的路径</param>
        /// <param name="width">缩略图的宽(像素)</param>
        /// <param name="height">缩略图的高(像素)</param>
        /// <param name="mode">缩略方式,参见<seealso cref="MakeThumbnail(Image, int, int, string)"/></param>
        public static void MakeThumbnail(Image originalImage, string thumbnailPath, int width, int height, ThumbnailMode mode)
        {
            Image bitmap = MakeThumbnail(originalImage, width, height, mode);
            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
            }
            finally
            {
                bitmap.Dispose();
            }
        }

        /// <summary>
        /// 制作图片的缩略图
        /// </summary>
        /// <param name="originalImagePath">原图的路径</param>
        /// <param name="thumbnailPath">保存缩略图的路径</param>
        /// <param name="width">缩略图的宽(像素)</param>
        /// <param name="height">缩略图的高(像素)</param>
        /// <param name="mode">缩略方式,参见<seealso cref="MakeThumbnail(Image, int, int, string)"/></param>
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode)
        {
            Image originalImage = Image.FromFile(originalImagePath);
            try
            {
              MakeThumbnail(originalImage, thumbnailPath, width, height, mode);                              
            }
            finally
            {
                originalImage.Dispose();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        public static byte[] MakeThumbnail(byte[] img, int width, int height, ThumbnailMode mode)
        {
            Stream byteStream = new MemoryStream(img);

            Image originalImage = Image.FromStream(byteStream);

            try
            {
                Image _img = MakeThumbnail(originalImage, width, height, mode);

                MemoryStream imgStream = new MemoryStream();

                _img.Save(imgStream, ImageFormat.Jpeg);

                return imgStream.ToArray();
            }
            finally
            {
                originalImage.Dispose();
            }
        }
        /// <summary>
        /// 产生高清缩略图 固定大小
        /// </summary>
        /// <param name="original_image_file">源文件</param>
        /// <param name="object_width">缩略图宽度</param>
        /// <param name="object_height">缩略图高度</param>
        public static void MakeHighQualityThumbnail(string original_image_file, string output, int object_width, int object_height)
        {


            int actual_width = 0;
            int actual_heigh = 0;
            string outputfilename = output; //original_image_file + ".jpg";

            System.Drawing.Bitmap original_image = new Bitmap(original_image_file);//读取源文件           
            actual_width = original_image.Width;
            actual_heigh = original_image.Height;

            Bitmap img = new Bitmap(object_width, object_height);
            img.SetResolution(108f, 108f);
            Graphics gdiobj = Graphics.FromImage(img);
            gdiobj.CompositingQuality = CompositingQuality.HighQuality;
            gdiobj.SmoothingMode = SmoothingMode.HighQuality;
            gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;

            gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0, object_width, object_height);
            Rectangle destrect = new Rectangle(0, 0, object_width, object_height);

            gdiobj.DrawImage(original_image, destrect, 0, 0, actual_width, actual_heigh, GraphicsUnit.Pixel);

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

            System.Drawing.Imaging.ImageCodecInfo ici = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0];

            if (ici != null)
            {
                if (File.Exists(outputfilename))
                    File.Delete(outputfilename);
                img.Save(outputfilename, ici, ep);

            }
            else
            {
                img.Save(outputfilename, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        public static byte[] MakeThumbnail(byte[] img, int width, int height)
        {
            return MakeThumbnail(img, width, height, ThumbnailMode.UsrHeightBound);
        }
    }

    public enum ThumbnailMode
    {
        UsrHeightWidth,
        UsrHeightWidthBound,
        UsrWidth,
        UsrWidthBound,
        UsrHeight,
        UsrHeightBound,
        Cut,
        NONE,
    }

调用示例:

 

            ThumbnailMaker.MakeThumbnail("需要转换的图片名", 要保存的路径+ "\\"+Guid.NewGuid().ToString()+原图的扩展名, 宽, 高, ThumbnailMode.Cut);

收获园豆:10
需要格局 | 老鸟四级 |园豆:2145 | 2012-11-29 19:37
其他回答(4)
0
 1 function imgZoom(inW, inH, imgW, imgH)
 2 {
 3     var size = 
 4     {
 5         width : 0,
 6         height : 0
 7     };
 8     if(!inW || !inH)
 9     {
10         return size;
11     }
12     if(!imgW || !imgH)
13     {
14         return size;
15     }
16     var rateW = (inW / imgW).toFixed('2');
17     var rateH = (inH / imgH).toFixed('2');
18 
19     //图片宽高均小于缩放宽高
20     if(rateW >= 1 && rateH >= 1)
21     {
22         size.width = imgW;
23         size.height = imgH;
24         return size;
25     }
26 
27     //图片宽高均大于缩放宽高
28     if(rateW < 1 && rateH < 1)
29     {
30         var rate = rateW > rateH ? rateH : rateW;
31         size.width = Math.round(imgW * rate);
32         size.height = Math.round(imgH * rate);
33         return size;
34     }
35 
36     //图片宽大于缩放宽, 图片高小于缩放高
37     if(rateW < 1 && rateH >=1)
38     {
39         size.width = inW;
40         size.height = Math.round(imgH * rateW);
41         return size;
42     }
43 
44     //图片高大于缩放高, 图片宽小于缩放宽
45     if(rateH < 1 && rateW >=1)
46     {
47         size.height = inH;
48         size.width = Math.round(imgW * rateH);
49         return size;
50     }
51     return size();
52 }

 

不管啥语言大概就这个逻辑吧, 代码可以再精简!

收获园豆:10
张坤 | 园豆:1865 (小虾三级) | 2012-11-29 17:56
0
///   <summary> 
///   图像尺寸调节 
///   </summary> 
///   <param   name= "filePath"> 图像路径</param> 
///   <param   name= "savePath"> 生成路径</param> 
///   <param   name= "dstWidth"> 缩放后的宽度 </param> 
///   <returns> </returns> 
public static bool SmallPic(string filePath, string savePath, int dstWidth)
        {
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            Bitmap b = new Bitmap(filePath);
            int bigWidth = b.Width;
            int bigHeight = b.Height;

            //自动计算高度
            int dstHeight = Convert.ToInt32((dstWidth / (double)bigWidth) * bigHeight);

            Bitmap dstImage = new Bitmap(dstWidth, dstHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);

            //   设置插值模式 
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;

            //   设置平滑模式 
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

            g.DrawImage(b, new System.Drawing.Rectangle(0, 0, dstImage.Width, dstImage.Height), new System.Drawing.Rectangle(0, 0, b.Width, b.Height), System.Drawing.GraphicsUnit.Pixel);
            g.Save();
            g.Dispose();
            dstImage.Save(savePath);
            b.Dispose();
            dstImage.Dispose();
            return true;
        }
收获园豆:10
geass.. | 园豆:1821 (小虾三级) | 2012-11-29 19:26
0

楼的正解!

jason2013 | 园豆:1998 (小虾三级) | 2012-11-30 09:21
0

很不错,学习了,呵呵

KivenRo | 园豆:1734 (小虾三级) | 2012-11-30 21:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册