WEB开发网
开发学院WEB开发ASP.NET 在ASP.NET里轻松实现缩略图 阅读

在ASP.NET里轻松实现缩略图

 2007-12-15 09:30:37 来源:WEB开发网   
核心提示:在ASP.NET里轻松实现缩略图作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月23日 8点26分35秒 以前,在页面上实现缩略图必须借助第三方组件,在ASP.NET里轻松实现缩略图,现在,有了.NET,查看例子代码如下:Thumbnail.aspx<%@ Page Language="

在ASP.NET里轻松实现缩略图

作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月23日 8点26分35秒


以前,在页面上实现缩略图必须借助第三方组件。现在,有了.NET,就可以很轻松地实现缩略图。下面就是实现缩略图的例子。

查看例子

代码如下:Thumbnail.aspx

&lt;%@ Page Language="vb" AutoEventWireup="false" Codebehind="Thumbnail.aspx.vb" Inherits="aspxWeb.Thumbnail" %&gt;&lt;DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;<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> <form id="Form1" method="post" runat="server" enctype="multipart/form-data"> <INPUT type="file" name="file" width="600"><br/><br/> </form> </body></HTML>

后代码:Thumbnail.aspx.vb

Imports SystemImports System.WebImports System.DrawingImports System.IOImports System.Drawing.ImagingPublic 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() Dim 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 SubEnd 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&#123;/// <summary>/// Thumbnail 的摘要说明。/// </summary>public class Thumbnail : System.Web.UI.Page&#123;protected System.Web.UI.WebControls.Label Label1;protected System.Web.UI.WebControls.Button Button1;private void Page_Load(object sender, System.EventArgs e)&#123;// 在此处放置用户代码以初始化页面Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>"; Button1.Text = "上载并显示缩略图"; &#125;#region Web 窗体设计器生成的代码override protected void OnInit(EventArgs e)&#123;//// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);&#125;/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent()&#123; this.Button1.Click += new System.EventHandler(this.Button1_Click);this.Load += new System.EventHandler(this.Page_Load);&#125;#endregionprivate void Button1_Click(object sender, System.EventArgs e)&#123;HttpFileCollection MyFileColl = HttpContext.Current.Request.Files; HttpPostedFile MyPostedFile = MyFileColl[0]; if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) &#123; Response.Write("无效的图形格式。"); return; &#125; GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream); &#125;private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) &#123; if ((strContentType.ToString().ToLower()) == "image/pjpeg") &#123; return System.Drawing.Imaging.ImageFormat.Jpeg; &#125; else if ((strContentType.ToString().ToLower()) == "image/gif") &#123; return System.Drawing.Imaging.ImageFormat.Gif; &#125; else if ((strContentType.ToString().ToLower()) == "image/bmp") &#123; return System.Drawing.Imaging.ImageFormat.Bmp; &#125; else if ((strContentType.ToString().ToLower()) == "image/tiff") &#123; return System.Drawing.Imaging.ImageFormat.Tiff; &#125; else if ((strContentType.ToString().ToLower()) == "image/x-icon") &#123; return System.Drawing.Imaging.ImageFormat.Icon; &#125; else if ((strContentType.ToString().ToLower()) == "image/x-png") &#123; return System.Drawing.Imaging.ImageFormat.Png; &#125; else if ((strContentType.ToString().ToLower()) == "image/x-emf") &#123; return System.Drawing.Imaging.ImageFormat.Emf; &#125; else if ((strContentType.ToString().ToLower()) == "image/x-exif") &#123; return System.Drawing.Imaging.ImageFormat.Exif; &#125; else if ((strContentType.ToString().ToLower()) == "image/x-wmf") &#123; return System.Drawing.Imaging.ImageFormat.Wmf; &#125; else &#123; return System.Drawing.Imaging.ImageFormat.MemoryBmp; &#125; &#125; private void GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream) &#123; System.Drawing.Image oImg;if (blnGetFromFile) &#123; oImg = System.Drawing.Image.FromFile(strFileName); &#125; else &#123; oImg = System.Drawing.Image.FromStream(ImgStream); &#125; 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); &#125; &#125;&#125;

Tags:ASP NET 轻松

编辑录入:coldstar [复制链接] [打 印]
赞助商链接