ITPub博客

首页 > 应用开发 > IT综合 > 在ASP.NET里轻松实现缩略图 (转)

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

原创 IT综合 作者:amyz 时间:2007-08-15 12:51:09 0 删除 编辑
在ASP.NET里轻松实现缩略图 (转)[@more@]

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

dotnetplayground.com/">http://lucky_elove.www1.dotnetplayground.com/

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

aspx" target=_blank>查看例子

代码如下:Thumbnail.aspx

<a href="leibie-11-1.html">web</a></a>.Thumbnail" %> w3c</a>//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>在<a href="tag-46-1.html">ASP.net</a>里轻松实现缩略图</title> <meta content="a href="tag-126-1.html""">microsoft</a> <a href="tag-39-1.html">Visual Studio</a>.NET 7.0" name="GENERATOR"> <meta content="a href="tag-50-1.html"""><a href="leibie-7-1.html">Visual Basic</a></a> 7.0" name="CODE_LANGUAGE"> <meta content="a href="tag-133-1.html"""><a href="tag-10-1.html"><a href="leibie-1-1.html">Java</a></a>script</a>" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/a href="tag-340-1.html""">intel</a>lisense/<a href="tag-393-1.html">IE5</a>" name="vs_targetSchema"> </HEAD> <body> <asp id="Label1" runat="server"></asp> <fo href="tag-415-1.html">RM </a>id="Form1" method="post" runat="server" enctype="multipart/form-data"> <INPUT type="file" name="file" width="600"><br /><br /> <asp id="Button1" runat="server"></asp> </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 href="tag-268-1.html">bug</a>gerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.<a href="tag-252-1.html">object</a>, 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 exit sub end if end sub private function as href="tag-73-1.html">select</a> 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<a href="tag-418-1.html">文件</a> '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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10752019/viewspace-957924/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7895717