if this were my project, I would never store image data in the database (I'm a PHP developer and know the difficulties) -- I'm firmly against that and the unnecessary complexity of doing so. however, it isn't my choice.
anyway, to answer your questions -- this is the code on my form that gets an uploaded file (there is an abstract class here that is used to only allow certain mime types, by the way):
vb Code:
Private Function GetUploadedPhoto(ByVal UploadedFile As FileUpload) As Byte()
Dim Photo As Byte() = Nothing
If UploadedFile.HasFile AndAlso UploadedFile.PostedFile IsNot Nothing Then
Dim Extension As String = Path.GetExtension(UploadedFile.PostedFile.FileName).ToLower
Dim MIMEType As String = "image/" + Extension.Replace(".", "")
If WebClient.UI.Handlers.AbstractStreamableImage.ImageFormats.ContainsValue(MIMEType) Then
Dim Length As Long = UploadedFile.PostedFile.InputStream.Length
Dim ImageBytes(Length) As Byte
UploadedFile.PostedFile.InputStream.Read(ImageBytes, 0, Length)
Photo = ImageBytes
Else
Throw New Exception("Invalid file type uploaded - only picture files are allowed.")
End If
End If
Return Photo
End Function
then, in my event handler for adding:
vb Code:
Dim aLargePhoto As Byte() = GetUploadedPhoto(LargePhotoUpload)
Dim aThumbnailPhoto As Byte() = GetUploadedPhoto(ThumbnailPhotoUpload)
Dim Item As ProductPhoto = New ProductPhoto(aThumbnailPhoto, ThumbnailFilename.Text, aLargePhoto, LargePhotoFilename.Text)
yes, I have an HTTPHandler that is reading the image. my business object has a readonly method that uses MemoryStream (like in the post you linked to) to stream the Byte() and create a new Bitmap() out of it, and returns an Image. for example:
vb Code:
Private _thumbnailPhotoImage As Image = Nothing
Public ReadOnly Property ThumbnailPhotoImage() As Image
Get
If _thumbnailPhotoImage Is Nothing Then
If ThumbnailPhoto IsNot Nothing Then
Dim Stream As MemoryStream = New MemoryStream(ThumbnailPhoto)
_thumbnailPhotoImage = New Bitmap(Stream)
End If
End If
Return _thumbnailPhotoImage
End Get
End Property
and I do the same for the larger photo.
this is the handler I'm using to load the images:
vb Code:
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Drawing
Imports System.Web
Imports System.IO
Imports WebClient.UI.Handlers
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Try
Dim SizeParam As String = context.Request.Params("Size")
Dim IDParam As String = context.Request.Params("ID")
Dim ID As Integer
Dim Streamer As AbstractStreamableImage = Nothing
If Not String.IsNullOrEmpty(SizeParam) AndAlso Integer.TryParse(IDParam, ID) Then
Select Case SizeParam.ToLower
Case "large"
Streamer = New ProductPhotoImage(ID, True) 'second parameter is a boolean of whether or not the size is large
Case "thumbnail"
Streamer = New ProductPhotoImage(ID, False)
End Select
End If
If Streamer Is Nothing OrElse Streamer.StreamableImage Is Nothing Then
Streamer = New DefaultImage("")
End If
Streamer.Write(context.Response)
Catch ex As Exception
context.Response.ContentType = "text/plain"
context.Response.Write("no image")
End Try
context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
and the ProductPhotoImage class that it's using simply looks up the data in the database, populates a business object, and returns it depending on the size used (_large is a boolean variable for either thumbnail/large photo). this class is based off of an abstract class that I can also post if needed:
vb Code:
Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports AWSystem.Data
Imports AWSystem.BLL
Namespace WebClient.UI.Handlers
Public Class ProductPhotoImage
Inherits AbstractStreamableImage
Private _productPhotoID As Integer
Private _large As Boolean
Public Sub New(ByVal pProductPhotoID As Integer, Optional ByVal pLarge As Boolean = True)
_productPhotoID = pProductPhotoID
_large = pLarge
End Sub
Public Overrides ReadOnly Property StreamableImage() As Image
Get
If _StreamableImage Is Nothing Then
Dim Controller As New ProductPhotoController()
Dim Info As ProductPhoto = Controller.Lookup(_productPhotoID)
Dim ProductPhoto As Image = Nothing
If Info IsNot Nothing Then
If _large Then
_StreamableImage = Info.LargePhotoImage
Else
_StreamableImage = Info.ThumbnailPhotoImage
End If
End If
End If
Return _StreamableImage
End Get
End Property
End Class
End Namespace
however, the fact that all the other images display fine leads me to believe that my HTTPHandler and my readonly properties are working correctly.
I'll take a look at some more of the code in the thread you posted and try a few things (though most of it looks very similar already at just a glance), and thanks for the reply!