国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用asp.net實(shí)現(xiàn)將上傳的圖片變小存入數(shù)據(jù)庫!(暑假里就開始想做的,很興奮了)

2024-07-10 12:58:24
字體:
供稿:網(wǎng)友
我校的學(xué)生網(wǎng):www.ehang.com.cn中有校友錄和圖庫等的項目有上傳圖片的功能,我將圖片數(shù)據(jù)存入數(shù)據(jù)庫中,需要將圖片變小后和原圖片一塊存入數(shù)據(jù)庫中,這樣先可顯示很多小圖,點(diǎn)擊后再顯示大圖!在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()   '大圖片數(shù)據(jù)、小圖片數(shù)據(jù)
        dim pic as system.data.sqlclient.sqlparameter, picsmall as system.data.sqlclient.sqlparameter
        '檢察上傳文件是否合標(biāo)準(zhǔn),check函數(shù)是我根據(jù)網(wǎng)站需要寫的了
        if check(file1.postedfile.filename) <> "ok" then
            response.write(check(file1.postedfile.filename))
            exit sub
        end if
        '設(shè)置臨時路徑,為了防止多用戶訪問時的沖突,設(shè)了一個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()
        '讀取圖片的數(shù)據(jù)
        redim bigdata((me.file1.postedfile.inputstream.length)
        me.file1.postedfile.inputstream.read(bigdata, 0, ubound(bigdata))  '將原圖片數(shù)據(jù)讀到bigdata中
        '改變圖片的大小
        image = system.drawing.image.fromstream(me.file1.postedfile.inputstream)
'newimage里面的size也可另外設(shè)置,我只用了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()
'讀取臨時文件數(shù)據(jù)到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
        '將數(shù)據(jù)加入數(shù)據(jù)庫中
'由于數(shù)據(jù)庫中有image字段,我用sql插不進(jìn)去,就用一個存儲過程
'請教各位大蝦用sql語句插入有image字段的表該怎么寫
'用insert into talbe(pic,picsmall) values("&bigdata&","&smalldata&")這樣不行,用'"&bigdata&"'也不行呀!
        sqlconn = new system.data.sqlclient.sqlconnection(connstr)  '可自己設(shè)置connstr連接數(shù)據(jù)庫服務(wù)器
        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)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐亭县| 纳雍县| 新巴尔虎右旗| 常宁市| 安康市| 桃江县| 吴川市| 怀集县| 扎赉特旗| 阿克苏市| 泽普县| 徐州市| 舟曲县| 普安县| 安阳市| 密山市| 常山县| 太保市| 青岛市| 胶州市| 延津县| 电白县| 辰溪县| 汉中市| 昌图县| 甘德县| 贡山| 石台县| 屯昌县| 平武县| 葵青区| 星座| 右玉县| 琼中| 临泽县| 蛟河市| 霞浦县| 汨罗市| 沂南县| 兴仁县| 明光市|