Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    manik's Avatar
    manik is online now Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, USA
    Posts
    13,172
    Thanks
    744
    Thanked 699 Times in 550 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default Create High Quality Thumbnail - Resize Image Dynamically - ASP .Net C# Code

    This article will help you create high quality thumbnail from an image as well as resize image dynamically.
    GetThumbnailImage method of class Bitmap produces a thumbnail image from the file specified. Life is so easy with it. Not always though. Sometimes the thumbnail produced has low quality - pixelated and blurred.
    Why it Happens? Why My Images are Pixelated and Blurred?
    Image formats like jpeg may store the thumbnail inside the same file. If we use System.Drawing.Bitmap method GetThumbnailImage, method checks if there’s a thumbnail image stored into the file and, if the thumb is found, it returns that thumbnail version scaled to the width and height you requested. If the thumbnail version of the image is smaller then the size you requested to produce, thats when problem occurs. The thumbnails produced become pixelated as we know stretching an image to a larger once reduces the Image Quality.
    Solution:

    Solution is really simple. We will load the source image to a System.Drawing.Image object, scale it to the desired width and height preserving good quality and save it to a new file.
    Step by Step Procedure to Create a Good Quality Thumbnail:
    For this example lets assume these variables
    String src="c:/myImages/a.jpg"; //absolute location of source image

    String dest="c:/myImages/a_th.jpg"; //absolute location of the new image created(thumbnail)
    int thumbWidth=132; //width of the image (thumbnail) to produce
    The code snippest shown here is in ASP .NET C#. You have to use these namespaces
    using System.Drawing;
    using System.Drawing.Drawing2D;
    Following are the Steps along with example code:
    - Get the source image to a System.Drawing.Image object
    System.Drawing.Image image = System.Drawing.Image.FromFile(src);

    - Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.
    int srcWidth=image.Width;
    int srcHeight=image.Height;
    int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
    Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

    - Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

    - Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;

    - Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQu ality;

    - Set the System.Drawing.Graphics object property InterpolationMode to High
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

    - Draw the original image into the target Graphics object scaling to the desired width and height
    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
    gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

    - Save to destination file
    bmp.Save(dest);

    - dispose / release resources
    bmp.Dispose();
    image.Dispose();

    All codes together look like this:
    Code:
    System.Drawing.Image image = System.Drawing.Image.FromFile(src);  
    int srcWidth=image.Width;
    int srcHeight=image.Height; 
    int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
    Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);  
    
    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); 
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality  ; 
    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
    
    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
    gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);  
    
    bmp.Save(dest); 
      
    bmp.Dispose();
    image.Dispose();
    Source: Create High Quality Thumbnail – Resize Image Dynamically – ASP .Net C# Code
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  2. The Following User Says Thank You to manik For This Useful Post:

    sajjad ali khan (04-21-2011)

  3. #2
    tdave is offline Sophomore
    Join Date
    Mar 2009
    Posts
    79
    Thanks
    0
    Thanked 1 Time in 1 Post
    Feedback Score
    0

  4. #3
    xenmeng is offline Freshman
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    Need some time to try it out,thank for the tutorial..

  5. #4
    jasonnn is offline Freshman
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    To me, it seems a little more complicated. However, thanks for the post.

  6. #5
    MikeL is offline Junior
    Join Date
    Sep 2008
    Posts
    155
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Feedback Score
    0

    Default

    This is a very useful post for .Net/ C# programmers wanting to create thumbnails. Non-programmers could try using this free web page thumbnail generator.

  7. The Following User Says Thank You to MikeL For This Useful Post:

    manik (08-31-2009)

  8. #6
    BrainPulse's Avatar
    BrainPulse is offline Master
    Join Date
    Jun 2008
    Location
    www.brainpulse.com
    Posts
    5,593
    Thanks
    170
    Thanked 429 Times in 296 Posts
    Blog Entries
    5
    Feedback Score
    0

    Default

    wow great post book mark it thanks manik for providing very useful code!!!
    Web Hosting India- Host Away with BrainPulse Hosting Solutions. SEO Company India- offers, Guaranteed SEO Services as a stand-alone Email Marketing Solutions- Complete Mass mailing

  9. #7
    striglivok is offline Freshman
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    That's pretty good.
    Many thanks to ur post. I love it.
    Would a new one like me be welcome here?
    Thanks so much in deed.

  10. #8
    manik's Avatar
    manik is online now Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, USA
    Posts
    13,172
    Thanks
    744
    Thanked 699 Times in 550 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default

    Quote Originally Posted by striglivok View Post
    That's pretty good.
    Many thanks to ur post. I love it.
    Would a new one like me be welcome here?
    Thanks so much in deed.
    Glad you find it useful. You are always welcome here to be part of us.
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  11. The Following User Says Thank You to manik For This Useful Post:

    sajjad ali khan (04-21-2011)

  12. #9
    Chocolate Lime's Avatar
    Chocolate Lime is offline Freshman
    Join Date
    Dec 2009
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    Feedback Score
    0

    Default

    Nice snippet Manik.

    What version of the framework is this written against?

  13. #10
    manik's Avatar
    manik is online now Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, USA
    Posts
    13,172
    Thanks
    744
    Thanked 699 Times in 550 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default

    Quote Originally Posted by Chocolate Lime View Post
    Nice snippet Manik.

    What version of the framework is this written against?
    thats with .Net 2.
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  14. #11
    marc is offline Freshman
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    Nice but a little bit complicated

  15. #12
    fostin is offline Freshman
    Join Date
    Jul 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

  16. #13
    silvana238 is offline Freshman
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default Create High Quality Thumbnail - Resize Image Dynamically - ASP .Net C# Code

    This article will help us create high quality thumbnail from an image as well as resize image dynamically.Thank for the post...

  17. #14
    samson77 is offline Freshman
    Join Date
    Dec 2010
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    Creatre high quality thumbnail from an image as well as resize image dynamically Get ThumbnailImage method of class Bitmap produces a thumbnail image from the file specified.Thanks for sharing info.

  18. #15
    jeffrey78's Avatar
    jeffrey78 is offline Freshman
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Feedback Score
    0

    Default

    Image quality is very bad

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •