设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
1#
发表于 2007-1-29 22:54:00 | 显示全部楼层
图片文件的扩展名通过IO来获取应该更好些,有些图片文件的扩展名可能是4个字母,比如:jpeg
2#
发表于 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

复制代码
3#
发表于 2008-3-11 10:08:38 | 显示全部楼层
使用方法:

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

本版积分规则

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

GMT+8, 2024-5-16 11:08 , Processed in 0.085076 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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