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

來源:互聯網
上載者:User
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)

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。