PDA

View Full Version : سوال: مشکل در لود عکس



karim orooji
دوشنبه 06 اردیبهشت 1389, 18:32 عصر
با سلام به دوستان گلم من به کمک این کد عکس رو به صورت باینری در بانک ذخیره میکنم


Sub picture()
Dim upLength As Integer = up.PostedFile.ContentLength
Dim picByte As Byte() = New Byte(upLength - 1) {}
Dim con As New SqlConnection
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\bank.mdf;Integrated Security=True;User Instance=True"

Dim com As New SqlCommand
com.Connection = con
com.CommandText = "insert into picture values(@pic,@user_name)"

com.Parameters.AddWithValue("@pic", picByte)
com.Parameters.AddWithValue("@user_name", txt_name.Text)
con.Open()
com.ExecuteNonQuery()
con.Close()
End Sub

حالا چطوری لودش کنم

salehbagheri
دوشنبه 06 اردیبهشت 1389, 23:25 عصر
ابتدا اين تابع رو براي خواندن عكس از DataBase مينويسيد!


Public Shared Function GetImage(ByVal ID As Integer) As Byte()
Dim Connection As New SqlConnection(Settings.ConnectionString)
Dim Command As New SqlCommand("MP_GetImage", Connection)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add("ID", SqlDbType.Int).Value = ID
Try
Connection.Open()
Dim Reader As SqlDataReader = Command.ExecuteReader()
Dim Item As Byte() = Nothing
If Reader.HasRows Then
Reader.Read()
Item = CType(Reader("Image"), Byte())
End If
Return Item
Connection.Close()
Catch ex As Exception
Throw New InvalidOperationException(ex.Message)
Finally
Connection.Dispose()
Command.Dispose()
End Try
End Function


سپس در فايل Handler.ashx كد زير رو وارد ميكنيد!


Sub ProcessRequest(ByVal Context As HttpContext) Implements IHttpHandler.ProcessRequest
' Set up the response settings
Context.Response.ContentType = "Image/Jpeg"
Context.Response.Cache.SetCacheability(HttpCacheab ility.Public)
Context.Response.BufferOutput = False

Dim ID As Int32 = 1
Dim Stream As Byte() = Nothing
If ((Not (Context.Request.QueryString("ImageID")) Is Nothing) _
AndAlso (Context.Request.QueryString("ImageID") <> "")) Then
ID = [Convert].ToInt32(Context.Request.QueryString("ImageID"))
Stream = GetImage(ID)
Context.Response.BinaryWrite(Stream)
End If
End Sub


در درآخر بدين صورت عكس رو نمايش ميديد!


<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/Handler.ashx?ImageID=" + Eval("ID").ToString %>' />


دقت كنيد كه كنترل Image درون يك GridView قرارگرفته!

BahmanDB
دوشنبه 06 اردیبهشت 1389, 23:30 عصر
http://www.dreamincode.net/code/snippet2846.htm