Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
how to fetch binary data from sqlserver image field and then convert into an image?

my vb code:

VB
Dim Ques_ImageData As Byte() = New Byte(QuestionUp.PostedFile.ContentLength - 1) {}

i have taken the Byte array to insert, but don't know how to fetch binary data and convert it into an image?
Posted
Updated 3-Aug-12 2:20am
v2

Do the following steps:

1) Create one blank aspx page
2) Assign panel as Panel1.BackImageUrl= url of that new page
3) Write code In that page that reads image bytes from database table binary field and write to that page using page.Response.BinaryWrite(bytes())

The panel will display the image rendered on that page.
 
Share this answer
 
In aspx i addded an image control

VB
<asp:image runat="server" id="ImageViewer" imageurl="~/Handler1.ashx" xmlns:asp="#unknown" />



Then in code behind
VB
Imports System.IO
Imports System.Drawing
Imports System.Data.SqlClient

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Con As New SqlConnection("data source=local;initial catalog=MYDB;User id=santhosh.kumar;password=pass@123")
        Dim cmd As New SqlCommand("select  cropImage from serverdocument where document_id=10217533", Con)
        Dim data() As Byte
        Con.Open()
        data = cmd.ExecuteScalar()

        Session("ImageBytes") = data
       
    End Sub

   
End Class


Then i added new Generic handler.

VB
Imports System.Web
Imports System.Web.Services

Public Class Handler1
    Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        If (context.Session("ImageBytes")) IsNot Nothing Then
            Dim image As Byte() = DirectCast(context.Session("ImageBytes"), Byte())
            context.Response.ContentType = "image/JPEG"
            context.Response.BinaryWrite(image)
        End If

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class


Now run. It will display the image.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900