Dieses kleine Code Snippet zeigt wie man auf einfache Weise Bilder (JPG,PNG, BMP usw) in C# verkleinern kann. Dazu wird die Methode GetThumbnailImage der Klasse Image Benutzt.
using System; using System.Drawing; namespace koesteronline.eu.Drawing { public static class ImageHelper { public static void ResizeImage(string sourceFileName, string destFileName, int newWidth) { using (Image image = Image.FromFile(sourceFileName)) using(Image thumbImage = ResizeImage(image, newWidth)) { thumbImage.Save(destFileName); } } public static Image ResizeImage(Image image, int newWidth) { int width = newWidth; int height = newWidth; double factor = image.Width / (double)image.Height; if (factor > 1) height = (int)(height / factor); // Querformat else width = (int)(width * factor); // Hochformat return image.GetThumbnailImage(width, height, null, IntPtr.Zero); } } }
Kommentare