• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Creating thumbnails from JPG images with .NET

July 22, 2011 By _tasos Leave a Comment

The .NET framework has the System.Drawing namespace which allows the developer to create and/or manipulate image files programmatically. A common and popular action that a developer might need is to create a thumbnail of an image.

The framework has a built-in method for creating thumbnails, the Image.GetThumbnailImage method. As you can read the method checks the image file if it has a thumbnail image embedded (as meta data) and returns this image. If not, it automatically creates a thumbnail by resizing the original image. This method does the trick but the quality of the thumbnail is low. But we cannot set the quality, so if we want high quality for our thumbnails we need to make our own method.

Our main concern for this method is to be able to have high quality images. There are a few things to consider. Also, the aspect ratio of the image will remain the same. Usually we want our images to be of the same width or height. So providing only e.g. the target width is enough, since we can calculate the target height.

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
 
Sub CreateThumb(ByVal SourceImagePath As String, ByVal DestinationImagePath As String, ByVal TargetWidth As Integer)
 
    Dim ThumbImage, TemporaryImage As Image
    TemporaryImage = System.Drawing.Image.FromFile(SourceImagePath)

Firstly, we need to create a bitmap with the desired dimensions. Then we load the source image into a System.Drawing.Rectangle object. Then with a System.Drawing.Graphics object we can set our resizing options and then draw our thumbnail. With this step we have resized our image but it is as a bitmap.

    Dim ScaleFactor As Double = TemporaryImage.Width / TargetWidth
    ThumbImage = New Bitmap(TargetWidth, CInt(TemporaryImage.Height / ScaleFactor))
 
    Dim DescinationRectangle As New Rectangle(0, 0, ThumbImage.Width, ThumbImage.Height)
    Dim GraphicsCrop As Graphics = Graphics.FromImage(ThumbImage)
    GraphicsCrop.SmoothingMode = SmoothingMode.HighQuality
    GraphicsCrop.CompositingQuality = CompositingQuality.HighQuality
    GraphicsCrop.InterpolationMode = InterpolationMode.High
 
    GraphicsCrop.DrawImage(TemporaryImage, DescinationRectangle, 0, 0, TemporaryImage.Width, TemporaryImage.Height, GraphicsUnit.Pixel

After resizing our bitmap image successfully we need to save it as a jpeg image. From the System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders() array we get all the available System.Drawing.Imaging.ImageCodecInfo objects. Looping that array we can find the “JPEG” ImageCodecInfo.

    Dim arrayICI() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
    Dim jpegICI As ImageCodecInfo = Nothing
    Dim x As Integer = 0
    For x = 0 To arrayICI.Length - 1
        If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
            jpegICI = arrayICI(x)
            Exit For
        End If
    Next

Then we set some EncoderParameters. These parameters will define the quality of the jpeg we want to create. I use the LZW compression , set quality level to 95, and color depth to 60L. You can change these parameters or use more in order to get the desired result.

    Dim myEncoderParameters As New EncoderParameters(3)
 
    myEncoderParameters.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionLZW)
    myEncoderParameters.Param(1) = New EncoderParameter(Encoder.Quality, 95)
    myEncoderParameters.Param(2) = New EncoderParameter(Encoder.ColorDepth, 60L)
    ThumbImage.Save(DestinationImagePath, jpegICI, myEncoderParameters)
    ThumbImage.Dispose()
    TemporaryImage.Dispose()
End Sub

Be sure to call the Dispose() function to the Image object, because the source file remains locked. This methods works for JPG image files. It also works with .png files but if the .png files has transparent colors a black background is put and that messes the image. Also, this method strips all meta data (date taken, camera model, GPS coordinates, etc) an image might have.

Filed Under: .NET Development Tagged With: Image manipulation

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in