<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      samesite


      【譯】MVC3 20個秘方-(12)改變圖片的大小生成縮略圖

      問題

      你允許用戶上傳一個圖片,但是傳統的來說,這個圖片一般是從一個camera輸出的,這個圖片太大。所以你想展現一個簡單的圖片或者縮略圖。在你的網站允許用戶在他看到完整圖片之前先預覽縮略圖(譯者:這是一個很好的用戶體驗)。

      解決方案

      使用以下幾個類去更新現有的文件上傳功能去調整圖片:FileStream, Image, Bitmap,和Graphics 類去指定寬度和高度。

      討論

      在下面的例子,以前創建的FileUpload類將得到更新和重組。創建一個新的功能,稱為ResizeImage執行調整圖片大小。調整大小后的圖像將被保存在以前的文件夾的子文件夾中,名為(thumbnail)縮略圖。 DeleteFile函數也被更新,同時刪除縮略圖和原始圖像,并創建一個新的函數,并調用了兩次刪除功能
      為了避免重復代碼。下面是FileUpload類的代碼:

      譯者:下邊標紅的代碼是我加上去的。這樣我們可以把圖片和縮略圖存到我們項目的文件夾下。否則他會存到:C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\目錄下。

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Linq;
      using System.Web;
      using System.IO;

      namespace MvcApplication.Utils
      {
      public static class FileUpload
      {
      public static char DirSeparator = Path.DirectorySeparatorChar;
      public static string FilesPath = HttpContext.Current.Server.MapPath(string.Format("Content{0}Uploads{1}", DirSeparator, DirSeparator));
      public static string UploadFile(HttpPostedFileBase file)
      {
      // Check if we have a file
      if (null == file) return "";
      // Make sure the file has content
      if (!(file.ContentLength > 0)) return "";
      string fileName = file.FileName;
      string fileExt = Path.GetExtension(file.FileName);
      // Make sure we were able to determine a proper
      // extension
      if (null == fileExt) return "";
      // Check if the directory we are saving to exists
      if (!Directory.Exists(FilesPath))
      {
      // If it doesn't exist, create the directory
      Directory.CreateDirectory(FilesPath);
      }
      // Set our full path for saving
      string path = FilesPath + DirSeparator + fileName;
      // Save our file
      file.SaveAs(Path.GetFullPath(path));
      // Save our thumbnail as well
      ResizeImage(file, 150, 100);
      // Return the filename
      return fileName;
      }
      public static void DeleteFile(string fileName)
      {
      // Don't do anything if there is no name
      if (fileName.Length == 0) return;
      // Set our full path for deleting
      string path = FilesPath + DirSeparator + fileName;
      string thumbPath = FilesPath + DirSeparator +
      "Thumbnails" + DirSeparator + fileName;
      RemoveFile(path);
      RemoveFile(thumbPath);
      }
      private static void RemoveFile(string path)
      {
      // Check if our file exists
      if (File.Exists(Path.GetFullPath(path)))
      {
      // Delete our file
      File.Delete(Path.GetFullPath(path));
      }
      }
      public static void ResizeImage(HttpPostedFileBase file, int width, int height)
      {

      string thumbnailDirectory =
      String.Format(@"{0}{1}{2}", FilesPath,
      DirSeparator, "Thumbnails");
      // Check if the directory we are saving to exists
      if (!Directory.Exists(thumbnailDirectory))
      {
      // If it doesn't exist, create the directory
      Directory.CreateDirectory(thumbnailDirectory);
      }
      // Final path we will save our thumbnail
      string imagePath =
      String.Format(@"{0}{1}{2}", thumbnailDirectory,
      DirSeparator, file.FileName);
      // Create a stream to save the file to when we're
      // done resizing
      FileStream stream = new FileStream(Path.GetFullPath(
      imagePath), FileMode.OpenOrCreate);
      // Convert our uploaded file to an image
      Image OrigImage = Image.FromStream(file.InputStream);
      // Create a new bitmap with the size of our
      // thumbnail
      Bitmap TempBitmap = new Bitmap(width, height);
      // Create a new image that contains quality
      // information
      Graphics NewImage = Graphics.FromImage(TempBitmap);
      NewImage.CompositingQuality =
      CompositingQuality.HighQuality;
      NewImage.SmoothingMode =
      SmoothingMode.HighQuality;
      NewImage.InterpolationMode =
      InterpolationMode.HighQualityBicubic;
      // Create a rectangle and draw the image
      Rectangle imageRectangle = new Rectangle(0, 0,
      width, height);
      NewImage.DrawImage(OrigImage, imageRectangle);
      // Save the final file
      TempBitmap.Save(stream, OrigImage.RawFormat);
      // Clean up the resources
      NewImage.Dispose();
      TempBitmap.Dispose();
      OrigImage.Dispose();
      stream.Close();
      stream.Dispose();
      }
      }
      }

       

      上邊的例子做了很多事特別是在ResizeImage函數。

      首先,如果縮略圖??目錄不存在,它將被創建。接下來,一個新的FileStream會根據縮略圖存放的完整路徑被創建用于編輯。
      原上傳的圖像根據uploaded的InputStream被轉換為Image類的對象。一個新的位圖會被根據圖圖像的寬度和高度創建。接下來用這個位圖去創建一個新的Graphics對象。Graphics對象,NewImage,用于設置和定義質量,表面光滑,插補模式。如果沒有這些設置,縮略圖會不會好看非常像素化和調整笨拙。

      一旦都設置好了,一個新的矩形被創建并且原始圖像被畫到Graphics中。這是執行實際的調整大小。最后保存位圖和所有創建的對象的處置,以釋放資源。

       

      另請參見

      FileStream, Image, Bitmap, and Graphics

      posted @ 2011-12-01 08:12  技術弟弟  閱讀(4506)  評論(42)    收藏  舉報
      主站蜘蛛池模板: 伊人中文在线最新版天堂| 中国孕妇变态孕交xxxx| 91老肥熟女九色老女人| 粉嫩国产av一区二区三区| 国产精品美女黑丝流水| 精品国产乱码久久久久APP下载| 91蜜臀国产自产在线观看| 国产精品久久久一区二区三区| 在线日韩日本国产亚洲| 亚洲热视频这里只有精品| 日本高清在线播放一区二区三区| 国产精品一二二区视在线| 国模在线视频一区二区三区| 亚洲国产日韩在线视频| 亚洲国模精品一区二区| 成人欧美一区二区三区在线观看| 亚洲精品国产一区二区在线观看| 动漫av网站免费观看| 人妻少妇| 推油少妇久久99久久99久久| 国产高清在线男人的天堂| 亚洲成人一区二区av| 蜜臀av久久国产午夜福利软件| 亚洲精品一区二区三区大| 精品乱码一区二区三四五区| 国产精品中文字幕免费| 深夜福利成人免费在线观看 | 2020精品自拍视频曝光| 啪啪av一区二区三区| 67194亚洲无码| 日本熟妇XXXX潮喷视频| 久久天天躁狠狠躁夜夜不卡| 亚洲青青草视频在线播放| 国产精品天干天干综合网| 亚洲人妻一区二区精品| 女高中生自慰污污网站| 国产精品第二页在线播放| 鲁鲁网亚洲站内射污| 一区二区三区精品偷拍| 国产麻豆放荡av激情演绎| 亚洲无人区一区二区三区|