asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

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


发布日期:2022年05月24日
 
在ASP.NET里轻松实现缩略图

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

查看例子

代码如下

Thumbnailaspx

<%@ Page Language=vb AutoEventWireup=false Codebehind=Thumbnailaspxvb

Inherits=aspxWebThumbnail %>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN

<HTML>

<HEAD>

<title>在ASPNET里轻松实现缩略图</title>

<meta content=Microsoft Visual StudioNET name=GENERATOR

<meta content=Visual Basic name=CODE_LANGUAGE

<meta content=JavaScript name=vs_defaultClientScript

<meta content=http://schemasmicrosoftcom/intellisense/ie name=vs_targetSchema

</HEAD>

<body MS_POSITIONING=GridLayout

<asp:Label id=Label runat=server></asp:Label>

<form id=Form method=post runat=server enctype=multipart/formdata

<INPUT type=file name=file width=><br><br>

<asp:Button id=Button runat=server></asp:Button>

</form>

</body>

</HTML>

后代码Thumbnailaspxvb

Imports System

Imports SystemWeb

Imports SystemDrawing

Imports SystemIO

Imports SystemDrawingImaging

Public Class Thumbnail

Inherits SystemWebUIPage

Protected WithEvents Label As SystemWebUIWebControlsLabel

Protected WithEvents Button As SystemWebUIWebControlsButton

#Region Web Form Designer Generated Code

This call is required by the Web Form Designer

<SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseInit

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 SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad

LabelText = <h>在ASPNET里轻松实现缩略图</h

ButtonText = 上载并显示缩略图

End Sub

Private Sub Button_Click(ByVal sender As Object ByVal e As SystemEventArgs) Handles ButtonClick

Dim MyFileColl As HttpFileCollection = HttpContextCurrentRequestFiles

Dim MyPostedFile As HttpPostedFile = MyFileCollItem()

If LCase(MyPostedFileContentTypeToString())IndexOf(image) < Then

ResponseWrite(无效的图形格式)

Exit Sub

End If

GetThumbNail(MyPostedFileFileName MyPostedFileContentTypeToString()_

False MyPostedFileInputStream)

End Sub

Private Function GetImageType(ByVal strContentType) As SystemDrawingImagingImageFormat

Select Case (strContentTypeToString()ToLower())

Case image/pjpeg

GetImageType = SystemDrawingImagingImageFormatJpeg

Case image/gif

GetImageType = SystemDrawingImagingImageFormatGif

Case image/bmp

GetImageType = SystemDrawingImagingImageFormatBmp

Case image/tiff

GetImageType = SystemDrawingImagingImageFormatTiff

Case image/xicon

GetImageType = SystemDrawingImagingImageFormatIcon

Case image/xpng

GetImageType = SystemDrawingImagingImageFormatPng

Case image/xemf

GetImageType = SystemDrawingImagingImageFormatEmf

Case image/xexif

GetImageType = SystemDrawingImagingImageFormatExif

Case image/xwmf

GetImageType = SystemDrawingImagingImageFormatWmf

Case Else

GetImageType = SystemDrawingImagingImageFormatMemoryBmp

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 = oImgFromFile(strFileName)

Else

oImg = oImgFromStream(ImgStream)

End If

oImg = oImgGetThumbnailImage(iWidth iheight Nothing (New IntPtr())Zero)

Dim strGuid As String = (New Guid())NewGuid()ToString()ToUpper()

Dim strFileExt As String = strFileNameSubstring(strFileNameLastIndexOf())

保存到本地

oImgSave(ServerMapPath(images) + \ + strGuid + strFileExt GetImageType(strContentType))

直接输出url文件

ResponseRedirect(images/ + strGuid + strFileExt)

以下显示在屏幕上

ResponseContentType = strContentType

Dim MemStream As New MemoryStream()

注意这里如果直接用 oImgSave(ResponseOutputStream GetImageType(strContentType))

对不同的格式可能会出错比如Png格式

oImgSave(MemStream GetImageType(strContentType))

MemStreamWriteTo(ResponseOutputStream)

End Sub

End Class

上一篇:ASP.NET设计控件净化网站语言

下一篇:ASP.NET中Cookie编程的基础知识