I've found some similar questions on SO and elsewhere, but nothing that quite does what I'm looking for (or, maybe I'm just too boneheaded to connect the dots!)
I'm looking to return a thumbnail image from a full size image stored as varbinary(max) in the database. I'll be using the thumbnails in a gallery-style view, so small size / efficient loading is crucial. I've been using an .ashx to return the full size image into a bound asp.net Image control, with the following code:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim conn As New SqlConnection()
conn.ConnectionString = *connectionstring*
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetEmplHeadShot"
Dim emplID As New SqlParameter("@emplID", context.Request.QueryString("emplid"))
cmd.Parameters.Add(emplID)
conn.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader
If myReader.Read Then
context.Response.BinaryWrite(myReader("HeadShot"))
context.Response.ContentType = "image/jpeg"
End If
myReader.Close()
conn.Close()
End Sub
I realize it's probably best to address the re-size at the upload, and stored the smaller image as a discrete column. But in the meantime, is it possible to program the handler to re-size the image on the fly? Ideally, I'd be able to pass an additional query string parameter, something like isResized from the image binding, allowing the same handler to be used for both full size and re-sized images.
Any advice / assistance would be greatly appreciated. Ideas on alternative approaches (i.e. "you're going about this all wrong") would also be very welcome.
Sorry, not a VB guy, but here's an example if you can read C#. You'll want to add some handling for missing parameters and such, but this could outline your basic approach.
public void ProcessRequest( HttpContext context )
{
int height = Convert.ToInt32(context.Request["height"]);
int width = Convert.ToInt32(context.Request["width"]);
//Get image from database here, put into a stream
var stream = new MemoryStream(); //this would represent the stream from your database image
using( var original = Image.FromStream( stream ) )
{
using( var resized = new Bitmap(width, height, PixelFormat.Format24bppRgb) )
{
using( var g = Graphics.FromImage( resized ) )
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage( original, new Rectangle( new Point( 0, 0 ), new Size(width, height) ) );
var resizedStream = new MemoryStream();
resized.Save(resizedStream, ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(resizedStream.GetBuffer());
context.Response.End();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With