TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Thumbnail Image With GetThumbnailImage

GetThumbnailImage. A thumbnail is generated from an existing image. With the GetThumbnailImage method on the Image type, we scale an image to be smaller. We demonstrate a complete program that uses this method with some dynamic scaling logic.Image Type
Example. First, you will need to manually add the System.Drawing assembly in Visual Studio. The program introduces one custom method, GetThumbnailSize, which implements some logic to scale the thumbnail to be at most 40 pixels on any dimension.
C# program that generates thumbnail image using System; using System.Drawing; class Program { static void Main() { const string input = "C:\\background1.png"; const string output = "C:\\thumbnail.png"; // Load image. Image image = Image.FromFile(input); // Compute thumbnail size. Size thumbnailSize = GetThumbnailSize(image); // Get thumbnail. Image thumbnail = image.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero); // Save thumbnail. thumbnail.Save(output); } static Size GetThumbnailSize(Image original) { // Maximum size of any dimension. const int maxPixels = 40; // Width and height. int originalWidth = original.Width; int originalHeight = original.Height; // Compute best factor to scale entire image based on larger dimension. double factor; if (originalWidth > originalHeight) { factor = (double)maxPixels / originalWidth; } else { factor = (double)maxPixels / originalHeight; } // Return thumbnail size. return new Size((int)(originalWidth * factor), (int)(originalHeight * factor)); } } Output 1. Thumbnail image is generated.
Loading image. To begin, the program loads the C:\background1.png image from the disk. You can change the string literal so it points to another image if you want to. You may need to place an image on your disk.
GetThumbnailImage is an instance method on the Image type. We use the Size returned by our custom scaling code to compute the best dimensions. Finally, we call the Save method and write the thumbnail image to C:\thumbnail.png.
Scaling code. The most difficult part of generating thumbnails is probably scaling them. It isn't a good idea just to decrease all dimensions by 50% if the images are of different sizes, because then some thumbnails will be much larger than others.

Instead: The scaling logic here caps each dimension to 40 pixels. Based on the larger dimension, it scales the entire image.

Note: I tested this code with different sizes of images, both horizontally larger and vertically larger, and it worked correctly.

Compression. In my testing, the thumbnail images generated in the PNG format were not well-compressed. This means that if you plan to use the images in a website, you will want to run a batch compression on them afterwards.

Tip: JPEG images might not have this problem to the same extent due to their different compression algorithm.

PNG Optimization Program
Summary. Thumbnail images can be useful for web sites that must display many different pictures at once. You can save a tremendous amount of bandwidth by sending images that are 40 or even 100 pixels in a dimension rather than 1000 pixels.

Thus: The GetThumbnailImage method presents a realistic solution for many picture-oriented websites.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf