zoukankan      html  css  js  c++  java
  • 等比例缩放图片

            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", @"imgTemp");
                    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);
                }
  • 相关阅读:
    Swift如何判断上午还是下午
    Qt Creator编译app到iPhone
    用swift判断string是否包含字母
    QToolTip显示富文本问题
    mac如何发起屏幕共享?
    Redis持久化
    bean 实例化原理解析
    WebSocket和SocketIO总结
    netty入门
    redis 工具类
  • 原文地址:https://www.cnblogs.com/gouyanfeng/p/4209700.html
Copyright © 2011-2022 走看看