程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 在ASP.NET裡輕松實現縮略圖

在ASP.NET裡輕松實現縮略圖

編輯:.NET實例教程

     以前,在頁面上實現縮略圖必須借助第三方組件。現在,有了.Net,就可以很輕松地實現縮略圖。下面就是實現縮略圖的例子。
  
  查看例子
  
  代碼如下:Thumbnail.ASPx
  
  <%@ Page Language="vb" AutoEventWireup="false" Codebehind="Thumbnail.ASPx.vb"
   Inherits="ASPxWeb.Thumbnail" %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN">
  <Html>
   <HEAD>
   <title>在ASP.Net裡輕松實現縮略圖</title>
   <meta content="Microsoft Visual Studio.Net 7.0" name="GENERATOR">
   <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClIEntScript">
   <meta content="http://schemas.microsoft.com/intellisense/IE5" name="vs_targetSchema">
   </HEAD>
   <body MS_POSITIONING="GridLayout">
   <asp:Label id="Label1" runat="server"></ASP:Label>
   <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
   <INPUT type="file" name="file" width="600"><br><br>
   <asp:Button id="Button1" runat="server"></ASP:Button>
   </form>
   </body>
  </Html>
  後代碼:Thumbnail.ASPx.vb
  
  Imports System
  Imports System.Web
  Imports System.Drawing
  Imports System.IO
  Imports System.Drawing.Imaging
  
  Public Class Thumbnail
   Inherits System.Web.UI.Page
   Protected WithEvents Label1 As System.Web.UI.WebControls.Label
   Protected WithEvents Button1 As System.Web.UI.WebControls.Button
  
  #Region " Web Form Designer Generated Code "
  
   'This call is required by the Web Form Designer.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  
   End Sub
  
   Private Sub Page_Init(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
   End Sub
  
  #End Region
  
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Label1.Text = "<h3>在ASP.Net裡輕松實現縮略圖</h3>"
   Button1.Text = "上載並顯示縮略圖"
   End Sub
  
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim MyFileColl As HttpFileCollection = HttpContext.Current.Request.Files
   Dim MyPostedFile As HttpPostedFile = MyFileColl.Item(0)
   If LCase(MyPostedFile.ContentType.ToString()).IndexOf("image") < 0 Then
   Response.Write("無效的圖形格式。")
   Exit Sub
   End If
   GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(),_
   False, MyPostedFile.InputStream)
   End Sub
  
   Private Function GetImageType(ByVal strContentType) As System.Drawing.Imaging.ImageFormat
   Select Case (strContentType.ToString().ToLower())
   Case "image/pjpeg"
   GetImageType = System.Drawing.Imaging.ImageFormat.Jpeg
   Case "image/gif"
   GetImageType = System.Drawing.Imaging.ImageFormat.Gif
   Case "image/bmp"
   GetImageType = System.Drawing.Imaging.ImageFormat.Bmp
   Case "image/tiff"
   GetImageType = System.Drawing.Imaging.ImageFormat.Tiff
   Case "image/x-icon"
   GetImageType = System.Drawing.Imaging.ImageFormat.Icon
   Case "image/x-png"
   GetImageType = System.Drawing.Imaging.ImageFormat.Png
   Case "image/x-emf"
   GetImageType = System.Drawing.Imaging.ImageFormat.Emf
   Case "image/x-exif"
   GetImageType = System.Drawing.Imaging.ImageFormat.Exif
   Case "image/x-wmf"
   GetImageType = System.Drawing.Imaging.ImageFormat.Wmf
   Case Else
   GetImageType = System.Drawing.Imaging.ImageFormat.MemoryBmp
   End Select
   End Function
  
   Private Sub GetThumbNail(ByVal strFileName, ByVal iWidth, ByVal iheight, ByVal strContentType, _
   ByVal blnGetFromFile, ByVal ImgStream)
   Dim oImg As Image
   If blnGetFromFile Then
   oImg = oImg.FromFile(strFileName)
   Else
   oImg = oImg.FromStream(ImgStream)
   End If
   oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr()).Zero)
   Dim strGuid As String = (New Guid()).NewGuid().ToString().ToUpper()
 &nbsDim strFileExt As String = strFileName.Substring(strFileName.LastIndexOf("."))
   '保存到本地
   'oImg.Save(Server.MapPath("images") + "\" + strGuid + strFileExt, GetImageType(strContentType))
   '直接輸出url文件
   'Response.Redirect("images/" + strGuid + strFileExt)
   '以下顯示在屏幕上
   Response.ContentType = strContentType
   Dim MemStream As New MemoryStream()
   ' 注意:這裡如果直接用 oImg.Save(Response.OutputStream, GetImageType(strContentType))
   ' 對不同的格式可能會出錯,比如Png格式。
   oImg.Save(MemStream, GetImageType(strContentType))
   MemStream.WriteTo(Response.OutputStream)
   End Sub
  
  End Class
  C#
  
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  using System.IO;
  using System.Drawing.Imaging;
  
  namespace eMeng.Exam
  {
  /// <summary>
  /// Thumbnail 的摘要說明。
  /// </summary>
  public class Thumbnail : System.Web.UI.Page
  {
   protected System.Web.UI.WebControls.Label Label1;
   protected System.Web.UI.WebControls.Button Button1;
  
   private void Page_Load(object sender, System.EventArgs e)
   {
   // 在此處放置用戶代碼以初始化頁面
   Label1.Text = "<h3>在ASP.Net裡輕松實現縮略圖</h3>";
   Button1.Text = "上載並顯示縮略圖";
  
   }
  
   #region Web 窗體設計器生成的代碼
   override protected void OnInit(EventArgs e)
   {
   //
   // CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
   }
  
   /// <summary>
   /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
   /// 此方法的內容。
   /// </summary>
   private void InitializeComponent()
   {
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);
  
   }
   #endregion
  
   private void Button1_Click(object sender, System.EventArgs e)
   {
   HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
   HttpPostedFile MyPostedFile = MyFileColl[0];
  
   if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
   {
   Response.Write("無效的圖形格式。");
   return;
   }
   GetThumbNail(MyPostedFile.FileName, 100, 100,
   MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream);
   }
   private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
   {
   if ((strContentType.ToString().ToLower()) == "image/pjpeg")
   {
   return System.Drawing.Imaging.ImageFormat.Jpeg;
   }
   else if ((strContentType.ToString().ToLower()) == "image/gif")
   {
   return System.Drawing.Imaging.ImageFormat.Gif;
   }
   else if ((strContentType.ToString().ToLower()) == "image/bmp")
   {
   return System.Drawing.Imaging.ImageFormat.Bmp;
   }
   else if ((strContentType.ToString().ToLower()) == "image/tiff")
   {
   return System.Drawing.Imaging.ImageFormat.Tiff;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-icon")
   {
   return System.Drawing.Imaging.ImageFormat.Icon;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-png")
   {
   return System.Drawing.Imaging.ImageFormat.Png;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-emf")
   {
   return System.Drawing.Imaging.ImageFormat.Emf;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-exif")
   {
   return System.Drawing.Imaging.ImageFormat.Exif;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
   {
   return System.Drawing.Imaging.ImageFormat.Wmf;
   }
   else
   {
   return System.Drawing.Imaging.ImageFormat.MemoryBmp;
   }
   }
  
   private void GetThumbNail(string strFileName, int iWidth, int iheight,
   string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream){
   System.Drawing.Image oImg;
  
   if (blnGetFromFile)
   {
 &nbsp; oImg = System.Drawing.Image.FromFile(strFileName);
   }
   else
   {
   oImg = System.Drawing.Image.FromStream(ImgStream);
   }
   oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
   string strGuid = System.Guid.NewGuid().ToString().ToUpper();
   string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
   Response.ContentType = strContentType;
   MemoryStream MemStream = new MemoryStream();
   oImg.Save(MemStream, GetImageType(strContentType));
   MemStream.WriteTo(Response.OutputStream);
   }
  
  }
  }
  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved