分享
 
 
 

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

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有