Back to all postsPrevious Post
C#.NETImage Processing
How to Convert Image Format Programmatically – .NET
SathishMarch 26, 2010
Converting images between different formats is a common requirement in many applications. Here's how to do it in C#.
Image Conversion Code
csharp
using System.Drawing;
using System.Drawing.Imaging;
public class ImageConverter
{
public static void ConvertImage(string inputPath, string outputPath, ImageFormat format)
{
using (Image image = Image.FromFile(inputPath))
{
image.Save(outputPath, format);
}
}
public static void ConvertToJpeg(string inputPath, string outputPath)
{
ConvertImage(inputPath, outputPath, ImageFormat.Jpeg);
}
public static void ConvertToPng(string inputPath, string outputPath)
{
ConvertImage(inputPath, outputPath, ImageFormat.Png);
}
public static void ConvertToBmp(string inputPath, string outputPath)
{
ConvertImage(inputPath, outputPath, ImageFormat.Bmp);
}
public static void ConvertToGif(string inputPath, string outputPath)
{
ConvertImage(inputPath, outputPath, ImageFormat.Gif);
}
}Supported Formats
- JPEG - Best for photographs
- PNG - Best for graphics with transparency
- BMP - Uncompressed bitmap
- GIF - Supports animation and transparency
- TIFF - High quality for printing
WCF and HTTP Error 404.3
Next PostHow to Generate RSA Encryption Keys – C#
Comments
No comments yet. Be the first to comment.