zoukankan      html  css  js  c++  java
  • C#生成高清缩略图

    首先是前台代码:

    1 <div>
    2     <asp:FileUpload ID="photoFile" runat="server" />
    3     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    4     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    5 </div>

    然后是后台代码:

     1 using System;
     2 using System.Drawing;
     3 using System.IO;
     4 using System.Web;
     5 
     6 namespace Shop
     7 {
     8     public partial class Test : System.Web.UI.Page
     9     {
    10         protected void Page_Load(object sender, EventArgs e)
    11         {
    12         }
    13 
    14         /// <summary>
    15         /// 压缩
    16         /// </summary>
    17         /// <param name="sender"></param>
    18         /// <param name="e"></param>
    19         protected void Button1_Click(object sender, EventArgs e)
    20         {
    21             HttpPostedFile file = photoFile.PostedFile;
    22             if (!file.ContentType.Contains("image"))
    23             {
    24                 this.Label1.Text = "照片格式不合法";
    25             }
    26             string ext = Path.GetExtension(file.FileName).ToLower();
    27             if (ext != ".jpg" && ext != ".gif" && ext != ".png" && ext != ".jpeg")
    28             {
    29                 this.Label1.Text = "请您上传jpg、gif、png图片";
    30             }
    31             if (file.ContentLength > 5 * 1024 * 1024)
    32             {
    33                 this.Label1.Text = "请您上传512字节内的图片";
    34             }
    35             string newName = Guid.NewGuid().ToString();
    36 
    37             string tempPath = "upload/";
    38 
    39             string img = tempPath + newName + ext;
    40 
    41             string filePath = Server.MapPath(img);
    42 
    43             try
    44             {
    45                 using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
    46                 {
    47                     GetThumbnail(originalImage, 280, 160).Save(filePath);
    48                 }
    49                 this.Label1.Text = "压缩成功!";
    50             }
    51             catch (Exception ex)
    52             {
    53                 
    54                 throw ex;
    55             }
    56         }
    57 
    58         //---------------------------------------------为图片生成缩略图-----------------------
    59         /// <summary> 
    60         /// 为图片生成缩略图   
    61         /// </summary> 
    62         /// <param name="phyPath">原图片的路径</param> 
    63         /// <param name="width">缩略图宽</param> 
    64         /// <param name="height">缩略图高</param> 
    65         /// <returns></returns> 
    66         public System.Drawing.Image GetThumbnail(System.Drawing.Image image, int width, int height)
    67         {
    68             Bitmap bmp = new Bitmap(width, height);
    69             //从Bitmap创建一个System.Drawing.Graphics 
    70             System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
    71             //设置  
    72             gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    73             //下面这个也设成高质量 
    74             gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    75             //下面这个设成High 
    76             gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    77             //把原始图像绘制成上面所设置宽高的缩小图 
    78             System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
    79 
    80             gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height,GraphicsUnit.Pixel);
    81 
    82             return bmp;
    83         }
    84         //----------------------------------------------------------------------------------------
    85     }
    86 }

    运行效果:

    最终上传(文件夹要已经存在):

    以上就是缩略图的制作。

  • 相关阅读:
    Nmap绕过防火墙&脚本的使用
    Nmap在实战中的高级用法
    kali&BT安装好之后无法上网或者无法获得内网IP
    [转]谈渗透测试方法和流程
    xssless
    国内外有名的安全扫描工具,你知道几个?
    Linux下如果忘记了Mysql的root密码该怎么办?
    JSP手动注入 全
    sqlmap用户手册 [详细]
    Windows系统如何使用sqlmap
  • 原文地址:https://www.cnblogs.com/KTblog/p/4264393.html
Copyright © 2011-2022 走看看