473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Thumbnails in good quality

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(image Url))

'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(Addres sOf
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.HorizontalResolutio n,
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.HighQualityBic ubic

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!

Nov 19 '05 #1
2 1775
Hey IkBenHet,

Wish I had the time to explain this, but I'm up against a SERIOUS deadline,
so I'll just give you the following code from an app I wrote, and let you
figure it out. Note: "ImageObject" is an in-memory Bitmap class:

/// <summary>

/// Write Image to browser

/// </summary>

public virtual void WriteToBrowser()

{

HttpResponse Response;

MemoryStream objStream = new MemoryStream();

ImageCodecInfo objImageCodecInfo;

EncoderParameters objEncoderParameters;

try

{

if (_ImageObject == null)

throw new Exception("ImageObject is not initialized. Use CreateImage() to
initialize ImageObject");

if (HttpContext.Current == null)

throw new Exception("No HttpContext");

Response = HttpContext.Current.Response;

switch (_ImageType)

{

default:

objImageCodecInfo = GetEncoderInfo("image/jpeg");

Response.ContentType = "image/jpeg";

break;

case ImageTypesEnum.GIF:

objImageCodecInfo = GetEncoderInfo("image/gif");

Response.ContentType = "image/gif";

break;

case ImageTypesEnum.BMP_TRANSP:

case ImageTypesEnum.BMP:

objImageCodecInfo = GetEncoderInfo("image/bmp");

Response.ContentType = "image/bmp";

break;

}

objEncoderParameters = new EncoderParameters(3);

objEncoderParameters.Param[0] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionLZW);

objEncoderParameters.Param[1] = new EncoderParameter(Encoder.Quality,
(long)_QualityPercentage);

objEncoderParameters.Param[2] = new EncoderParameter(Encoder.ColorDepth,
24L);

_ImageObject.Save(Response.OutputStream, objImageCodecInfo,
objEncoderParameters);

}

catch (Exception E)

{

HandleError(E);

}

}
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"IkBenHet" <ik********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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(image Url))

'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(Addres sOf
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.HorizontalResolutio n,
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.HighQualityBic ubic

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!

Nov 19 '05 #2
Hello,

Is it also possible with a VB based script?

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johan | last post by:
Hi, Where to find a php script to upload jpg files and make thumbnails of the jpg files ? Johan
5
by: Ken | last post by:
I am in the process of designing my first web-site, and am having a problem with my picture gallery. My thumbnails are all different sizes, I would like them to be one size. The manual does not...
3
by: Vagabond Software | last post by:
I'm trying to display thumbnail images in a Listview that look more like the Windows thumbnail view. Everything is working pretty good, but my thumbnails are decidedly not like the Windows...
8
by: Fabricio Sperandio | last post by:
Hi everyone, I am trying to generate some thumbnails using System.Drawing.Image Class. Actually the GetThumnailImage method. The question is: How can I get a better thumbnail picture? I mean,...
6
by: Chip | last post by:
I allow users to upload their own images, but I need to display them in a small space. I can use the src tags to limit the size, but this doesn't keep the aspect ratio. How can I display any image...
4
by: Ron Brennan | last post by:
Good evening, Windows 2000, JDK 1.5. What opinions do people have on what way and tool programmaticly produces the best quality thumbnails from larger images? On the web I've seen Java...
4
by: Almad | last post by:
Hello, I wonder how do I create reasonable thumbnails from JPEG with PIL. My code: logging.debug('Downloading image %s' % id) uri = ''.join(, '?p=', str(id)]) uf = urlopen(uri).read() f =...
1
by: Summercoolness | last post by:
In PIL, since thumbnail() first makes a draft copy of the image, and then resize it, so thumbnail() can run a lot faster than resize() because draft() seems a lot faster when resizing from very big...
5
by: JJ | last post by:
I have a gallery-like application. (The gallery will be actually presented in Flash, but the management (cms) of the images will be in asp.net. ) My question is, is it ok to create Thumbnail...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.