• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create an Image Converter in C#

KryptoPanda

Humor Commander
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
58
Likes
123
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Today in C#, I will teach you how to create a program that will convert an image to its file extension in jpeg, bmp, png, and gif.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.

2. Next, add one PictureBox named picbox, two buttons named btnBrowse and btnconvert, and one ComboBox named cbformats. Add two dialogs named saveDialog as saveFileDialog, and OFDialog as o You must design your interface like this:

imagedes.png


3. Click the Items property of your cbformats and encode PNG, BMP, GIF, and JPEG.

4. In your form_load() put this code below:
  1. public

    void

    Form1_Load(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. cbformats.

    SelectedItem

    =

    "PNG"

    ;
  4. }


.
5. In your btnbrowse put this code below so that it can browse image files.
  1. public

    void

    btnbrowse_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {

  3. try
  4. {
  5. // when the Browse Image button is click it will open the OpenfileDialog
  6. //this line of will check if the dialogresult selected is cancel then
  7. if

    (

    OFDialog.

    ShowDialog

    (

    )

    !=

    System

    .

    Windows

    .

    Forms

    .

    DialogResult

    .

    Cancel

    )
  8. {
  9. //the the selected image speciied by the user will be put into out picturebox
  10. picbox.

    Image

    =

    Image.

    FromFile

    (

    OFDialog.

    FileName

    )

    ;
  11. //then the image sizemode is set to strectImage
  12. picbox.

    SizeMode

    =

    PictureBoxSizeMode.

    StretchImage

    ;
  13. //then we assign val in to true because val variable above is declare as boolean
  14. val =

    true

    ;
  15. }
  16. }
  17. catch

    (

    Exception ex)
  18. {
  19. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  20. }
  21. }



6. In your btnconvert, put this code below to convert the images with its format.
  1. public

    void

    btnconvert_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. try
  4. {
  5. //it check if the variable val is set to val
  6. if

    (

    val ==

    true

    )
  7. {
  8. //check if what format is selected by user in the combobox
  9. //the after this the program will convert it to the desired format by the user.
  10. if

    (

    (

    string

    )

    cbformats.

    SelectedItem

    ==

    "PNG"

    )
  11. {
  12. saveDialog.

    Filter

    =

    "PNG|*.png"

    ;
  13. if

    (

    saveDialog.

    ShowDialog

    (

    )

    !=

    System

    .

    Windows

    .

    Forms

    .

    DialogResult

    .

    Cancel

    )
  14. {
  15. picbox.

    Image

    .

    Save

    (

    saveDialog.

    FileName

    , System.Drawing

    .

    Imaging

    .

    ImageFormat

    .

    Png

    )

    ;
  16. }

  17. }
  18. else

    if

    (

    (

    string

    )

    cbformats.

    SelectedItem

    ==

    "JPEG"

    )
  19. {
  20. saveDialog.

    Filter

    =

    "JPEG|*.jpg"

    ;
  21. if

    (

    saveDialog.

    ShowDialog

    (

    )

    !=

    System

    .

    Windows

    .

    Forms

    .

    DialogResult

    .

    Cancel

    )
  22. {
  23. picbox.

    Image

    .

    Save

    (

    saveDialog.

    FileName

    , System.Drawing

    .

    Imaging

    .

    ImageFormat

    .

    Jpeg

    )

    ;
  24. }
  25. }
  26. else

    if

    (

    (

    string

    )

    cbformats.

    SelectedItem

    ==

    "BMP"

    )
  27. {
  28. saveDialog.

    Filter

    =

    "BMP|*.bmp"

    ;
  29. if

    (

    saveDialog.

    ShowDialog

    (

    )

    !=

    System

    .

    Windows

    .

    Forms

    .

    DialogResult

    .

    Cancel

    )
  30. {
  31. picbox.

    Image

    .

    Save

    (

    saveDialog.

    FileName

    , System.Drawing

    .

    Imaging

    .

    ImageFormat

    .

    Bmp

    )

    ;
  32. }
  33. }
  34. else

    if

    (

    (

    string

    )

    cbformats.

    SelectedItem

    ==

    "GIF"

    )
  35. {
  36. saveDialog.

    Filter

    =

    "GIF|*.gif"

    ;
  37. if

    (

    saveDialog.

    ShowDialog

    (

    )

    !=

    System

    .

    Windows

    .

    Forms

    .

    DialogResult

    .

    Cancel

    )
  38. {
  39. picbox.

    Image

    .

    Save

    (

    saveDialog.

    FileName

    , System.Drawing

    .

    Imaging

    .

    ImageFormat

    .

    Gif

    )

    ;
  40. }
  41. }
  42. }
  43. }
  44. catch

    (

    Exception ex)
  45. {
  46. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  47. }
  48. }

Sample output:
imageout1.png


Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,696

329,704

Top