熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .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


From:http://tw.wingwit.com/Article/program/net/201311/15750.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.