Make a Picture Converter in VB 2008

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts Page 1 of 1
Contributors
User avatar
shabs
Just Registered
Just Registered
Posts: 3
Joined: Mon Jun 07, 2010 6:35 am

Make a Picture Converter in VB 2008
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
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

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
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

simple but useful
You can find me on Facebook or on Skype mihai_92b
User avatar
MrBrockWalker
VIP - Donator
VIP - Donator
Posts: 171
Joined: Mon Jul 05, 2010 10:30 am

Nice job, most of these converters i see on the net just change the extension without actually doing anything else
Visit BW Photography and check out my photos!
4 posts Page 1 of 1
Return to “Tutorials”