Advertisement
Zahrim

Picture Converter

May 13th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. private void btnConvert_Click(object sender, EventArgs e)
  2.         {
  3.             if (txtInput.Text != "")
  4.             {
  5.                 // Find files and put them into oldFiles
  6.                 DirSearch(txtInput.Text);
  7.  
  8.                 // Setup the progress bar
  9.                 tsProgressBar.Maximum = oldFiles.Count;
  10.                 tsProgressBar.Step = 1;
  11.                 tsProgressBar.Visible = true;
  12.                
  13.                 // Do the converting and update progress bar
  14.                 for (int i = 0; i < oldFiles.Count; i++)
  15.                 {
  16.                     SaveBmpAsPNG(oldFiles[i], newFiles[i]);
  17.                     tsProgressBar.PerformStep();
  18.                     tsStatusLabel.Text = "Converting " + oldFiles[i];
  19.                 }
  20.             }
  21.             // Clean up
  22.             oldFiles.Clear();
  23.             newFiles.Clear();
  24.             tsStatusLabel.Text = "";
  25.             tsProgressBar.Value = 0;
  26.             tsProgressBar.Visible = false;
  27.         }
  28.  
  29.         // Function to convert bitmaps to png files
  30.         private void SaveBmpAsPNG(string oldFile, string newFile)
  31.         {
  32.             Bitmap bmp1 = new Bitmap(oldFile);
  33.             bmp1.Save(@newFile, System.Drawing.Imaging.ImageFormat.Png);
  34.         }
  35.  
  36.         // Folder recursion
  37.         void DirSearch(string sDir)
  38.         {
  39.             // Create the folder structure in the output folder
  40.             foreach (string dirPath in Directory.GetDirectories(txtInput.Text, "*",
  41.                 SearchOption.AllDirectories))
  42.             {
  43.                 // Variable to hold the new folder structure
  44.                 string newFolder = dirPath.Replace(sDir, txtOutput.Text);
  45.                 // Check if the folder exists
  46.                 if (!Directory.Exists(newFolder))
  47.                 {
  48.                     // Create the new folder
  49.                     Directory.CreateDirectory(newFolder);
  50.                 }
  51.                 else
  52.                 {
  53.                     //MessageBox.Show("Folder already exists:\n" + newFolder, "Folder Exists!");
  54.                 }
  55.             }
  56.             foreach (string f in Directory.GetFiles(sDir, "*.bmp"))
  57.             {
  58.                 // Process files in directory
  59.                 string newName = f.Substring(0, f.Length - 3) + "png";
  60.                 newName = newName.Substring(txtInput.TextLength, newName.Length - txtInput.TextLength);
  61.                 newName = txtOutput.Text + newName;
  62.                 oldFiles.Add(f);
  63.                 newFiles.Add(newName);
  64.                 //MessageBox.Show(f + "\n" + newName);
  65.             }
  66.  
  67.             foreach (string d in Directory.GetDirectories(sDir))
  68.             {
  69.                 DirSearch(d);
  70.             }
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement