Page 1 of 1

Make a Picture Converter in VB 2008

Posted: Mon Jun 07, 2010 1:20 pm
by shabs
Hi there,

I tried to make a pic converter and after spending 4 hours i got it...

Today i am going to show how to make a Picture Converter

You Need

2 Buttons ( Button 1 "Open File" Button 2 "Convert To")
1 ComboBox ( Add PNG JPEG GIF BMP ICO)
1 Label
1 Picture Box
1 OpenFileDialog
1 SaveFileDialog

Now add the Following Code to Open File Button
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)
            End If
        Catch ex As Exception

        End Try
    End Sub
Now Add the Following Code to Convert to Button
Code: Select all
If ComboBox1.SelectedItem = "JPEG" Then
            SaveFileDialog1.Filter = "JPEG |*.jpg"
            Try
                If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception

            End Try
        End If
        Try
            If ComboBox1.SelectedItem = "ICO" Then
                SaveFileDialog1.Filter = "ICO |*.ico"
                If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Icon)
                End If
            End If
        Catch ex As Exception
        End Try
        If ComboBox1.SelectedItem = "PNG" Then
            SaveFileDialog1.Filter = "PNG |*.png"
            Try
                If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png)
                End If
            Catch ex As Exception

            End Try
        End If
        If ComboBox1.SelectedItem = "GIF" Then
            SaveFileDialog1.Filter = "GIF |*.gif"
            Try
                If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif)
                End If
            Catch ex As Exception

            End Try
        End If
        If ComboBox1.SelectedItem = "BMP" Then
            SaveFileDialog1.Filter = "BMP |*.bmp"
            Try
                If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub
Hope You Got it..


Regards
Shahbaz Singh

Re: Make a Picture Converter in VB 2008

Posted: Mon Jun 07, 2010 4:07 pm
by mandai
You could probably clean up the save part a bit:
Code: Select all
SaveFileDialog1.Filter = "JPEG|*.jpg|ICO|*.ico|PNG|*.png|GIF|*.gif|BMP|*.bmp"

        Dim dr As DialogResult = SaveFileDialog1.ShowDialog()
        If dr = Windows.Forms.DialogResult.OK Then

            Dim fmt As Imaging.ImageFormat

            If SaveFileDialog1.FilterIndex = 0 Then
                fmt = System.Drawing.Imaging.ImageFormat.Jpeg
            ElseIf SaveFileDialog1.FilterIndex = 1 Then
                fmt = System.Drawing.Imaging.ImageFormat.Icon
            ElseIf SaveFileDialog1.FilterIndex = 2 Then
                fmt = System.Drawing.Imaging.ImageFormat.Png
            ElseIf SaveFileDialog1.FilterIndex = 3 Then
                fmt = System.Drawing.Imaging.ImageFormat.Gif
            Else 'only bmp left
                fmt = System.Drawing.Imaging.ImageFormat.Bmp
            End If

            Try
                PictureBox1.Image.Save(SaveFileDialog1.FileName, fmt)
            Catch ex As Exception

            End Try

        End If

Re: Make a Picture Converter in VB 2008

Posted: Tue Jun 08, 2010 8:17 pm
by XTechVB
simple but useful

Re: Make a Picture Converter in VB 2008

Posted: Mon Jul 05, 2010 10:47 am
by MrBrockWalker
Nice job, most of these converters i see on the net just change the extension without actually doing anything else