欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.Net上傳圖片同時(shí)生成高清晰縮略圖

 更新時(shí)間:2015年09月05日 14:58:30   投稿:lijiao  
這篇文章主要介紹了Asp.Net上傳圖片同時(shí)生成高清晰縮略圖的方法,需要的朋友可以參考下

在asp.net中,上傳圖片功能或者是常用的,生成縮略圖也是常用的。baidu或者google,c#的方法也是很多的,但是一用卻發(fā)現(xiàn)縮略圖不清晰啊,縮略圖片太大之類的事情,下面是我在處理圖片上的代碼,效果不錯(cuò),所以拿出來分享,(效果能達(dá)到一些繪圖軟件的效果)

代碼如下:

 /// <summary>
  /// asp.net上傳圖片并生成縮略圖
  /// </summary>
  /// <param name="upImage">HtmlInputFile控件</param>
  /// <param name="sSavePath">保存的路徑,些為相對(duì)服務(wù)器路徑的下的文件夾</param>
  /// <param name="sThumbExtension">縮略圖的thumb</param>
  /// <param name="intThumbWidth">生成縮略圖的寬度</param>
  /// <param name="intThumbHeight">生成縮略圖的高度</param>
  /// <returns>縮略圖名稱</returns>
  public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
  {
   string sThumbFile = "";
   string sFilename = "";  
   if (upImage.PostedFile != null)
   {
    HttpPostedFile myFile = upImage.PostedFile;
    int nFileLen = myFile.ContentLength;
    if (nFileLen == 0)
     return "沒有選擇上傳圖片";   
    //獲取upImage選擇文件的擴(kuò)展名
    string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
    //判斷是否為圖片格式
    if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
     return "圖片格式不正確";
    
    byte[] myData = new Byte[nFileLen];
    myFile.InputStream.Read(myData, 0, nFileLen);
    sFilename = System.IO.Path.GetFileName(myFile.FileName);
    int file_append = 0;
    //檢查當(dāng)前文件夾下是否有同名圖片,有則在文件名+1
    while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
    {
     file_append++;
     sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
      + file_append.ToString() + extendName;
    }
    System.IO.FileStream newFile
     = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
     System.IO.FileMode.Create, System.IO.FileAccess.Write);
    newFile.Write(myData, 0, myData.Length);
    newFile.Close();
    //以上為上傳原圖
    try
    {
     //原圖加載
     using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
     {
      //原圖寬度和高度
      int width = sourceImage.Width;
      int height = sourceImage.Height;
      int smallWidth;
      int smallHeight;
      //獲取第一張繪制圖的大小,(比較 原圖的寬/縮略圖的寬 和 原圖的高/縮略圖的高)
      if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
      {
       smallWidth = intThumbWidth;
       smallHeight = intThumbWidth * height / width;
      }
      else
      {
       smallWidth = intThumbHeight * width / height;
       smallHeight = intThumbHeight;
      }
      //判斷縮略圖在當(dāng)前文件夾下是否同名稱文件存在
      file_append = 0;
      sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
      while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
      {
       file_append++;
       sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
        file_append.ToString() + extendName;
      }
      //縮略圖保存的絕對(duì)路徑
      string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
      //新建一個(gè)圖板,以最小等比例壓縮大小繪制原圖
      using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
      {
       //繪制中間圖
       using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
       {
        //高清,平滑
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Black);
        g.DrawImage(
        sourceImage,
        new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
        new System.Drawing.Rectangle(0, 0, width, height),
        System.Drawing.GraphicsUnit.Pixel
        );
       }
       //新建一個(gè)圖板,以縮略圖大小繪制中間圖
       using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
       {
      //繪制縮略圖 http://www.cnblogs.com/sosoft/
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
        {
         //高清,平滑
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
         g.Clear(Color.Black);
         int lwidth = (smallWidth - intThumbWidth) / 2;
         int bheight = (smallHeight - intThumbHeight) / 2;
         g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
         g.Dispose();
         bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
       }
      }
     }
    }
    catch
    {
     //出錯(cuò)則刪除
     System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
     return "圖片格式不正確";
    }
    //返回縮略圖名稱
    return sThumbFile;
   }
   return "沒有選擇圖片";
  }

HtmlInputFile控件我想大家都應(yīng)該知道的,就是input type=file....

