none
convert images tif to png in a loop. RRS feed

  • Question

  • I have a folder that contains a list of TIF files.
    I want to loop through the TIF files and save them to png in the same folder, but in my program the second file overwrites the first one.
    How do I fix the program?

                string File1 = "bar.png";
                string File2 = "ula.png";
    
                string path = "H:/temp/test/";
                var images = Directory.GetFiles(path, "*.TIF");
                
                foreach (var image in images)
                {
    
                    System.Drawing.Bitmap.FromFile(image).Save(path + File1, System.Drawing.Imaging.ImageFormat.Png);
                    System.Drawing.Bitmap.FromFile(image).Save(path + File2, System.Drawing.Imaging.ImageFormat.Png);
                }

    Monday, May 20, 2019 2:24 PM

Answers

  • Hi Alexs Roth,

    Thank you for posting here.

    Based on your description, you want to solve the problem that the second file overwrites the first one.

    You could try the following code, the bold code is modified.

                string File1 = "bar.png";
                string File2 = "ula.png";
    
                string path = "D:/temp/test/";
                var images = Directory.GetFiles(path, "*.TIF");
                int count = 1;
                foreach (var image in images)
                {
                    if(count==1)
                    {
                        System.Drawing.Bitmap.FromFile(image).Save(path + File1, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    if(count==2)
                    {
                        System.Drawing.Bitmap.FromFile(image).Save(path + File2, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    count++;
                }

    Result

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 1:29 AM
    Moderator

All replies

  • Hi Alexs Roth,

    Thank you for posting here.

    Based on your description, you want to solve the problem that the second file overwrites the first one.

    You could try the following code, the bold code is modified.

                string File1 = "bar.png";
                string File2 = "ula.png";
    
                string path = "D:/temp/test/";
                var images = Directory.GetFiles(path, "*.TIF");
                int count = 1;
                foreach (var image in images)
                {
                    if(count==1)
                    {
                        System.Drawing.Bitmap.FromFile(image).Save(path + File1, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    if(count==2)
                    {
                        System.Drawing.Bitmap.FromFile(image).Save(path + File2, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    count++;
                }

    Result

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 1:29 AM
    Moderator
  • Well, yes, if you have 5 files and you try to copy them all to "bar.png", it should not be surprising that you'll end up with one file.  What did you want?  Do you want them called "bar1.png" and "bar2.png" and "bar3.png"?  Or do you want them to have the same name as the original, but with a different extension?  It can all be done, but the key is you need to BUILD a file name each time.  You can't just use the same name.  For example:

        int count = 0;
        foreach( var image in images )
        {
            string File1 = "bar" + Convert.ToString(count) + ".png";
            System.Drawing.Bitmap.FromFile(image).Save(path+File1), System.Drawing.Imaging.ImageFormat.Png);
            string File2 = "ula" + Convert.ToString(count) + ".png";
            System.Drawing.Bitmap.FromFile(image).Save(path+File2), System.Drawing.Imaging.ImageFormat.Png);
            count++;
        }

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.


    Tuesday, May 21, 2019 7:16 AM