I
IkBenHet
Hello,
I want to make thumbnails from images. I found this code and modified
it and it works fine:
<%@Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
Return False
End Function
Sub Page_Load(sender as Object, e as EventArgs)
'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString("img")
'Read in the width and height
Dim imageHeight as Integer = Request.QueryString("h")
Dim imageWidth as Integer = Request.QueryString("w")
'Add on the appropriate directory
imageUrl = "/upload/" & imageUrl
Dim fullSizeImg as System.Drawing.Image
fullSizeImg =
System.Drawing.Image.FromFile(Server.MapPath(imageUrl))
'Do we need to create a thumbnail?
Response.ContentType = "image/gif"
If imageHeight > 0 and imageWidth > 0 then
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort
dummyCallBack = New _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf
ThumbnailCallback)
Dim thumbNailImg as System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth,
imageHeight, _
dummyCallBack,
IntPtr.Zero)
thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif)
End If
End Sub
</script>
The quality of that generated thumbnails is really bad. I found several
posts on it saying that it can be a problem when doing it with images
coming from Digitalcamera... but this is not the case.
I want to create thumbnails with a better quality.
Then I found that by using 'InterpolationMode' you can get a higher
quality image. But I cannot get it working.
The last try was with the below script:
<%@Import Namespace="System.Drawing.Imaging" %>
<%@Import Namespace="System.Drawing.Drawing2D" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
Return False
End Function
Sub Page_Load(sender as Object, e as EventArgs)
Dim imageUrl as String
ScaleStatic("/test.jpg", 50)
End Sub
Sub ScaleStatic(ByVal FromFile, ByVal intSize)
Dim imgPhoto As System.Drawing.image =
System.Drawing.Image.FromFile(Server.MapPath("/test.jpg"))
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0
If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If
Response.ContentType = "image/jpeg"
Dim bmPhoto As System.Drawing.Bitmap
bmPhoto = New System.Drawing.Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution)
Dim grPhoto As System.Drawing.Graphics
grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
grPhoto = imgPhoto.GetThumbnailImage(imgPhoto, New
System.Drawing.Rectangle(destX, destY, destWidth, destHeight), New
System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing.GraphicsUnit.Pixel)
bmPhoto.Save(Response.OutputStream, ImageFormat.Jpeg)
grPhoto.Dispose()
bmPhoto.Dispose()
End Sub
</script>
But is doesn't work.
Can somebody help me to get the first code sample modified so I returns
a better quality image.
Thanks!
I want to make thumbnails from images. I found this code and modified
it and it works fine:
<%@Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
Return False
End Function
Sub Page_Load(sender as Object, e as EventArgs)
'Read in the image filename to create a thumbnail of
Dim imageUrl as String = Request.QueryString("img")
'Read in the width and height
Dim imageHeight as Integer = Request.QueryString("h")
Dim imageWidth as Integer = Request.QueryString("w")
'Add on the appropriate directory
imageUrl = "/upload/" & imageUrl
Dim fullSizeImg as System.Drawing.Image
fullSizeImg =
System.Drawing.Image.FromFile(Server.MapPath(imageUrl))
'Do we need to create a thumbnail?
Response.ContentType = "image/gif"
If imageHeight > 0 and imageWidth > 0 then
Dim dummyCallBack as System.Drawing.Image.GetThumbNailImageAbort
dummyCallBack = New _
System.Drawing.Image.GetThumbnailImageAbort(AddressOf
ThumbnailCallback)
Dim thumbNailImg as System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth,
imageHeight, _
dummyCallBack,
IntPtr.Zero)
thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif)
End If
End Sub
</script>
The quality of that generated thumbnails is really bad. I found several
posts on it saying that it can be a problem when doing it with images
coming from Digitalcamera... but this is not the case.
I want to create thumbnails with a better quality.
Then I found that by using 'InterpolationMode' you can get a higher
quality image. But I cannot get it working.
The last try was with the below script:
<%@Import Namespace="System.Drawing.Imaging" %>
<%@Import Namespace="System.Drawing.Drawing2D" %>
<script language="VB" runat="server">
Function ThumbnailCallback() as Boolean
Return False
End Function
Sub Page_Load(sender as Object, e as EventArgs)
Dim imageUrl as String
ScaleStatic("/test.jpg", 50)
End Sub
Sub ScaleStatic(ByVal FromFile, ByVal intSize)
Dim imgPhoto As System.Drawing.image =
System.Drawing.Image.FromFile(Server.MapPath("/test.jpg"))
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0
If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If
Response.ContentType = "image/jpeg"
Dim bmPhoto As System.Drawing.Bitmap
bmPhoto = New System.Drawing.Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution)
Dim grPhoto As System.Drawing.Graphics
grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
grPhoto = imgPhoto.GetThumbnailImage(imgPhoto, New
System.Drawing.Rectangle(destX, destY, destWidth, destHeight), New
System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
System.Drawing.GraphicsUnit.Pixel)
bmPhoto.Save(Response.OutputStream, ImageFormat.Jpeg)
grPhoto.Dispose()
bmPhoto.Dispose()
End Sub
</script>
But is doesn't work.
Can somebody help me to get the first code sample modified so I returns
a better quality image.
Thanks!