ARGH! Images to and from SQL Server

J

Jordan Bowness

Can anyone tell me why this doesn't work? It appears to post, but my
..asp page I use to retieve the image pulls up nothing.

Here's the posting snippet:

=======
begin
=======



'\\ turn our posted file into an image:
Dim ourImage As System.Drawing.Image =
System.Drawing.Image.FromStream(imagepost.PostedFile.InputStream)

Dim streamImage As New System.IO.MemoryStream


'\\ read the image into streams then into byte arrays:
ourImage.Save(streamImage,
System.Drawing.Imaging.ImageFormat.Gif)
Dim b(streamImage.Length - 1) As Byte
streamImage.Read(b, 0, streamImage.Length)


Dim sql As String = "INSERT INTO images(imageguid,
imagedata, imagedataThumb) VALUES(newid(),@BlobData,@BlobDataThumb) "

sql = "INSERT INTO images(imageguid, imagedata)
VALUES(newid(),@BlobData) "
Dim parmBlob As New SqlClient.SqlParameter
parmBlob.SqlDbType = SqlDbType.Binary
parmBlob.Value = b
Dim cmd As New SqlClient.SqlCommand(sql, cnn)

cmd.Parameters.Add("@BlobData", SqlDbType.Image)
cmd.Parameters("@BlobData").Value = b

cnn.Open()
cmd.ExecuteNonQuery()
=====
end
=====


and here is the source of the asp page which retrieves the image:


=====
begin
=====

<%
Response.Expires = 0
Response.Buffer = True
Response.Clear
response.ContentType = "image/jpeg"

strImageGUID = getRequest("ImageGUID")

Dim cnn
set cnn = server.createobject("ADODB.Connection")
cnn.Open "Provider=SQLOLEDB;data Source=(local); initial
catalog=ImageDB"

Dim rs
set rs = server.createobject("ADODB.Recordset")
rs.Open "Select * from Images where cast(imageguid as nvarchar(100)) =
'" & strImageGUID & "'", cnn

if rs.eof then response.end
'response.Write "FOUND IT"
response.binarywrite rs.Fields("ImageData").Value

rs.close
cnn.close

%>

<%
function GetRequest(a_RequestName)

dim strTempValue

strTempValue = request.form(a_RequestName)
if len(strTempValue) < 1 then strTempValue =
request.querystring(a_RequestName)

GetRequest = strTempValue

end function
%>


=====
end
=====




Any help would be greatly appreciated!
 
G

Guest

I use a custom HttpHandler to show my images from a database. The following class is an example

Imports System.Drawing.Imagin
Imports System.We

Public Class ImageRequestHandle
Implements IHttpHandle

'Notice ProcessRequest is the only metho
'exposed by the IHttpHandle
Public Sub ProcessRequest(ByVal context As HttpContext) Implements System.Web.IHttpHandler.ProcessReques
Dim strFilename As Strin
Tr
Dim strString As String = "yes

'ADD CUSTOM AUTHENICATION HERE (IF ANY

If strString = "yes" The
Dim strFileType As Strin
Dim appsrv As New SecurityBrokerCentralAdmin.CentralAdmin(

'grab filename from end of UR
Dim strArray As String() = Split(context.Request.RawUrl, "/"
strFilename = strArray(UBound(strArray)
strArray.Initialize(
strArray = Split(strFilename, "."
strFilename = strArray(0
strFileType = strArray(1).ToLowe
strArray = Nothin

'Dim intPointer As Intege
'Dim intFlag As Integer =
'intPointer = Len(strFilename
'Do While intFlag <> 1 And intPointer >
' If Right(Left(strFilename, intPointer), 1) = "/" The
' intFlag =
' Els
' intPointer = intPointer -
' End I
'Loo
'strFilename = Right(strFilename, (Len(strFilename) - intPointer)
Dim byteArray() As Byt
If strFileType = "jpg" Or strFileType = "jpeg" The
byteArray = appsrv.GetApplicationIcon(strFilename
context.Response.ClearContent(
context.Response.ClearHeaders(
context.Response.AddHeader("content-length", byteArray.Length.ToString
context.Response.ContentType = "image/jpeg
context.Response.BinaryWrite(byteArray
Els
If strFileType = "gif" The
byteArray = appsrv.GetApplicationIcon(strFilename
context.Response.ClearContent(
context.Response.ClearHeaders(
context.Response.AddHeader("content-length", byteArray.Length.ToString
context.Response.ContentType = "image/gif
context.Response.BinaryWrite(byteArray
Els
context.Response.Write("<b>ACCESS TO THIS TYPE OF RESOURCE IS DENIED</b>"
End I
End I

Els
context.Response.Write("<b>ACCESS TO THIS RESOURCE IS DENIED</b>"
End I
Catch ex As Exceptio
context.Response.Write("The resource '" & strFilename & "' was not found."
'TODO - email content owner with URL
End Tr
End Su

'By calling IsReusable, an HTTP factory can query a handler to
'determine whether the same instance can be used to service
'multiple requests

Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusabl
Ge
Return Tru
End Ge
End Propert
End Clas

In web.config I add the following lines

<httpHandlers><add verb="*" path="*ximages/*" type="SecurityAdmin.Web.ImageRequestHandler, SecurityAdmin.Web"/></httpHandlers

I then concatenate ximages/ onto the front of the of the database id relating to the image that I want to show

I hope this helps

James :)
 
J

Jordan Bowness

Thanks, James... I think that's doing just about the same thing as my
asp page in terms of retreiving the image. Very nice to see this
elegant way of doing it in .net

It still seems like I'm having trouble storing the image in SQL Server
properly. Does anyone happen to have a snippet of code that works or
can spot any errors in what I posted above?


Greatly appreciated.

================
Jordan Bowness
================
 
J

Jordan Bowness

I found out where my trouble is coming from, but I'm as yet unsure how
to resolve the matter.

I'm experiencing grief when going from the stream to the image to the
bit array. Trying to post bytes() to SQL Server only works with the
following example...

This works:
------------------------------------------------------
Dim len As Integer = imagepost.PostedFile.ContentLength
Dim bytes(len) As Byte
imagepost.PostedFile.InputStream.Read(bytes, 0, len)
------------------------------------------------------


This doesn't:
------------------------------------------------------
Dim stream As Stream = New System.IO.MemoryStream
Dim bmpPostedImage As System.Drawing.Bitmap =
System.Drawing.Bitmap.FromStream(imagepost.PostedFile.InputStream)

bmpPostedImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)

'test the image:
bmpPostedImage .Save(MapPath("/imagefromstream.jpg"))

Dim len As Integer = stream.Length
Dim bytes(len) As Byte
stream.Read(bytes, 0, len)
------------------------------------------------------

bytes() in the 2nd example winds up in the database as
0x00000000000000000000000 (etc.)

It's boggled me, because imagefromstream.jpg saves A-OK.



egads!



================
Jordan Bowness
================
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top