下面把調(diào)用代碼也一起C上來

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
  <title>圖片上傳-柯樂義</title>
 </head>
 <body>
  <form id="form1" runat="server">
  <div>  
   <input id="File1" runat="server" type="file" /></div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  </form>
 </body>
 </html>

 protected void Button1_Click(object sender, EventArgs e)
  {
   string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118); 
  }

這樣就會(huì)在你的UpLoad文件夾下多出兩張圖片,一張是原圖,一張是縮略圖。

提供一個(gè)更好的算法,由于沒有時(shí)間去測(cè)試和調(diào)試,僅供參考

即,在第一步等比例縮小的時(shí)候,可以分多次,即把原圖到上面代碼的中間圖以百分比縮小,

例如:原圖為500*500 我要縮略成100*80,上面代碼程序會(huì)先繪制一張100*100的中間圖,再在這圖片上繪制100*80的,在繪制100*100中間圖之前如果先繪300*300的中間圖,再在300*300的基礎(chǔ)上再繪100*100然后再繪100*80這樣會(huì)比我上面的代碼效果更好,圖片更清晰,即中間圖越多,效果越好,大家可以去試試。

相關(guān)文章

  • asp.net操作Word實(shí)現(xiàn)批量替換

    asp.net操作Word實(shí)現(xiàn)批量替換

    這篇文章主要介紹了asp.net操作Word實(shí)現(xiàn)批量替換的方法,需要的朋友可以參考下
    2015-10-10
  • ASP.NET、SharePoint中另存文件的長(zhǎng)文件名被截?cái)嗟脑蚣敖鉀Q辦法

    ASP.NET、SharePoint中另存文件的長(zhǎng)文件名被截?cái)嗟脑蚣敖鉀Q辦法

    這個(gè)問題起初發(fā)生在SharePoint的環(huán)境中,我以為是SharePoint限制了長(zhǎng)度,后來我試驗(yàn)了一下,在ASP.NET的應(yīng)用中也同樣會(huì)發(fā)生。
    2009-11-11
  • 一步步教你在Asp.net Mvc中使用UEditor編輯器

    一步步教你在Asp.net Mvc中使用UEditor編輯器

    大家都知道ueditor是百度編輯器,目前使用也比較廣泛,下面這篇文章主要是通過一步步的步驟教大家在Asp.net Mvc中使用UEditor編輯器,需要的朋友可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • Attribute/特性心得隨筆

    Attribute/特性心得隨筆

    從事asp.net工作的相關(guān)人員對(duì)Attribute并不陌生吧,本文就來為大家介紹下Attribute特性,下面有個(gè)不錯(cuò)的示例,喜歡的朋友感受下
    2013-11-11
  • ASP.NET 4中的可擴(kuò)展輸出緩存(可以緩存頁面/控件等)

    ASP.NET 4中的可擴(kuò)展輸出緩存(可以緩存頁面/控件等)

    ASP.NET 1.0引入輸出緩存的概念,這使得開發(fā)者可以緩存頁面、控件、控制器以及HTTP響應(yīng)的輸出到內(nèi)存中,接下來詳細(xì)介紹,感興趣的朋友可以了解下
    2013-01-01
  • Visual?Studio快捷鍵匯總

    Visual?Studio快捷鍵匯總

    這篇文章介紹了Visual?Studio的常用快捷鍵,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • c# static的全部用法收集整理

    c# static的全部用法收集整理

    c# static的全部用法收集整理...
    2007-08-08
  • asp.net EXECUTENONQUERY()返回值介紹

    asp.net EXECUTENONQUERY()返回值介紹

    前些日子作一些數(shù)據(jù)項(xiàng)目的時(shí)候 在ADO.NET 中處理 ExecuteNonQuery()方法時(shí),總是通過判斷其返回值是否大于0來判斷操作時(shí)候成功 。但是實(shí)際上并不是這樣的,下面詳細(xì)介紹一下,有需要的朋友可以參考
    2013-08-08
  • 淺析GridView中顯示時(shí)間日期格式的問題

    淺析GridView中顯示時(shí)間日期格式的問題

    下面小編就為大家?guī)硪黄獪\析GridView中顯示時(shí)間日期格式的問題。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • javascript操作ASP.NET服務(wù)器控件

    javascript操作ASP.NET服務(wù)器控件

    這篇文章主要介紹了javascript操作ASP.NET服務(wù)器控件 的相關(guān)資料,需要的朋友可以參考下
    2015-06-06

最新評(píng)論