Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ARGH! Images to and from SQL Server

Reply
Thread Tools

ARGH! Images to and from SQL Server

 
 
Jordan Bowness
Guest
Posts: n/a
 
      02-17-2004
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.PostedFi le.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!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmFtZXMgQ3Vsc2hhdw==?=
Guest
Posts: n/a
 
      02-17-2004
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
 
Reply With Quote
 
 
 
 
Jordan Bowness
Guest
Posts: n/a
 
      02-17-2004
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
================

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Jordan Bowness
Guest
Posts: n/a
 
      02-17-2004
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.PostedF ile.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
================

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help. Getting a An error has occurred while establishing a connectionto the server. When connecting to SQL Server 2005, this failure may be causedby the fact that under the default settings SQL Server does not allow remote aboutjav.com@gmail.com ASP .Net 0 05-03-2008 12:43 PM
Do the Self-Paced Training Kits: Microsoft SQL Server 2000 include Eval copy of SQL Server? Brian Whiting Microsoft Certification 2 12-29-2005 04:24 AM
sql server express vs sql server 2000 code Daves ASP .Net 1 06-13-2005 12:24 PM
How to read an SQL Server into a ASP page and then change, add, delete and write it back to SQL Server Belinda ASP General 4 06-11-2004 12:16 PM
Can't connect to SQL Server, using Windows Authentication users of SQL server? help =?Utf-8?B?UmV6YQ==?= ASP .Net 3 06-07-2004 06:42 PM



Advertisments