设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4372|回复: 6
打印 上一主题 下一主题

[ASP.NET]上传图片并生成缩略图

[复制链接]
跳转到指定楼层
1#
发表于 2006-12-30 00:30:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在《从access到asp.net》我给出了一个上传图片的函数,但是在网站运行的时候,会发现,有时我们使用列表,需要的是小图片,有时察看明细,需要的是大图片,而大图片在小图片框中会变形,不好看,而有时自动图片框又因为图片太大被涨得太大,所以最好有两种图片,分别用在不同的地方。好在asp.net2.0中为我们提供了一个image的类,可以有很多功能,包括生成缩略图,制作水印等功能。(水印的制作等有空我再贴出来)

首先我们建立一个图片上传的类:命名为:uploadimg

Public Class uploadimg
    Public file As Object       '输入图片的文件框
    Public Maximg As String     '大图片的存放地址
    Public Minimg As String     '小图片的存放地址
    Public Msglab As Label      '错误报告的标签框
    Public x As Integer         '缩略图的宽
    Public y As Integer         '缩略图的高
    Dim s1 As String     '图片的格式
    Dim s2() As String   '原图片的地址,用"\"分割为数组

    Sub upimg()
        If file.PostedFile.ContentLength > 0 Then
            Dim fileSize As Integer = file.PostedFile.ContentLength
            If fileSize < 200000 Then
                s2 = file.PostedFile.FileName.Split("\")
                s1 = Right(s2(s2.Length - 1), 4)
                If s1 = ".jpg" Or s1 = ".JPG" Or s1 = ".GIF" Or s1 = ".gif" Or s1 = ".bmp" Or s1 = ".BMP" Then
                    Dim s3 As String   '上传后图片的名称,时间+毫秒为名字
                    s3 = Format(Now(), "yyyyMMddHHmmss") & Trim(Str(DateTime.Now.Millisecond)) & s1
                    file.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(".") & "\images\" & s3)      '保存
                    Maximg = "~\images\" & s3
                    minup()
                Else
                    Msglab.Text = "图片的格式不符合要求"
                End If
            Else
                Msglab.Text = "图片大小超过了200K"
            End If
        Else
            Msglab.Text = "请选择图片"
        End If
    End Sub

    '缩略图上传
    Sub minup()
        Dim simg As System.Drawing.Image         '声明一个image对象
        Dim H As Integer                '图像高度
        Dim W As Integer                 '图像宽度
        simg = System.Drawing.Image.FromFile(file.PostedFile.FileName)     '实例化一个image对象
        H = simg.Height
        W = simg.Width
        If H > W Then
            y = y
            x = x * (W / H)
        Else
            x = x
            y = y * (H / W)
        End If
        Dim callback As GetThumbnailImageAbort
        simg = simg.GetThumbnailImage(x, y, callback, IntPtr.Zero)           '生成缩略图
        Dim s4 As String
        s4 = Format(Now(), "yyyyMMddHHmmss") & Trim(Str(DateTime.Now.Millisecond)) & "s" & s1
        simg.Save(HttpContext.Current.Server.MapPath(".") & "\images\" & s4)      '保存在指定文件夹
        Minimg = "~\images\" & s4
    End Sub
End Class

好了,让我们来测试一下:建立一个web窗体;添加一个浏览文本框,一个标签框,一个命令按钮,两个图片框

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>imgupload测试页</title>
</head>
<body>
    <form id="form1" runat="server">
        <input id="File1" type="file" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button"  />
        <aspabel ID="Label1" runat="server"></aspabel><br />
        <asp:Image ID="Image1" runat="server" /><br />
        <asp:Image ID="Image2" runat="server" />
    </form>
</body>
</html>

然后再后台代码写上调用的代码:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim meimg As New uploadimg
        meimg.file = File1
        meimg.Msglab = Me.Label1
        meimg.x = 100
        meimg.y = 100
   

本帖被以下淘专辑推荐:

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖1 订阅订阅
2#
发表于 2007-1-29 22:54:00 | 只看该作者
图片文件的扩展名通过IO来获取应该更好些,有些图片文件的扩展名可能是4个字母,比如:jpeg
3#
发表于 2008-3-9 13:58:22 | 只看该作者


  1. <%@ WebHandler Language="VB" Class="DisplaySmallPicture" %>
  2. Option Strict On
  3. Imports System
  4. Imports System.IO
  5. Imports System.Drawing
  6. Imports System.Drawing.Imaging
  7. Imports System.Web
  8. Imports System.Data
  9. Imports System.Data.SqlClient
  10. Imports System.Web.Configuration
  11. Public Class DisplaySmallPicture
  12.     Implements IHttpHandler
  13.    
  14.     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  15.         ' 取得传递进来的文件路径。
  16.         Dim PictureFilePath As String = Trim(context.Request.QueryString("PictureFilePath"))
  17.         
  18.         With context.Response
  19.             ' 改变 HTTP 头的输出格式,以便让浏览器知道所输出的文档格式是 JPEG 格式。
  20.             .ContentType = "Image/JPEG"
  21.             .Clear()
  22.             .BufferOutput = True
  23.                         
  24.             Using ms As New MemoryStream(My.Computer.FileSystem.ReadAllBytes(PictureFilePath))
  25.                 Using bmp As New Bitmap(ms)
  26.                     Dim intWidh As Integer = 100
  27.                     Dim intHeight As Integer = CInt(bmp.Height / bmp.Width * 100)
  28.                     
  29.                     ' 将照片缩小。
  30.                     Using SmallerBmp As New Bitmap(bmp, CInt(intWidh), CInt(intHeight))
  31.                         'Using SmallerBmp As New Bitmap(bmp, CInt(bmp.Width * 0.15), CInt(bmp.Height * 0.15))
  32.                         SmallerBmp.Save(.OutputStream, ImageFormat.Jpeg)
  33.                     End Using
  34.                 End Using
  35.             End Using
  36.         End With
  37.     End Sub

  38.     Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
  39.         Get
  40.             Return False
  41.         End Get
  42.     End Property
  43. End Class

复制代码
4#
发表于 2008-3-11 10:08:38 | 只看该作者
使用方法:

ImageUrl='<%# Eval("FullPath", "~/DisplaySmallPicture.ashx?PictureFilePath={0}") %>'
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 18:19 , Processed in 0.117045 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表