用asp.net实现将上传的图片变小存入数据库!(暑假里就开始想做的,很兴奋了)

原创 2005年04月26日 01:32:00
我校的学生网:www.ehang.com.cn中有校友录和图库等的项目有上传图片的功能,我将图片数据存入数据库中,需要将图片变小后和原图片一块存入数据库中,这样先可显示很多小图,点击后再显示大图!在asp.net中是这么做的:
changimage.aspx中代码:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="changimage.aspx.vb" Inherits="uploadimage.changimage"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title></title>
        <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
        <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
            <FONT face="宋体"><INPUT id="File1" style="Z-INDEX: 101; LEFT: 291px; WIDTH: 180px; POSITION: absolute; TOP: 119px; HEIGHT: 45px" type="file" size="10" name="File1" runat="server">                        <asp:Button id="cmdupload" style="Z-INDEX: 103; LEFT: 402px; POSITION: absolute; TOP: 194px" runat="server" Text="上传图片" Width="81px" Height="42px"></asp:Button>
            </FONT>
        </form>
    </body>
</HTML>
changimage.aspx.vb中代码如下:
Public Class changimage
    Inherits System.Web.UI.Page
    Protected WithEvents cmddemo As System.Web.UI.WebControls.Button
    Protected WithEvents cmdupload As System.Web.UI.WebControls.Button
    Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
    Protected WithEvents SqlComm As System.Data.SqlClient.SqlCommand
    Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.SqlConn = New System.Data.SqlClient.SqlConnection()
        Me.SqlComm = New System.Data.SqlClient.SqlCommand()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
       Form Designer
       InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
   
    Private Sub cmdupload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupload.Click
        Dim image As System.Drawing.Image, newimage As System.Drawing.Image
        Dim callb As System.Drawing.Image.GetThumbnailImageAbort
        Dim f As System.IO.File, fs As System.IO.FileStream
        Dim temppath As String
        Dim bigdata As Byte(), smalldata As Byte()   '大图片数据、小图片数据
        Dim pic As System.Data.SqlClient.SqlParameter, picsmall As System.Data.SqlClient.SqlParameter
        '检察上传文件是否合标准,check函数是我根据网站需要写的了
        If check(File1.PostedFile.FileName) <> "ok" Then
            Response.Write(check(File1.PostedFile.FileName))
            Exit Sub
        End If
        '设置临时路径,为了防止多用户访问时的冲突,设了一个application对象
        If Application("image") = "" Then
            Application("image") = 0
        End If
        Application.Lock()
        temppath = Server.MapPath(CStr(Application("image")))  '临时路径
        Application("image") = Application("image") + 1
        Application.UnLock()
        '读取图片的数据
        ReDim bigdata((Me.File1.PostedFile.InputStream.Length)
        Me.File1.PostedFile.InputStream.Read(bigdata, 0, UBound(bigdata))  '将原图片数据读到bigdata中
        '改变图片的大小
        image = System.Drawing.Image.FromStream(Me.File1.PostedFile.InputStream)
'newimage里面的size也可另外设置,我只用了80*60和60*80两种
        If image.Width > image.Height Then
            newimage = image.GetThumbnailImage(80, 60, callb, New System.IntPtr(0))
        Else
            newimage = image.GetThumbnailImage(60, 80, callb, New System.IntPtr(0))
        End If
        image.Dispose()   
'将新图片及图片变小后存到临时路径中
        newimage.Save(temppath, System.Drawing.Imaging.ImageFormat.Jpeg)
        newimage.Dispose()
'读取临时文件数据到smalldata中
        fs = New System.IO.FileStream(temppath, IO.FileMode.Open, IO.FileAccess.Read)
        ReDim smalldata(fs.Length)
        fs.Read(smalldata, 0, UBound(smalldata))
        fs.Close()
'上述获得小图片的方法我原本想用system.io.memorystream的,可是行不通:代码如下:
'dim m as system.io.memorystream
'm=new system.io.memorystream()
'newimage.save(m,System.Drawing.Imaging.ImageFormat.Jpeg)
'redim smalldata(m.length)
'm.read(smalldata,0,m.length)
'可是上述方法读出来的smalldata全是空的,不知道原因,请指教
        '删除临时文件
        If f.Exists(temppath) Then
            f.Delete(temppath)
        End If
        '将数据加入数据库中
'由于数据库中有image字段,我用sql插不进去,就用一个存储过程
'请教各位大虾用sql语句插入有image字段的表该怎么写
'用insert into talbe(pic,picsmall) values("&bigdata&","&smalldata&")这样不行,用'"&bigdata&"'也不行呀!
        SqlConn = New System.Data.SqlClient.SqlConnection(connstr)  '可自己设置connstr连接数据库服务器
        SqlComm = New System.Data.SqlClient.SqlCommand()
        SqlComm.CommandType = CommandType.StoredProcedure
        SqlComm.CommandText = "dbo.image"
        pic = New System.Data.SqlClient.SqlParameter("@pic", SqlDbType.Image)
        pic.Value = bigdata
        picsmall = New System.Data.SqlClient.SqlParameter("@picsmall", SqlDbType.Image)
        picsmall.Value = smalldata
        SqlComm.Parameters.Add(pic)
        SqlComm.Parameters.Add(picsmall)
        SqlComm.Connection = SqlConn
        SqlComm.Connection.Open()
        SqlComm.ExecuteNonQuery()
        SqlComm.Connection.Close()
        SqlComm.Dispose()
        SqlConn.Dispose()
    End Sub
End Class

dbo.image存储过程如下:
create proc dbo.image
  @pic image,
  @picsmall image
as
  insert into table(pic,picsmall) values (@pic,@picsmall)

实现ASP.NET数据库图片上传与读取的功能

以下代码实现ASP.NET数据库图片上传与读取的功能,详细见代码内部的注解。 开发环境:Window 2000、SQLServer2000、.Net Framework SDK正式版 开发...

ASP.NET使用Jcrop插件实现图片上传裁剪功能

一、需求 1.需求:在Web端实现上传图片功能的基础上,增加预览图片并裁剪图片功能。 2.未更改之前如下图所示: 3.更改后的功能,当点击浏览(原生input type='file'控件)选...

asp.net+js+ajax实现手机移动端页面预览、剪裁、上传头像图片

参考资料:支持移动设备触摸图片裁剪的jQuery插件 移动端html 测试html --> ...

Asp.net MVC 实现图片上传剪切

首先上页面 1 2 3 4 5 Index 6 css" rel="stylesheet" type="text/css" /> 7 jqu...

ASP.NET(C#)实现一次性上传多张图片(多个文件)

http://www.cnblogs.com/Fooo/archive/2007/12/11/991033.html http://www.w3.org/TR/xhtml1/DTD/xhtm...

使用Jcrop实现图片上传、剪裁和保存(ASP.NET)

Jcrop是一款jquery图片裁剪插件,可以实现图片在线裁剪,达到和图像软件处理的效果,界面和操作也相当的人性化,只需要拖曳鼠标即可完成,部署到服务器也非常简单,在网站建设过程中轻易美化表单,并且改...

在线编辑器Kindedior在ASP.NET下实现图片批量上传

前一段时间做网站开发的时候需要用到在线编辑器实现批量上传图片,在网上找了好多资料,发现kindeditor可以满足需求,由于网上关于kindeditor的用法参差不一,现在特意将kindeditor的...

AspSHOP将上传图片存入ACCESS数据库

  • 2009年04月26日 22:54
  • 245KB
  • 下载

ASP.NET控件FileUpload实现Excel文件内容上传到数据库

实现思路:Excel的内容并不可以直接上传到数据库,所以先将Excel内容读取出来转化为DataSet,然后可以得到DataTable,遍历每一行,存到数据库里就好了。 前台只放了两个控件: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用asp.net实现将上传的图片变小存入数据库!(暑假里就开始想做的,很兴奋了)
举报原因:
原因补充:

(最多只允许输入30个字)