Click here to Skip to main content
15,868,118 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using iTextSharp.text.pdf.parser;
using Dotnet = System.Drawing.Image;
using iTextSharp.text.pdf;
using System.Drawing.Imaging;

namespace ImageConverter
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
        SqlCommand cmd;
        DataTable dt;
        SqlDataAdapter sda;
        public Form1()
        {
            InitializeComponent();
        }

        private void Select_Path_Click(object sender, EventArgs e)
        {
            try
            {
                string newfile = "";
                string imgnewfile = "";
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                if (Directory.Exists(Application.StartupPath + "/CanFiles") != true)
                {
                    Directory.CreateDirectory(Application.StartupPath + "/CanFiles");
                }

                foreach (string file in files)
                {
                    string fn = System.IO.Path.GetFileNameWithoutExtension(file);
                    if (System.IO.Path.GetExtension(file) == ".png" || System.IO.Path.GetExtension(file) == ".tiff" ||
                        System.IO.Path.GetExtension(file) == ".jpg" || System.IO.Path.GetExtension(file) == ".tif" ||
                        System.IO.Path.GetExtension(file) == ".gif" || System.IO.Path.GetExtension(file) == ".jfif" ||
                        System.IO.Path.GetExtension(file) == ".jpe" || System.IO.Path.GetExtension(file) == ".dib" ||
                        System.IO.Path.GetExtension(file) == ".bmp" || System.IO.Path.GetExtension(file) == ".JPG" ||
                        System.IO.Path.GetExtension(file) == ".BMP" || System.IO.Path.GetExtension(file) == ".JPE" || 
                        System.IO.Path.GetExtension(file) == ".GIF" || System.IO.Path.GetExtension(file) == ".PNG" ||
                        System.IO.Path.GetExtension(file) == ".jpeg" || System.IO.Path.GetExtension(file) == ".JPEG"
                            )
                    {

                        string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
                        string filepath = System.IO.Path.GetFullPath(file);
                        string fileext = System.IO.Path.GetExtension(file);
                        try
                        {
                            using (Image newImage = Image.FromFile(filepath))
                            {
                                Bitmap image1 = new Bitmap(Image.FromFile(filepath));
                                newfile = Application.StartupPath + "/CanFiles/" + fileName + ".jpg";
                                string tempfileName = "";
                                if (System.IO.File.Exists(newfile))
                                {
                                    int counter = 2;
                                    while (System.IO.File.Exists(newfile))
                                    {
                                        tempfileName = Application.StartupPath + "/CanFiles/" + counter.ToString() + fileName + ".jpg";
                                        newfile = tempfileName;
                                        counter++;
                                    }

                                }

                                image1.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
                                image1.Dispose();
                            }

                            GC.Collect();
                        }

                        catch (Exception)
                        {

                            listBox1.Items.Add("Corrupt FileName:" + fileName);
                            continue;
                        }


                    }

                    else
                    {
                        int pageNum = 1;
                        newfile = Application.StartupPath + "/CanFiles/";
                        PdfReader pdf = new PdfReader(file);
                        PdfDictionary pg = pdf.GetPageN(pageNum);
                        PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
                        PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                        if (xobj == null) { return; }
                        foreach (PdfName name in xobj.Keys)
                        {
                            PdfObject obj = xobj.Get(name);
                            if (!obj.IsIndirect()) { continue; }
                            PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                            PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                            if (!type.Equals(PdfName.IMAGE)) { continue; }
                            int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                            PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                            PdfStream pdfStrem = (PdfStream)pdfObj;
                            byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                            if (bytes == null) { continue; }
                            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                            {
                                memStream.Position = 0;
                                System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
                                if (!Directory.Exists(newfile))
                                    Directory.CreateDirectory(newfile);

                                string path = System.IO.Path.Combine(newfile, String.Format(@"{0}.jpg", fn));
                                System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                                parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
                                var jpegEncoder = ImageCodecInfo.GetImageEncoders().ToList().Find(x => x.FormatID == ImageFormat.Jpeg.Guid);
                                img.Save(path, jpegEncoder, parms);
                            }
                            // <-- For debugging use.
                        }

                        //listBox1.Items.Add("Image Not Converted:"+fn);
                    }
                }
                GC.Collect();
                MessageBox.Show("Image Successfully Converted!");

                string[] imgfiles = Directory.GetFiles(Application.StartupPath + "/CanFiles/");
                foreach (string imgfile in imgfiles)
                {
                    if (imgfile.Contains("_Sign"))
                    {
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(imgfile);
                        string filepath = System.IO.Path.GetFullPath(imgfile);
                        int lngimg = fileName.Length;
                        string imgname = fileName.Remove(lngimg - 5, 5);
                        //MessageBox.Show(""+imgname);
                        con.Open();

                        cmd = new SqlCommand("Get_MicaId_By_CandIdwise", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@CanId", imgname);
                        sda = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        sda.Fill(dt);
                        foreach (DataRow row in dt.Rows)
                        {
                            string rollname = row["RollNo"].ToString();
                            imgnewfile = Application.StartupPath + "\\CanFiles\\" + rollname + "_Sign" + ".jpg";
                            File.Move(imgfile, imgnewfile);


                        }
                        con.Close();
                    }

                    else
                    {
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(imgfile);
                        string filepath = System.IO.Path.GetFullPath(imgfile);
                        con.Open();

                        cmd = new SqlCommand("Get_MicaId_By_CandIdwise", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@CanId", fileName);
                        sda = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        sda.Fill(dt);
                        foreach (DataRow row in dt.Rows)
                        {
                            string rollname = row["RollNo"].ToString();
                            imgnewfile = Application.StartupPath + "\\CanFiles\\" + rollname + ".jpg";
                            File.Move(imgfile, imgnewfile);
                        }
                        con.Close();
                    }

                }
                MessageBox.Show("Image Successfully Rename");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                lbl1.Text = "Images: " + files.Length.ToString();
            }
        }
    }
}
Posted
Updated 4-Dec-15 19:29pm
v4

