Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

Join 244,296 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 871 people online right now. Registration is fast and FREE... Join Now!




Converting to BMP to JPEG

 
Reply to this topicStart new topic

Converting to BMP to JPEG, Shamelessy taking others code that I do not understand...

R3volv360
2 Jan, 2009 - 11:45 PM
Post #1

New D.I.C Head
*

Joined: 5 Dec, 2008
Posts: 14



Thanked: 1 times
My Contributions
I want to write a program that will take all the files in a folder (assuming they are all bmp's) and convert them to jpeg's. In my mind it's a simple loop through all the files, process them with some algorithm, output them as jpeg's. Unfortunately I have no clue whatsoever as where to start. So I googled this and found some code and due to my lack of knowledge on the subject (and C# for that matter) I have no clue what is happening. Not wanting to be in ignorant bliss could someone please explain the following to me:

CODE

private void saveJpeg(string path, Bitmap img, long quality)
{
   EncoderParameter qualityParam =
      new EncoderParameter(Encoder.Quality, (long)quality);

   ImageCodecInfo jpegCodec =
      this.getEncoderInfo("image/jpeg");

   if(jpegCodec == null)
      return;

   EncoderParameters encoderParams = new EncoderParameters(1);
   encoderParams.Param[0] = qualityParam;

   img.Save(path, jpegCodec, encoderParams);
}

private ImageCodecInfo getEncoderInfo(string mimeType)
{
   ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

   for (int i = 0; i <codecs.Length; i++)
      if (codecs[i].MimeType == mimeType)
         return codecs[i];
   return null;
}


User is offlineProfile CardPM
+Quote Post


Core
RE: Converting To BMP To JPEG
2 Jan, 2009 - 11:57 PM
Post #2

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
What about this?

CODE

Image img = Image.FromFile(imagePath);
img.Save(pathToSave, System.Drawing.Imaging.ImageFormat.Jpeg);


imagePath is the full path to the BMP image (including the file name). pathToSave is the full path where the output JPEG file should be placed (including the file name and jpg file extension).

Then you just add this to a loop, so it will go through each file in the directory.

This post has been edited by Core: 2 Jan, 2009 - 11:58 PM
User is offlineProfile CardPM
+Quote Post

R3volv360
RE: Converting To BMP To JPEG
3 Jan, 2009 - 01:36 AM
Post #3

New D.I.C Head
*

Joined: 5 Dec, 2008
Posts: 14



Thanked: 1 times
My Contributions
That seems to work just as well. So far I've thrown this together. Still going to change the naming of the files and some other aesthetic stuff but it serves its purpose.

CODE
        private void button2_Click(object sender, EventArgs e)
        {
            string sDirectory;
            FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
            {                
                FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
                FolderBrowserDialog1.SelectedPath = "C:\\";
                FolderBrowserDialog1.Description = "Select folder with the desired files to process:";
                if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    sDirectory = FolderBrowserDialog1.SelectedPath;
                    ProcessDir(sDirectory);
                }
            }
        }
        public void ProcessDir(string sDir)
        {
            int x = 0;
            int k;
            string sExtension;
            string[] fileEntries = Directory.GetFiles(sDir);

                foreach (string fileName in fileEntries)
                {
                    x++;
                    k = fileName.Length;
                    sExtension = null;
                    for (int j = 3; j != 0; j--)
                    {
                        sExtension = sExtension + Convert.ToString(fileName[k - j]);
                    }
                    if (sExtension == "bmp")
                    {

                        Image img = Image.FromFile(fileName);
                        Directory.CreateDirectory(sDir + "\\Processed");
                        img.Save(sDir + "\\Processed\\img" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
        }

    }


Credit to Ohad Israeli and this guy, and Core, of course wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:36PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month