Last post Apr 13, 2005 10:56 PM by gmsetzer
Apr 13, 2005 10:56 PM|gmsetzer|LINK
I am trying to create an http handler that will pull an image from sql server and display in a browser. I want to resize the images on the fly and am having trouble passing the byte array from the memory stream to the image object. Can someone please advise
me on the proper way to do this? I am merging two examples together and both of them work. The first example is an ashx handler that pulls the image from the BLOB field in sql server and renders it in a web browser. The second example takes an image and
resizes it. The working example references an image on the server.
In the code below i am trying to use the Image.FromStream() to pass in the memory stream of the image. Is this the correct way to perform this task?
Currently I am not getting any error messages, however there is nothing rendering when i load the ashx file in a browser. I assume I am not passing the stream correctly. Some things to note, the GetBlob function returns the image as a byte() which is working
properly. Also the the thumbnail code works properly if I were to use the Image.FromFile() and pass in image path on server.
So, I assume for some reason the memory stream isnt passing into the Image.FromStream() correctly. Any thoughts would be greatly appreciated.
Public Sub ProcessRequest(ByVal ctx As HttpContext) Implements IHttpHandler.ProcessRequest Dim myArr as Array = Microsoft.VisualBasic.Split(ctx.Request.Querystring(0), ",") Dim PhotoID As String = ctx.Request.QueryString(0) Dim ImageWidth as String = 100 Dim ImageHeight as String = 50 ctx.ClearError() ctx.Response.Expires = 0 ctx.Response.Buffer = True ctx.Response.Clear() 'Dim fs As System.IO.FileStream Dim memStream As MemoryStream = New MemoryStream(DirectCast(GetBLOB(PhotoID), Byte())) 'memStream.WriteTo(fs) Dim fullSizeImg as System.Drawing.Image = System.Drawing.Image.FromStream(memstream) memStream.Close()
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim thumbNailImg as System.Drawing.Image = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero) thumbNailImg.Save(ctx.Response.OutputStream, System.Drawing.Imaging.ImageFormat.jpeg) ctx.Response.ContentType = "image/jpeg" ctx.Response.StatusCode = 200 ctx.Response.End() End Sub