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 }

    运行效果:

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

    以上就是缩略图的制作。

  • 相关阅读:
    poj 3280 Cheapest Palindrome(区间DP)
    POJ 2392 Space Elevator(多重背包)
    HDU 1285 定比赛名次(拓扑排序)
    HDU 2680 Choose the best route(最短路)
    hdu 2899 Strange fuction (三分)
    HDU 4540 威威猫系列故事――打地鼠(DP)
    HDU 3485 Count 101(递推)
    POJ 1315 Don't Get Rooked(dfs)
    脱离eclipse,手动写一个servlet
    解析xml,几种方式
  • 原文地址:https://www.cnblogs.com/KTblog/p/4264393.html
Copyright © 2011-2022 走看看