שתף באמצעות


Resize & Save images

Question

Sunday, December 17, 2017 4:55 PM

Hello,

I would like to help me in this proccess.

I want the programm to take a lot of directories of images from a listbox, resize them and then save them as a .jpeg(while the first photo is selected and shown in the picturebox.

Button2 is the resize & save button

textbox7 is a textbox with the folder of the images directory

listbox1 is the images directories 

picturebox1 is the selected image(directory) preview from the listbox in the picturebox1

Specificly I want the programm to open a directory with many images, insert directory images in listbox, select the first listbox index, show it in the picturebox, resize the image, save the directory that is selected and resized and then  go to the next listbox index and do the same progress insert in the picturebox, resize it  and save it so every single image directory shown in the listbox can be resized and saved

Thank you for your time

All replies (7)

Sunday, December 17, 2017 8:44 PM ✅Answered

I want to take the images, resize them and save them in an other folder as .jpeg

This example reads the .jpg images in a folder and resizes and saves in another folder.

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim InputFolderPath As String = "c:\bitmaps\planets\"
        Dim OutputFolderPath As String = "c:\test5\"
        Dim bmp As New Bitmap(100, 100)

        Dim scale As Double = 0.5       'save half size image

        Dim di As New IO.DirectoryInfo(InputFolderPath)
        Dim aryFi As IO.FileInfo() = di.GetFiles("*." & "jpg")

        For Each fi As IO.FileInfo In aryFi

            Using bmpOrg As New Bitmap(fi.FullName)
                'size the original by the scale ratio
                bmp = New Bitmap(CInt(bmpOrg.Width * scale), CInt(bmpOrg.Height * scale))

                Using g As Graphics = Graphics.FromImage(bmp)
                    'draw the original at the new size on memory bitmap
                    g.DrawImage(bmpOrg, 0, 0, bmp.Width, bmp.Height)

                    'save the temp resized bitmamp
                    bmp.Save(OutputFolderPath & fi.Name)
                End Using
            End Using
        Next

        If bmp IsNot Nothing Then bmp.Dispose()

        MsgBox("Save Completed")
    End Sub

Sunday, December 17, 2017 5:05 PM

hi

and your code so far is ................

Regards Les, Livingston, Scotland


Sunday, December 17, 2017 5:11 PM

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            Dim path As String = FolderBrowserDialog1.SelectedPath
            TextBox7.Text = path
        End If

        For Each file As String In System.IO.Directory.GetFiles(TextBox7.Text)
            ListBox1.Items.Add(file)
        Next
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex >= 0 Then
            If IO.Directory.Exists(TextBox7.Text) Then
                PictureBox1.ImageLocation = (ListBox1.SelectedItem)
                PictureBox1.Load()
            End If

            If PictureBox1.ImageLocation = Nothing Then
                ListBox1.Items(ListBox1.Items.Count - 1) = True
            End If
        End If
    End Sub
End Class

Completly nothing, I am trying 2 weeks now to do it and I have deleted over 5 (100lines) code, I also searched on google in the end but found nothing else than trash


Sunday, December 17, 2017 6:12 PM

Completly nothing, I am trying 2 weeks now to do it and I have deleted over 5 (100lines) code, I also searched on google in the end but found nothing else than trash

Its not clear what you want to do. Make a resized image of every image in a folder selected from the the list box? If so what size is the resized image to be? Do you want to save the resized image in the same folder?

Or do you want to make one image with a resized thumbnail of every image in a folder? And save that in the same folder?


Sunday, December 17, 2017 7:18 PM

I want to take the images, resize them and save them in an other folder as .jpeg


Sunday, December 17, 2017 8:30 PM | 1 vote

I want to take the images, resize them and save them in an other folder as .jpeg

You need to provide much more detail.

What files will you identify as image files?  Is the list here suitable?
https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

If not (for instance, if you want to use TGA or PDF) then  then you need to use a third-party library. But if that list is suitable you can read, resize and write the files using .Net methods.

By 'resize' I presume that you mean that you want to change the pixel dimensions of the images, not the file size.  How will the new dimension be calculated?  Is it fixed to some nominal value, or is it calculated from the existing size (eg, as a percentage).   Will the height/width ratio stay the same, or will it be forced to some fixed value?  If it is forced to some fixed value, is this done by stretching or padding?

Where will the files be stored to?   Does that destination already exist or will it need to be created?  Should it be user-selected or is the destination hard-coded?  What should happen if a file with the same name already exists in the destination folder (eg ABC.BMP and ABC.JPG both exist in the source folder)?

The Image class includes methods to load and save images.
https://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.drawing.image.save(v=vs.110).aspx

To resize an image you create a destination image of the required size, create a Graphics object from the destination image, and draw the source image to it using a draw command that allows you to specify the size of the destination image.   But the detail depends on how you want to do the resizing. For example, to copy the whole image to a new image at a new size, use
https://msdn.microsoft.com/en-us/library/dbsak4dc(v=vs.110).aspx


Sunday, December 17, 2017 9:13 PM | 1 vote

Thank you very much, based on this code I made what I wanted to