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

首頁 > 編程 > .NET > 正文

用asp.net實現將上傳的圖片變小存入數據庫

2024-07-10 13:07:03
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者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)
    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 宜宾县| 宜阳县| 正蓝旗| 辽阳市| 广昌县| 瑞丽市| 南城县| 东城区| 松潘县| 右玉县| 溆浦县| 龙陵县| 石嘴山市| 兰州市| 江北区| 芜湖县| 海原县| 精河县| 恭城| 康马县| 连城县| 宜兰市| 思南县| 新巴尔虎右旗| 双峰县| 盐源县| 吴堡县| 通化县| 闸北区| 广德县| 宁德市| 信丰县| 穆棱市| 敦化市| 凤冈县| 东台市| 获嘉县| 永昌县| 东乡族自治县| 中牟县| 监利县|