Click here to Skip to main content
15,886,857 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Convert JPG to PNG Format (Bulk or Single)

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
4 Dec 2013CPOL 15.9K   380   5   8
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.

C#
//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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Tapas is a .NET Tech Lead at Tavant Technologies, a software development company located at Bangalore, India. Here he is working on various .NET projects.

Comments and Discussions

 
GeneralMy vote of 5 Pin
fredatcodeproject5-Dec-13 1:53
professionalfredatcodeproject5-Dec-13 1:53 
BugIssues Pin
Ravi Bhavnani4-Dec-13 8:51
professionalRavi Bhavnani4-Dec-13 8:51 
GeneralRe: Issues Pin
Adam R Harris4-Dec-13 10:58
Adam R Harris4-Dec-13 10:58 
Ill answer for him

1 No
2 You can't
Don't comment your code - it was hard to write, it should be hard to read!

GeneralRe: Issues Pin
Ravi Bhavnani5-Dec-13 1:48
professionalRavi Bhavnani5-Dec-13 1:48 
GeneralRe: Issues Pin
Adam R Harris5-Dec-13 6:58
Adam R Harris5-Dec-13 6:58 
GeneralRe: Issues Pin
Ravi Bhavnani5-Dec-13 8:19
professionalRavi Bhavnani5-Dec-13 8:19 
GeneralRe: Issues Pin
Tapas Ranjan Singh4-Dec-13 18:49
Tapas Ranjan Singh4-Dec-13 18:49 
GeneralRe: Issues Pin
Ravi Bhavnani5-Dec-13 1:47
professionalRavi Bhavnani5-Dec-13 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.