I had the ashx page all set up and began using the GetThumbnailImage procedure which I ave used many times before.
Unknown to me was the fact the images were needed to be displayed at a fairly large size, just scaled down - which is where the thumbnail procedure falls apart!
Instead I had to get a bit more serious but also received more control.
The new plan was to redraw the image as a bitmap. To start with I needed to establish the new size which was being passed in the querystring so that was done as follows:
Dim intWidth As Int32 = 100 'Default width if a non numeric number has been supplied.
Int32.TryParse(context.Request.QueryString("Width"), intWidth)
If intWidth > 2000 Then intWidth = 2000 'Make sure no stupid high width has been supplied.
Dim ms As New MemoryStream(byteImage)
Dim img As System.Drawing.Image = FromStream(ms, True)
Dim dblScale As Double = intWidth / img.Width
Dim intNewWidth As Int32 = Convert.ToInt32(img.Width * dblScale)
Dim intNewHeight As Int32 = Convert.ToInt32(img.Height * dblScale)
Int32.TryParse(context.Request.QueryString("Width"), intWidth)
If intWidth > 2000 Then intWidth = 2000 'Make sure no stupid high width has been supplied.
Dim ms As New MemoryStream(byteImage)
Dim img As System.Drawing.Image = FromStream(ms, True)
Dim dblScale As Double = intWidth / img.Width
Dim intNewWidth As Int32 = Convert.ToInt32(img.Width * dblScale)
Dim intNewHeight As Int32 = Convert.ToInt32(img.Height * dblScale)
This will calculate a new width and height based on the supplied width. 'byteImage' is the binary data for the original image and the difference in scale is worked out from that.
The next stage is to create a new bitmap and then begin drawing the image based on that:
Dim thumbnailBitmap = New Bitmap(intNewWidth, intNewHeight)
Dim thumbnailImage = Graphics.FromImage(thumbnailBitmap)
thumbnailImage.CompositingQuality = CompositingQuality.HighQuality
thumbnailImage.SmoothingMode = SmoothingMode.HighQuality
thumbnailImage.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim thumbnailImage = Graphics.FromImage(thumbnailBitmap)
thumbnailImage.CompositingQuality = CompositingQuality.HighQuality
thumbnailImage.SmoothingMode = SmoothingMode.HighQuality
thumbnailImage.InterpolationMode = InterpolationMode.HighQualityBicubic
As you can see there are extra quality parameters here that give more control.
Once the new thumbnail image has been created, it can be returned to the original image:
Dim imgRectangle As New Rectangle(0, 0, intNewWidth, intNewHeight)
thumbnailImage.DrawImage(img, imgRectangle)
ms = New MemoryStream()
thumbnailBitmap.Save(ms, img.RawFormat)
byteImage = ms.ToArray
thumbnailImage.DrawImage(img, imgRectangle)
ms = New MemoryStream()
thumbnailBitmap.Save(ms, img.RawFormat)
byteImage = ms.ToArray
Finally we have a very large thumbnail.
If rotation is required then I suspected that the bitmap should be rotated before it is redrawn:
thumbnailBitmap.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
Although I haven't tested this yet.
No comments:
Post a Comment