Part I:創(chuàng)建數(shù)據(jù)庫(kù)
代碼:
CREATE TABLE imgProcess(
pic_id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
pic_title VARCHAR(50) NULL,
pic_type VARCHAR(50) NULL,
pic_content IMAGE NULL
)
Part II:圖片存入數(shù)據(jù)庫(kù)
代碼(.aspx):
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
<asp:TextBox id="txtTitle" runat="server"></asp:TextBox><br>
<INPUT id="myImageFile" type="file" name="File1" runat="server">
<asp:button id="btnUpload" runat="server" Text="Upload"></asp:button><br>
<br>
<asp:HyperLink id="HyperLink1" runat="server" NavigateUrl="ReadAllImagesFromDB.aspx">ReadAllImagesPage</asp:HyperLink>
</form>
代碼(.vb)

Private Sub btnUpload_Click()Sub btnUpload_Click()Sub btnUpload_Click()Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim intImageSize As Int32
Dim strImageType As String
Dim imageStream As Stream
intImageSize = myImageFile.PostedFile.ContentLength

strImageType = myImageFile.PostedFile.ContentType
imageStream = myImageFile.PostedFile.InputStream
If intImageSize <= 0 Then
Response.Write("Picture can't be null")
Return
End If
Dim imageContent(intImageSize) As Byte
Dim intStatus As Int32
intStatus = imageStream.Read(imageContent, 0, intImageSize)
Dim dbAccess As New CommonDB
Try
dbAccess.OpenConnection()
dbAccess.Command.CommandText = "INSERT INTO imgProcess(pic_title,pic_type,pic_content) VALUES (@imgtitle,@imgtype,@imgdata )"
dbAccess.Command.Parameters.Add("@imgtitle", SqlDbType.VarChar, 50).Value = txtTitle.Text.Trim
dbAccess.Command.Parameters.Add("@imgtype", SqlDbType.VarChar, 50).Value = strImageType
dbAccess.Command.Parameters.Add("@imgdata", SqlDbType.Image).Value = imageContent
dbAccess.Command.ExecuteNonQuery()
Response.Write("Success!")
Catch ex As Exception
Response.Write(dbAccess.ErrorMessage)
Finally
dbAccess.CloseConnection()
End Try
End Sub
Part III 圖片取出數(shù)據(jù)庫(kù),顯示在頁(yè)面
代碼(show.aspx)
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="Grid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="pic_title" HeaderText="Title"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Photo">
<ItemTemplate>
<img src='<%# "GetImg.ashx?ID=" & DataBinder.Eval(Container.DataItem,"pic_id")%>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Photo2">
<ItemTemplate>
<img src='<%# "GetImg.aspx?ID=" & DataBinder.Eval(Container.DataItem,"pic_id")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
代碼(show.vb)

Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
ReBind()
End If
End Sub


Private Sub ReBind()Sub ReBind()
Dim dbAccess As New CommonDB
Try
dbAccess.OpenConnection()
dbAccess.Command.CommandText = "SELECT pic_id,pic_title FROM imgProcess "
Grid1.DataSource = dbAccess.Command.ExecuteReader
Grid1.DataBind()
Catch ex As Exception
Response.Write(ex.ToString)
Finally
dbAccess.CloseConnection()
End Try
End Sub
代碼(getImg.ashx)

<%
@ WebHandler Language="vb" Class="ASPNETPP.GetImg" %>
代碼(getImg.ashx.vb)
Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO


Public Class GetImgClass GetImg : Implements IHttpHandler

Public Sub ProcessRequest()Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim strId As String = Convert.ToString(context.Request.Item("id"))
If strId Is Nothing Then
Return
End If
Dim stream As New MemoryStream
Dim bm As Bitmap
Dim img As Image
Dim dbAccess As New CommonDB
Try
dbAccess.OpenConnection()
dbAccess.Command.CommandText = "SELECT pic_content FROM imgProcess WHERE pic_id=" & strId
Dim blob() As Byte = dbAccess.Command.ExecuteScalar()
stream.Write(blob, 0, blob.Length)
bm = New Bitmap(stream)
Dim width As Int16 = 200
Dim height As Int16 = bm.Height / bm.Height * width
img = bm.GetThumbnailImage(width, height, Nothing, IntPtr.Zero)
img.Save(context.Response.OutputStream, ImageFormat.Jpeg)
Catch ex As Exception
context.Response.Write(ex.ToString)
Finally
dbAccess.CloseConnection()
If Not (img Is Nothing) Then
img.Dispose()
End If
If Not (bm Is Nothing) Then
bm.Dispose()
End If
stream.Close()
End Try
End Sub


Public ReadOnly Property IsReuseable()Property IsReuseable() As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
代碼(getImg.aspx)
<form id="Form1" method="post" runat="server">
</form>
代碼(getImg.aspx.vb)

Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dbAccess As New CommonDB
Try
dbAccess.OpenConnection()
dbAccess.Command.CommandText = "SELECT * FROM imgProcess WHERE pic_id = " & Request.QueryString("id").Trim
dbAccess.Reader = dbAccess.Command.ExecuteReader()
Do While (dbAccess.Reader.Read())
Response.ContentType = dbAccess.Reader.Item("pic_type")
Response.BinaryWrite(dbAccess.Reader.Item("pic_content"))
Loop
Catch ex As Exception
Response.Write(dbAccess.ErrorMessage)
Finally
dbAccess.CloseConnection()
End Try
End Sub