Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with changing file extension from TIFF to PNG using VB.NET

Hi,

How do I change all file extensions from TIFF to PNG in my images subfolder using VB.NET?

Thanks,

Victor
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Dim img As Image
Img.FromFile("some.png") 'full path here
Img.Save("a.tiff", ImageFormat.Tiff)

Open in new window

try

My.Computer.FileSystem.RenameFile("C:\*.tiff", "*.png")
My code above actually changes the image of type "Tiff" to type "png". I think I may have misunderstood your question. Do you really just need to rename files?
if it does not support wildcard, something like this will do the job

Dim dir As String = "c:\test"
Dim path As String = "*.tiff"

Dim dirInfo As New DirectoryInfo(dir)
For Each fileInfo As FileInfo In dirInfo.GetFiles(path)
    My.Computer.FileSystem.RenameFile(fileInfo.FullName, replace(fileInfo.FullName.toLowercase(),".tiff",".png"))

Open in new window

To follow up on David's excellent comment, you almost surely do not want to simply change the file extension from TIFF to PNG — unless you know that they are really PNG files that were mistakenly named as TIFF files. Otherwise, you need to convert the TIFF files to PNG files, as David correctly noted. The point is, TIFF and PNG are different file formats — you must convert from one to the other, not just rename them. Regards, Joe
if you want to mass resize/crop/convert etc, this is the excellent application and free

http://www.faststone.org/FSResizerDetail.htm

FastStone Photo Resizer is an image converter and renaming tool that intends to enable users to convert, rename, resize, crop, rotate, change color depth, add text and watermarks to images in a quick and easy batch mode. Drag and Drop mouse operation is well supported.

  • Convert and Rename images in batch mode
  • Support JPEG, BMP, GIF, PNG, TIFF and JPEG2000
  • Resize, crop, change color depth, apply color effects, add text, watermark and border effects
  • Rename images with sequential number
  • Search and replace texts in the file names
  • Preview conversion and renaming
  • Support folder/non-folder structure
  • Load and save settings
  • And much more...
Avatar of Victor  Charles

ASKER

Hi,

Thanks for all the comments. I need to convert to PNG.

Victor
> if you want to mass resize/crop/convert etc, this is the excellent application and free

There are many excellent, free apps that can do mass converting, cropping, resizing, etc., such as GIMP, ImageMagick, IrfanView, and more. I published three articles here at EE about one of them — GraphicsMagick (a fork of ImageMagick):
Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method
Create a PDF file with Contact Sheets (montage of thumbnails) for all JPG files in a folder and each of its subfolders using an automated, batch method
Create an image (BMP, GIF, JPG, PNG, TIF, etc.) from a multi-page PDF

And also published a video on GIMP:
How to divide/split a single image file into multiple image files

But keep in mind that Victor wants to use VB.Net, so I recommend using one of the imaging programs that can easily be called from the command line, such as ImageMagick and IrfanView (it is extremely easy to run the ImageMagick and IrfanView stand-alone executables with command line parameters from any language that can make command line calls). Regards, Joe
> I need to convert to PNG.

Why do you want to use VB.Net to do that? There are many excellent programs out there that will convert all files in a folder from TIFF to PNG (via a GUI or CLI), several of which have already been mentioned in this thread.
I'm not allowed to download other programs on my work computer.
Can code below be modified to convert all JPG in Images folder to type PNG?
Dim img As Image
Img.FromFile("some.png") 'full path here
Img.Save("a.tiff", ImageFormat.Tiff)
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I removed tolowercase in code provided and copied .tif images to the tiff folder, than tried the code below, but no mages are saved in the png folder.

 Dim pathSource As String = "c:\tiff\"
        Dim pathTarget As String = "c:\png\"

        Dim dirInfo As New DirectoryInfo(pathSource)
        For Each fileInfo As FileInfo In dirInfo.GetFiles("*.tiff")
            Dim newImage As Image = Image.FromFile(fileInfo.FullName)
            Dim NewName = pathTarget & Replace(fileInfo.Name, ".tiff", ".png")
            newImage.Save(NewName, System.Drawing.Imaging.ImageFormat.Png)
        Next

Any ideas why it is  not working?

Victor
put a break point on line 8 (my code / dont remove tolowercase)
and see whats the value of

fileInfo.Name
NewName
It works, extension is .tif not tiff.
Thank you.