Bobby

聚沙成塔 集腋成裘
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net生成缩略图

Posted on 2006-06-21 13:29  Bobby  阅读(351)  评论(0编辑  收藏  举报
功能说明公司的旅游管理软件竟然用FLASH去做, 真佩服那位一天三包烟的工作狂人,别人说他工作卖力,我觉得他在卖命,呵呵,闲扯到此结束!我要实现的功能为:首先生成三个文件夹然后上传图片,重命名图片输入缩略图尺寸并生成缩略图,全部完成后关闭窗口,根据孟子E章的代码修改后即可实现,新建名为lytp的项目,项目下有两个页面pass.aspx和index.aspx,其中pass.aspx是负责传递参数测试的,实际只用到index.aspx,以下分别是index和pass的代码

Imports System
Imports System.Web
Imports System.Drawing
Imports System.IO
Imports System.Web.HttpPostedFile
Imports System.Drawing.Imaging
Public Class lytp
    
Inherits System.Web.UI.Page
    
Protected WithEvents lblfile As System.Web.UI.WebControls.Label
    
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
Protected WithEvents txt_name1 As System.Web.UI.WebControls.TextBox
    
Protected WithEvents txt_w As System.Web.UI.WebControls.TextBox
    
Protected WithEvents txt_rename As System.Web.UI.WebControls.TextBox
    
Protected WithEvents txt_h As System.Web.UI.WebControls.TextBox
    
Protected WithEvents txt_name2 As System.Web.UI.WebControls.TextBox
    
Protected WithEvents txt_name3 As System.Web.UI.WebControls.TextBox
    
Protected WithEvents file As System.Web.UI.HtmlControls.HtmlInputFile
Web Form Designer Generated Code

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text 
= "上传图片"
        
'传递参数
        txt_name1.Text = Request.QueryString("lja")
        txt_name2.Text 
= Request.QueryString("ljb")
        txt_name3.Text 
= Request.QueryString("ljc")

        txt_w.Text 
= Request.QueryString("tpw")
        txt_h.Text 
= Request.QueryString("tph")
        txt_rename.Text 
= Request.QueryString("tpmc")
    
End Sub

    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
'创建文件夹
        Dim Path1 As String = Server.MapPath(txt_name1.Text)
        
Dim Path2 As String = Server.MapPath("../" + "lytp" + "/" + txt_name1.Text + "/" + txt_name2.Text)
        
Dim Path3 As String = Server.MapPath("../" + "lytp" + "/" + txt_name1.Text + "/" + txt_name2.Text + "/" + txt_name3.Text)

        
Try
            
If txt_name1.Text = "" Or txt_name2.Text = "" Or txt_name3.Text = "" Then
                lblfile.Text 
= "请输入文件夹名称"
            
Else
                
If (Directory.Exists(Path1)) Or (Directory.Exists(Path2)) Or (Directory.Exists(Path3)) Then
                    lblfile.Text 
= "此文件夹已经存在了"
                
Else
                    Directory.CreateDirectory(Path1)
                    Directory.CreateDirectory(Path2)
                    Directory.CreateDirectory(Path3)
                    lblfile.Text 
= "已经创建文件夹"
                
End If
            
End If

        
Catch
            lblfile.Text 
= "新建文件夹错误"
        
Finally

        
End Try

        
Dim MyFileColl As HttpFileCollection = HttpContext.Current.Request.Files
        
Dim MyPostedFile As HttpPostedFile = MyFileColl.Item(0)

        
Dim x As Integer
        
Dim y As Integer
        
'定义图片的长和宽
        x = txt_w.Text
        y 
= txt_w.Text

        
If LCase(MyPostedFile.ContentType.ToString()).IndexOf("image"< 0 Then
            
      lblfile.Text = "无效的图形格式。"
                  
Exit Sub
        
End If
        GetThumbNail(MyPostedFile.FileName, x, y, MyPostedFile.ContentType.ToString(), 
False, MyPostedFile.InputStream)

        
Dim fileFullname As String = file.PostedFile.FileName
        
'取文件扩展名
        Dim fileName As String = fileFullname.Substring(fileFullname.LastIndexOf("\"+ 1)
        
Dim type As String = fileFullname.Substring(fileFullname.LastIndexOf("."+ 1)
        file.PostedFile.SaveAs(Server.MapPath(
"../" + "lytp" + "/" + txt_name1.Text + "/" + txt_name2.Text + "/" + txt_name3.Text) + "\" + txt_rename.Text + "_d." + type)
        Response.Write(
"<script>window.opener=null;window.close()</script>")
    
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(
"../" + "lytp" + "/" + txt_name1.Text + "/" + txt_name2.Text + "/" + txt_name3.Text) + "\" + txt_rename.Text + "_x" + strFileExt, GetImageType(strContentType))
    
End Sub

End Class



Public Class WebForm1
    
Inherits System.Web.UI.Page

Web 窗体设计器生成的代码

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
  
       
    
End Sub


    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim url As String
        
Dim v1 As String
        '把输入的内容传递给index.aspx
        url = "index.aspx?lja=" + TextBox1.Text + "&ljb=" + TextBox2.Text + "&ljc=" + TextBox3.Text + "&tpw=" + TextBox4.Text + "&tpmc=" + TextBox6.Text
        Response.Redirect(url)
    
End Sub

End Class

代码打包下载地址https://files.cnblogs.com/Bobby/lytp_last.rar