Convert JPG to PNG Format (Bulk or Single)






4.67/5 (2 votes)
This tip will help in understanding the directory and file dialog box, the progressbar control, and image conversion.

Introduction
This tip covers many things simultaneously. This tip will help in understanding the directory and file dialog box, and also the progressbar control and image conversion.
Using the Code
I have used very simple and easy to understand code. I am saving PNG images at the same location from where the application is getting JPG images. This can be changed as per your requirements.
//Get all JPG files from the location provided.
private void ReadJPGFiles(string path)
{
var files = Directory.EnumerateFiles(path).Where(p =>
Path.GetExtension(p).ToUpper() == ".JPG" ||
Path.GetExtension(p).ToUpper() == ".JPEG")
.Select(p => Path.GetFullPath(p));
progressBar1.Maximum = files.Count();
progressBar1.Value = 0;
foreach (string s in files)
{
ConvertToPng(s, Path.GetFileNameWithoutExtension(s));
progressBar1.Value += 1;
}
}
//Convert JPG image to PNG
private void ConvertToPng(string path,string fileName)
{
Image bmpImageToConvert = Image.FromFile(path);
bmpImageToConvert.Save(FolderPath + "\\" +
fileName.Trim() + ".png", ImageFormat.Png);
}
...
History
- 3rd December, 2013: Initial version.