首页 > 其他 > 详细

等比例缩放图片

时间:2015-01-08 00:45:34      阅读:329      评论:0      收藏:0      [点我收藏+]
        public class GetThumbnailImg
        {
            /// <summary>
            /// 读取图片的缩略图
            /// </summary>
            /// <param name="PicPath">源图片的路径</param>
            /// <param name="PicTemp">生成缩略图的目录</param>
            /// <param name="Width">生成缩略图的宽</param>
            /// <param name="Height">生成缩略图的高</param>
            /// <returns>生成成功则返回路径,否则返回""</returns>
            public static string GetThumbnailPic(string PicPath, string PicTemp, int Width, int Height)
            {
                System.Drawing.Bitmap Bitmap = new System.Drawing.Bitmap(PicPath);
                if (Bitmap.Width > Bitmap.Height)
                {
                    Height = Bitmap.Height * Width / Bitmap.Width;
               
                }
                else if (Bitmap.Width < Bitmap.Height)
                {
                    Width = Bitmap.Width * Height / Bitmap.Height;
                }

                var img = Bitmap.GetThumbnailImage(Width, Height, () => { return false; }, IntPtr.Zero);
                try
                {
                    img.Save(PicTemp);
                    return PicTemp;
                }
                catch
                {
                    return "";
                }
            }
        }

 

 

调用

 

 

         if (IsPostBack)
            {
                string FileName = MapPath("~/img/") + Guid.NewGuid().ToString() + System.IO.Path.GetExtension(FileUpload1.FileName);
                FileUpload1.SaveAs(FileName);
                string TempPath = FileName.Replace(@"\img\", @"\img\Temp\");
                var Path = GetThumbnailImg.GetThumbnailPic(FileName, TempPath, 200, 200);
                Image img = new Image();
                img.ImageUrl = "~/img/Temp/" + System.IO.Path.GetFileName(Path);
                this.Controls.Add(img);
            }

 

等比例缩放图片

原文:http://www.cnblogs.com/gouyanfeng/p/4209700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!