1 solution

You have some strange stuff going on in your code.
For example these lines:
C#
picturebox1.Image = new Bitmap(Image.FromFile(filepath));
Bitmap image1 = new Bitmap(picturebox1.Image);
image1.Save(Application.StartupPath  + "/CanFiles/" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

should be
C#
Bitmap image1 = new Bitmap(Image.FromFile(filepath));
Bitmap image1 = Image.FromFile(filepath);
image1.Save(Path.Combine(Application.StartupPath, "CanFiles", fileName, ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
image1.Dispose();  // You need to free the memory of the image

I see no reason to to use the PictureBox in a loop. The image will just flicker.

Also it would be easier to read and more efficient doing it this way:
C#
string extension = Path.GetExtension(file).ToUpper();
if (extension == ".PNG" || extension == ".TIFF" ||
    extension == ".JPG" || extension == ".TIF" ||
    extension == ".GIF" || extension == ".JFIF" ||
    extension == ".JPE" || extension == ".DIB" ||
    extension == ".BMP" || extension == ".JPEG")


[UPDATE]
Change this line
C#
Bitmap image1 = new Bitmap(Image.FromFile(filepath));

to
C#
Bitmap image1 = (Bitmap)Image.FromFile(filepath);

Otherwise you create two images and release only one.
(I missed this little "special" construct at first)
(Added typecast for Bitmap)

[UPDATE2]
Your code should look something like this now. Does it?
C#
private void Select_Path_Click(object sender, EventArgs e)
{
    string directoryPath = Path.Combine(Application.StartupPath, "CanFiles");
    if (!Directory.Exists(directoryPath))
        Directory.CreateDirectory(directoryPath);
    
    foreach (string file in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
    {
        string extension = Path.GetExtension(file).ToUpper();
        if (extension == ".PNG" || extension == ".TIFF" ||
            extension == ".JPG" || extension == ".TIF" ||
            extension == ".GIF" || extension == ".JFIF" ||
            extension == ".JPE" || extension == ".DIB" ||
            extension == ".BMP" || extension == ".JPEG")            
        {

            string filepath = Path.GetFullPath(file);
            Image image1 = Image.FromFile(filepath);

            string fileName = Path.ChangeExtension(file, ".jpg");

            string fileName = Path.ChangeExtension(Path.GetFileName(file), ".jpg");
            image1.Save(Path.Combine(directoryPath, fileName), System.Drawing.Imaging.ImageFormat.Jpeg);

            image1.Dispose();
        }
    }
    MessageBox.Show("Image Successfully Converted!");
}


[UPDATE3]
I have tested this code now and I have copied 2000 images about 5 to 8 MB each from one directory to another without a problem.
The memory consumption stays stable.

You might want to reconsider if you should "convert" also .jpg and .jpeg or if you should add code to just copy the files if they are of this type.
It will use less CPU and will be faster.
Something like:
C#
else if (extension == ".JPG" || extension == ".JPEG")
{
    File.Copy(oldPath, newPath);
}
 
Share this answer
 
v9
Comments
JigxP 30-Nov-15 12:48pm    
hello sir, i improve my code as you suggest but not get my solution.
same error occurs in this code.
George Jonsson 30-Nov-15 22:18pm    
Did you remove this line?
PictureBox picturebox1 = new PictureBox();

And on which line does the error occur?
JigxP 30-Nov-15 22:28pm    
Yes,sir
But the 851 IMG converted that time error occurred in in image1.save line. Error is out range memory. If want my project than I send u. If prefer. Ty
George Jonsson 30-Nov-15 22:41pm    
No, I would never want to get someones code to debug it. The reason is that all of a sudden, your problem becomes my problem and unless you pay for it I wouldn't do it.

See my updated answer.
You really like to create images, don't you?
JigxP 1-Dec-15 0:59am    
yes i like.

sir i implement your updated answer but first type casting error occurred. then improve to code by this line
Bitmap image1 = (Bitmap)Image.FromFile(filepath);
error solve of type casting.

but then after run my windows application that time error is->A generic error occurred in GDI+.
generated.

ty v much for help
solve my issues sir.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900