Question save 1 image as 3 different icons

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I have 1 picturebox for the source image to be converted to an icon...then I have 3 different pictureboxes with 3 different sizes a convert button , once clicked the source image in picturebox1 is resized to the 3 different size pictureboxes then I have a save dialog ...my question is how do i save all 3 with 1 save file dialog. I also want to add the size of the different icons to the file name before the extention.

example:
filename is test.ico
I want to add _32x32 between "test" and ".ico" i have this code


dim savedlg as new savefiledialog
dim sourceimage1 as image = Picturebox4.image
dim sourceimage2 as image = Picturebox3.image
dim sourceimage3 as image = Picturebox2.image

savedlg.filter = "Save Icon (*.ico) |*.ico"
savedlg.title = "Save Icon As"

if savedlg.showdialog() = dialogresult.ok then
dim EXT = system.io.path.getextention(savedlg.filename)
dim FILENAME = system.io.path.getfilenamewithoutextention(savedlg.filename)

sourceimage1.save(savedlg.filename, system.drawing.imaging.imageformat.icon)
sourceimage2.save(savedlg.filename, system.drawing.imaging.imageformat.icon)
sourceimage3.save(savedlg.filename, system.drawing.imaging.imageformat.icon)

end if

this code saves 1 icon , the last one......I ned to save all 3 at different sizes with the size added to the filename

I hade this line sourceimage1.save(FILENAME & "_32x32" & EXT, system.drawing.imaging.imageformat.icon)
but this didnt save anything but a messagebox would give the correct filename with "_32x32" added.

not sure what to do here.

any help will be appreciated

InkedGFX
 
FILENAME & "_32x32." & EXT

Might help!

Actually not, because the Path.GetExtension method gets the extension INCLUDING the dot.
MSDN said:
Return Value
Type: System.String
A String containing the extension of the specified path (including the "."), Nothing, or Empty. If path is Nothing,GetExtension returns Nothing. Ifpath does not have extension information, GetExtension returns Empty.
The issue here would be the fact that Path.GetFileNameWithoutExtension gets JUST the file name, not the folder path. When you later reconstruct the file name you don't include the folder path again.
Dim filePath = savedlg.FileName
Dim folderPath = Path.GetDirectoryName(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Dim extension = Path.GetExtension(filePath)

filePath = Path.Combine(folderPath, fileName & "_32x32" & extension)
 
Actually not, because the Path.GetExtension method gets the extension INCLUDING the dot.The issue here would be the fact that Path.GetFileNameWithoutExtension gets JUST the file name, not the folder path. When you later reconstruct the file name you don't include the folder path again.
Dim filePath = savedlg.FileName
Dim folderPath = Path.GetDirectoryName(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Dim extension = Path.GetExtension(filePath)

filePath = Path.Combine(folderPath, fileName & "_32x32" & extension)

this works howerver I need to save the source image 3 different times and the save dialog only saves it once. should I ue a for loop?

InkedGFX
 
   Dim savedlg As New SaveFileDialog
        Dim sourceImage1 As Image = PictureBox4.Image
        Dim sourceImage2 As Image = PictureBox3.Image
        Dim sourceImage3 As Image = PictureBox2.Image
        savedlg.Filter = "Save Icon (*.ico) |*.ico"
        savedlg.Title = "Save Icon As"

        
        If savedlg.ShowDialog() = DialogResult.OK Then
            Dim FilePath = savedlg.FileName
            Dim FolderPath = System.IO.Path.GetDirectoryName(FilePath)
            Dim EXT = System.IO.Path.GetExtension(FilePath)
            Dim FILENAME = System.IO.Path.GetFileNameWithoutExtension(FilePath)
           

            Dim filePath32 = System.IO.Path.Combine(FolderPath, FILENAME & "_32x32" & EXT)
            Dim filePath64 = System.IO.Path.Combine(FolderPath, FILENAME & "_64x64" & EXT)
            Dim filePath124 = System.IO.Path.Combine(FolderPath, FILENAME & "_124x124" & EXT)
            sourceImage1.Save(filePath32, System.Drawing.Imaging.ImageFormat.Icon)
            sourceImage2.Save(filePath64, System.Drawing.Imaging.ImageFormat.Icon)
            sourceImage3.Save(filePath124, System.Drawing.Imaging.ImageFormat.Icon)

        End If


this code works...what I needed to do was filepath for each different size.

thanks for the help

InkedGFX
 
Also, are you aware that you can save multiple sizes into a single icon file? I don't know how to do it in VB.NET to be honest but I'm sure that you could find out online.

Finally, you might want to think about importing some namespaces. If you import the System.IO namespace then you can just refer to the Path class instead of the System.IO.Path class and your code becomes cleaner and easier to read. If you import the System.Drawing.Imaging namespace then you can refer to the ImageFormat class instead of the System.Drawing.Imaging.ImageFormat class and the code looks even better. Even if you don't do that, the System and System.Drawing namespaces are already imported by default, so you can still refer to the IO.Path class and the Imaging.ImageFormat class as is. To learn about importing namespaces, follow the Blog link in my signature and check out my post on Assemblies & Namespaces.
 
I have all the namespaces imported, but I always still call the full name out..not sure why
What you do is obviously up to you but, personally, not only do I find writing fully qualified names all the time a pain, it does make your code genuinely harder to read, especially when there's no syntax highlighting.
 
Back
Top