本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。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" type="file" size="10" name="file1" runat="server"> <asp:button id="cmdupload" 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)