Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2005, 07:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default GetThumbnailImage produces a larger file

I am generating thumbnails on-the-fly for a web application; actually profile pictures. The problem is that the original picture has a much smaller size byte-wise than the thumbnail, which is not what I expected.

Scenario... One picture is 434x614 pixels in size and 59kb and fairly good quality. After creating the thumbnail, the image is 200x307 pixels and the size is 144kb!

How can this be? And how do I reduce the size (bytes) according to the size (pixels)?

I would assume that I would be able to get a thumbnail with a size (bytes) 1/4 of the original image. Why do I get a smaller image with a bigger size (bytes)?

My method for generating the thumbnail is as follows...
Code:
private string GenerateThumb(string sourcepath, string folder, string prefix, int width, 
    bool overwrite, bool deletecurrupt)
{
    double factor;
    System.Drawing.Image thumbnail, fullimage;        

    string file = Path.GetFileName(sourcepath);
    string filethumb = folder + (folder.EndsWith(@"\")? "" : @"\") 
        + prefix + "." + file;
    if(overwrite || (!overwrite && !File.Exists(filethumb)))
    {
        try
        {
            fullimage = System.Drawing.Image.FromFile(sourcepath);
            factor = fullimage.Width/(double)width;
            thumbnail = fullimage.GetThumbnailImage(width, 
            (int)(fullimage.Height/factor), null, IntPtr.Zero);
            fullimage.Dispose();

            string directory = Path.GetDirectoryName(filethumb);
            if(!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            thumbnail.Save(filethumb);
            thumbnail.Dispose();
        }
        catch(OutOfMemoryException)
        {
            if(deletecurrupt)
                File.Delete(sourcepath);
        }
    }
    return filethumb;
}
Thanks, Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old November 13th, 2005, 09:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Its probably 'cause of the difference of the encoding of the two image files. If you specify something like jpeg of gif while saving the thumbnail file, you will get a file of smaller size.

Regards
Ankur Verma
 
Old November 13th, 2005, 11:07 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jacob,

GetThumbnailImage isn't really the best way to get good looking thumbnails. If I am not mistaken, it tries to retrieve an embedded thumbnail from the image when present, or otherwise it creates a custom thumb. Internally, it's not using optimized settings.

You'll get more control over the resize process if you use the Graphics class and then use DrawImage top copy one Bitmap to another. With the Graphics object, you can control the quality of the image (with InterpolationMode) and control the output type (using the Save method of the Bitmap class and passing it an ImageFormat).

HtH and if not, let me know.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 13th, 2005, 01:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys. I changed the Save line to...
Code:
thumbnail.Save(filethumb, System.Drawing.Imaging.ImageFormat.Jpeg);
... and the picture I was refering to in the first post became 9kb. Quite a difference.

About the Graphics class... i found this inefficiency with the GetHumbnailImage a few days ago when I was looking for an answer for this post. If you are interested...

http://www.devx.com/dotnet/Article/22079/1954?pf=true

Unfortunately, my knowledge about images/pictures etc. are limited, why I am pretty sure I wouldn't gain the full benefit. I don't see the drawbacks mentioned in the article in the page I am currently doing.

The web application I am doing is merely a hobby project, and I therefore have the freedom to refine as much as I want, so perhaps I should look into graphics, image processing etc. Do you know about the performance in relation to GetThumbnailImage method? Does it require that the original image is a bitmap?

Thanks, Jacob.
(BTW Imar... one more abbreviation figured out)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Macro in .dot file produces error... ismith Word VBA 2 March 13th, 2007 09:23 AM
How to display .tif file, .pdf file and .jpg file phuong171 ASP.NET 1.0 and 1.1 Basics 1 March 13th, 2007 07:29 AM
data stored larger than bigint limitation... elvio SQL Server 2000 1 February 9th, 2006 07:33 AM
ADO object failure for larger querys rowlandc Excel VBA 0 May 5th, 2004 11:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.