asp.net实现将上传的图片变小存入数据库

时间:2006/5/28 18:48:09      阅读:4663          

程序代码:

       校友录和图库等的项目有上传图片的功能,我将图片数据存入数据库中,需要将图片变小后和原图片一块存入数据库中,这样先可显示很多小图,点击后再显示大图!在asp.net中是这么做的:

 changimage.aspx中代码:

 1<%@ Page Language="vb" AutoEventWireup="false" Codebehind="changimage.aspx.vb" Inherits="uploadimage.changimage"%>
 2DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 3<HTML>
 4    <HEAD>
 5        <title>title>
 6        <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
 7        <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
 8        <meta name="vs_defaultClientScript" content="JavaScript">
 9        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10    HEAD>
11    <body MS_POSITIONING="GridLayout">
12        <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
13            <INPUT id="File1" type="file" size="10" name="File1" runat="server">
14            <asp:Button id="cmdupload" runat="server" Text="上传图片">asp:Button>
15        form>
16    body>
17HTML>
18

changimage.aspx.vb中代码如下:

  1Public Class changimage
  2    Inherits System.Web.UI.Page
  3    Protected WithEvents cmddemo As System.Web.UI.WebControls.Button
  4    Protected WithEvents cmdupload As System.Web.UI.WebControls.Button
  5    Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
  6    Protected WithEvents SqlComm As System.Data.SqlClient.SqlCommand
  7    Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
  8
  9Web Form Designer Generated Code
 23
 24    Private Sub Page_Load(ByVal sender As System.Object, _
 25 ByVal e As System.EventArgs) Handles MyBase.Load
 26    End Sub
 27   
 28    Private Sub cmdupload_Click(ByVal sender As System.Object, _
 29 ByVal e As System.EventArgs) Handles cmdupload.Click
 30        Dim image As System.Drawing.Image, newimage As System.Drawing.Image
 31        Dim callb As System.Drawing.Image.GetThumbnailImageAbort
 32        Dim f As System.IO.File, fs As System.IO.FileStream
 33        Dim temppath As String 
 34        Dim bigdata As Byte(), smalldata As Byte()   '大图片数据、小图片数据
 35        Dim pic As System.Data.SqlClient.SqlParameter, picsmall As System.Data.SqlClient.SqlParameter
 36        '检察上传文件是否合标准,check函数是我根据网站需要写的了
 37        If check(File1.PostedFile.FileName) <> "ok" Then
 38            Response.Write(check(File1.PostedFile.FileName))
 39            Exit Sub
 40        End If
 41        '设置临时路径,为了防止多用户访问时的冲突,设了一个application对象
 42        If Application("image") = "" Then
 43            Application("image") = 0
 44        End If
 45        Application.Lock()
 46        temppath = Server.MapPath(CStr(Application("image")))  '临时路径
 47        Application("image") = Application("image") + 1
 48        Application.UnLock()
 49        '读取图片的数据
 50        ReDim bigdata((Me.File1.PostedFile.InputStream.Length)
 51        Me.File1.PostedFile.InputStream.Read(bigdata, 0, UBound(bigdata))  '将原图片数据读到bigdata中
 52        '改变图片的大小
 53        image = System.Drawing.Image.FromStream(Me.File1.PostedFile.InputStream)
 54'newimage里面的size也可另外设置,我只用了80*60和60*80两种
 55        If image.Width > image.Height Then
 56            newimage = image.GetThumbnailImage(80, 60, callb, New System.IntPtr(0))
 57        Else
 58            newimage = image.GetThumbnailImage(60, 80, callb, New System.IntPtr(0))
 59        End If
 60        image.Dispose()   
 61'将新图片及图片变小后存到临时路径中
 62        newimage.Save(temppath, System.Drawing.Imaging.ImageFormat.Jpeg)
 63        newimage.Dispose()
 64'读取临时文件数据到smalldata中
 65        fs = New System.IO.FileStream(temppath, IO.FileMode.Open, IO.FileAccess.Read)
 66        ReDim smalldata(fs.Length)
 67        fs.Read(smalldata, 0, UBound(smalldata))
 68        fs.Close()
 69'上述获得小图片的方法我原本想用system.io.memorystream的,可是行不通:代码如下:
 70'dim m as system.io.memorystream
 71'm=new system.io.memorystream()
 72'newimage.save(m,System.Drawing.Imaging.ImageFormat.Jpeg)
 73'redim smalldata(m.length)
 74'm.read(smalldata,0,m.length)
 75'可是上述方法读出来的smalldata全是空的,不知道原因,请指教
 76        '删除临时文件
 77        If f.Exists(temppath) Then
 78            f.Delete(temppath)
 79        End If
 80        '将数据加入数据库中
 81'由于数据库中有image字段,我用sql插不进去,就用一个存储过程
 82'请教各位大虾用sql语句插入有image字段的表该怎么写
 83'用insert into talbe(pic,picsmall) values("&bigdata&","&smalldata&")这样不行,用'"&bigdata&"'也不行呀!
 84        SqlConn = New System.Data.SqlClient.SqlConnection(connstr)  '可自己设置connstr连接数据库服务器
 85        SqlComm = New System.Data.SqlClient.SqlCommand()
 86        SqlComm.CommandType = CommandType.StoredProcedure
 87        SqlComm.CommandText = "dbo.image"
 88        pic = New System.Data.SqlClient.SqlParameter("@pic", SqlDbType.Image)
 89        pic.Value = bigdata
 90        picsmall = New System.Data.SqlClient.SqlParameter("@picsmall", SqlDbType.Image)
 91        picsmall.Value = smalldata
 92        SqlComm.Parameters.Add(pic)
 93        SqlComm.Parameters.Add(picsmall)
 94        SqlComm.Connection = SqlConn
 95        SqlComm.Connection.Open()
 96        SqlComm.ExecuteNonQuery()
 97        SqlComm.Connection.Close()
 98        SqlComm.Dispose()
 99        SqlConn.Dispose()
100    End Sub
101End Class
102

dbo.image存储过程如下:

1create proc dbo.image
2  @pic image,
3  @picsmall image
4as
5  insert into table(pic,picsmall) values (@pic,@picsmall)
评论
  • Re:asp.net实现将上传的图片变小存入数据库  (2006/7/17 11:35:14) by 111 
    可以先说一下您的思想再写代码吗?
  • Re:asp.net实现将上传的图片变小存入数据库  (2006/8/16 16:01:54) by 飘萍 
        sql server的?又access的吗?
  • Re:asp.net实现将上传的图片变小存入数据库  (2010/11/27 10:06:29) by midsummerlgy 

    Access的有么?谢谢

标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.