0

I was trying to save multiple image to a local folder in one go in vb. Right now, i am able to save one image at a time to a specified folder. In my code, i have set up a file name for the image to use when uploaded. What I needed is to save the image with it's original file name and file type. My program should also allow user to save multiple images in one go.

I am Using VB 2013 Ultimate

this is the code for searching.

 Try
            a.Filter = "Image files | *.*"
            If a.ShowDialog() = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(a.FileName)
                PictureBox1.BackgroundImageLayout = ImageLayout.Stretch

            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
and this is my code for saving

If (Not System.IO.Directory.Exists("d:/Site Images/" & Label33.Text & "/")) Then
                System.IO.Directory.CreateDirectory("d:/Site Images/" & Label33.Text & "/")
            End If
            'Dim address = ("d:/Site Images/" & a.FileName & ".jpg")
            PictureBox1.Image.Save("d:/Site Images/" & Label33.Text & "/" & "a.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

thanks for the help.

mrkdenz
  • 89
  • 2
  • 18
  • Not sure I understand what you are trying to accomplish with this code. I think you are using a OpenFileDialog to browse for an image file that already exists on your hard drive (with *.* filter). Then you load that image into a picturebox and from there you want to save the image back to your hard drive with a different filename? And you want to be able to do this with multiple images at the same time? Please give more information about what you are trying to do and then someone can better assist you with this. – Joe Uhren Jan 13 '15 at 05:37
  • yeah, an existing image from a hard drive or removable disk will be loaded to the picture box. Then from the picture box, it will be saved into a new folder preferably with the same or original file name and file type..I am currently using 5 picture boxes for a single session, Because i don't know how to enable multiple selection and upload. I wanted the image to be saved with its original file name so the system won't replace the saved image every time the user adds a new photo to the Profile. as you can see in the second code, I am using a.jpg as file name. – mrkdenz Jan 13 '15 at 08:29
  • i am also using a save command for each of the picture boxes. Like 5 of this `PictureBox1.Image.Save("d:/Site Images/" & Label33.Text & "/" & "a.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)` and file name set from a-e.. – mrkdenz Jan 13 '15 at 08:35
  • Ok well first of all you can select multiple images via OpenFileDialog by enabling multi-select: `a.Multiselect = true`. I would also set a better filter to check for valid image filetypes instead of *.*, and I would for sure use some kind of function to test if the file(s) selected are images like the answer from here: [http://stackoverflow.com/questions/3557874/filetype-check-in-vb-net]. The original filename can be preserved if needed by saving it to the picturebox.tag and reading it back when needed. – Joe Uhren Jan 13 '15 at 16:22
  • You could dynamically create pictureboxes to show multiple images but are you really going to do that if someone selects 100 images? What if they select 1,000? Are you going to display all of those on the screen? Of course you could use a scrollable panel but maybe you need to set a max # of images per upload at a time to combat that. – Joe Uhren Jan 13 '15 at 16:23
  • Another option is to have a button to select multiple images and then they will be loaded into a datagrid or listview control and from there you can more easily display a large number of images with a scrollbar. Unfortunately your question is very general and therefore it is difficult to give you a "proper" answer. – Joe Uhren Jan 13 '15 at 16:23
  • @joey thanks...i tried using `Dim name As String` and `name = PictureBox1.Image.Tag.ToString()` to get the file name, but it's not working for me. Am I doing it right? – mrkdenz Jan 14 '15 at 01:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68783/discussion-between-joey-joe-joe-jr-shabadoo-and-mrkdenz). – Joe Uhren Jan 14 '15 at 01:41
  • @joey by the way, i forgot to ask, is it possible to save the image as it is, without converting it to .jpeg ?.my application has to save different image file types to the folder. as well as images created by manifold also, i need to save a .pdf to the application..thanks – mrkdenz Jan 20 '15 at 00:07
  • Since you are saving the original filename to the picturebox tag you can simply check the extension from there and change the imageformat from `System.Drawing.Imaging.ImageFormat.Jpeg` to something else like `System.Drawing.Imaging.ImageFormat.Png` for example. Only a handful of popular image types are supported natively. Saving an image to pdf is something entirely different and I suggest you do a google search to find the answer to that one and ask another question here if you run into trouble. – Joe Uhren Jan 20 '15 at 06:00
  • i am trying to save a map image..I think it has a different image format so i need it to be saved as it is..thanks – mrkdenz Jan 21 '15 at 23:59

1 Answers1

0

A sample snippet

Dim a As New OpenFileDialog
a.Filter = "Jpeg Files|*.jpg"
a.ShowDialog()
For i As Integer = 0 To a.FileNames.Count - 1
    Try
        Dim img As Image = Image.FromFile(a.FileNames(i))
        img.Save("Your Saving Path")
    Catch ex As Exception

    End Try
Next
Tanmay Majumder
  • 392
  • 1
  • 4
  • 17