byte[]s, images and bitmaps with ASP.NET

G

Guest

I am working on a solution to store jpeg images in our sql server. I am able
to retrieve the byte array from the database, I can make it into a bitmap but
I cannot figure out how to put the bitmap within a control for rendering on a
webpage. The image control requres a url to render an image, I do not want to
have to create the file on the client's machine.

Is there a control that would work better than image so that I could render
the jpeg to the web page, without using Response.OutputStream.Write(which
clears the rest of the page)??

Thanks in advance for any help.
 
G

Guest

Why don't you create a Custom Web Control to do that. That way you won't care
about clearing the output (for that control) and the code is reusable for
future implementations.

Regards,
-Visar
 
E

Eliyahu Goldin

You need to make a separate page called, say, GetImage.aspx. The page will
accept a query parameter with meaning "image id". In the places where you
need to specify an image url put something like
ImageUrl="GetImage.aspx?id=xxx"

In GetImage.aspx retrieve the image by id and stream it down in
Response.OutputStream. It will land in exact place in the control that
ordered it in its ImageUrl property.

Eliyahu
 
P

Phillip Ian

I just happened to have one of these laying around...if you're in VB,
you're in luck. :)

Imports System.Data.SqlClient

Public Class getimage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Response.Clear()
Response.ContentType = "image/jpeg"

Dim imageid As Integer
Dim wantsthumb As Boolean
Try
imageid = Request.Params("id")
wantsthumb = Request.Params("thumb") = "yes"
Catch
Response.BinaryWrite(Nothing)
End Try

Dim imagedata() As Byte = GetProductImage(imageid, wantsthumb)
If imagedata Is Nothing Then
Response.Redirect("images/noimage.jpg")
Return
Else
Response.BinaryWrite(imagedata)
End If

Response.End()
End Sub

Private Function GetProductImage(ByVal AProduct As Integer, Optional
ByVal GetThumbnail As Boolean = False) As Byte()
Dim cn As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("", cn)

If GetThumbnail Then
cmd.CommandText = "select Thumbnail as data from tblImages where
ProdID=" & AProduct
Else
cmd.CommandText = "select FullImage as data from tblImages where
ProdID=" & AProduct
End If

cn.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader

If Not rdr.HasRows Then
rdr.Close()
cn.Close()
Return Nothing
End If

rdr.Read()
Dim imagedata() As Byte = rdr("data")
rdr.Close()
cn.Close()

Return imagedata
End Function

End Class
 
Joined
Aug 21, 2008
Messages
1
Reaction score
0
I had the same problem and this helped me out. Though I took a different approuch with the EnterpriseLibrary. Your a life saver. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top