Convertisseur d'images avec listview

Description

Sert a convertir différents type d'images (png, gif, bmp, jpg, wmf, tiff ect..) en d'autres formats.

Exemple d'isntallation aussi.

Source / Exemple :


Public Class frmConvertionImage
    Inherits System.Windows.Forms.Form

 Const EOF As Integer = -1

    Dim TYPES_IMAGES As String() = {".bmp", ".gif", ".emf", ".jpg", ".png", ".tiff", ".wmf"}
    Dim NomFormat As String() = {"Bitmap (BMP)", "Join Phjoto Expert Group (JPG)", "Portable Network Graphic (PNG)", "Tag Image File Format (TIFF)", "Windows Meta File (WMF)", "CompuServ Image (GIF)", "Enhanced Windows (EMF)"}
    Dim Compression As String() = {"Aucune", "Très Bonne", "Bonne", "Moyenne", "Faible", "Excellente", "Moyenne"}

    Dim strNomFichier As String
    Dim strFichierFullPath As String
    Dim Extension As String
    Dim ConfigFile As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim FileInfo As New IO.FileInfo(Application.ExecutablePath)

        'Path du fichier de configuration qui contient le dernier type d'image choisi 
        'ainsi que le dernier choix garder ou pas l'image originale.
        ConfigFile = String.Concat(FileInfo.DirectoryName, "\", "Convert.cfg")

        'Lecture des arguments, ex: Convert.exe c:\bmp\image.bmp 
        Dim Command As String() = Environment.GetCommandLineArgs

        Dim ioLecture As IO.StreamReader = IO.File.OpenText(ConfigFile)
        Dim Choix As Boolean
        Dim intSelection As Integer
        Dim I As Integer

        'Transforme le curseur en petit sablier
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        'Ajoute tous les noms de format dans la listview
        For I = 0 To NomFormat.Length - 1 Step 1
            lsvTypesImage.Items.Add(NomFormat(I))
            lsvTypesImage.Items(I).SubItems.Add(Compression(I))
            lsvTypesImage.Items(I).ImageIndex = 0
        Next I

        'Établi l'ordre alphabétique
        lsvTypesImage.Sorting = SortOrder.Ascending

        'Lit les lignes si la lecture n'est pas rendue a la fin de fichier (EOF) 
        'Choix Garder/Supprimer
        If ioLecture.Peek <> EOF Then
            Choix = ioLecture.ReadLine()
        Else
            Choix = False
        End If

        'Choix du format précédant
        If ioLecture.Peek <> EOF Then
            intSelection = CInt(ioLecture.ReadLine())
        Else
            intSelection = 0
        End If

        'Fermeture du fichier en lecture
        ioLecture.Close()

        'Sélectionne la ligne correspondant au format choisi la derniere fois
        lsvTypesImage.Items(intSelection).Selected = True

        'Coche l'option garder/supprimer choisie la derniere fois
        radSupprimer.Checked = Choix
        radGarder.Checked = Not Choix

        'Si un argument n'a pas été donné lors de l'appel du .exe
        If Command.Length = 1 Then

            'Restreint l'utilisateur a choisir que ces types de fichiers comme
            'fichier a convertir
            OpenFileDialog1.Filter = "Tous les types d'images suppotés|*.bmp;*.gif;*.emf;*.jpg;*.jpeg;*.png;*.tiff;*.wmf|Bitmap (BMP)|*.bmp|CompuServ Image (GIF)|*.gif|Enhanced Windows (EMF)|*.emf|Join Phjoto Expert Group (JPG)|*.jpg;*.jpeg|Portable Network Graphic (PNG)|*.png|Tag Image File Format (TIFF)|*.tiff|Windows Meta File (WMF)|*.wmf"

            'Ouvre la fenetre d'ouverture de fichier
            OpenFileDialog1.ShowDialog()

            'Saisie du nom de fichier
            strFichierFullPath = OpenFileDialog1.FileName()

            'Si aucun fichier n'a été sélectionnée, Fin du programme
            If strFichierFullPath = String.Empty Then
                Me.Close()
                Exit Sub
            End If

        Else

            'Si un argument a été donnée, cet argument est le fichier a convertir
            strFichierFullPath = Command(1)

        End If

        'Sert a obtenir des information sur le fichier
        Dim FileInfo2 As New IO.FileInfo(strFichierFullPath)

        Extension = FileInfo2.Extension
        strNomFichier = FileInfo2.Name

        lblNom.Text = String.Concat("Nom de l'image à convertir : ", strNomFichier)

        'Rétabli le curseur de Windows par défault
        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        'Fermeture du programme
        Me.Close()
    End Sub

    Private Sub Convertir()

        'Si un item a été sélectionné
        If lsvTypesImage.SelectedIndices.Count > 0 Then

            'Curseur Sablier
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

            'Sauvegarde des options (Types de fichier et Garder/Supprimer)
            Dim ioEcriture As IO.StreamWriter = IO.File.CreateText(ConfigFile)
            ioEcriture.WriteLine(radSupprimer.Checked)
            ioEcriture.WriteLine(lsvTypesImage.SelectedIndices(0))
            ioEcriture.WriteLine("caca")
            ioEcriture.Close()

            Dim Convertisseur As New System.Drawing.Bitmap(strFichierFullPath)
            Dim NouvelleExtension As String = TYPES_IMAGES(lsvTypesImage.SelectedIndices(0))
            Dim strNouveauFichier As String = String.Concat(strNomFichier.Substring(0, strNomFichier.LastIndexOf(".")), NouvelleExtension)

            'Vérification si le ficheir existe déja
            If NouvelleExtension = Extension Or IO.File.Exists(strNouveauFichier) Then
                MsgBox("Le fichier existe déjà. Choissisez un autre format d'image.", MsgBoxStyle.Critical, "Erreure lors de la création du fichier.")
                Me.Cursor = System.Windows.Forms.Cursors.Default

                'Si le fichier existe déja, la procédure ne se poursuit pas
                Exit Sub
            End If

            'Converti le fichier en un autre format selon l'extension cible
            Select Case NouvelleExtension
                Case ".jpg"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Jpeg)
                Case ".tiff"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Tiff)
                Case ".wmf"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Wmf)
                Case ".gif"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Gif)
                Case ".png"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Png)
                Case ".bmp"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Bmp)
                Case ".emf"
                    Convertisseur.Save(strNouveauFichier, System.Drawing.Imaging.ImageFormat.Emf)
            End Select

            'Libère les ressources utilisées par Convertisseur
            Convertisseur.Dispose()

            'Supprime l'image originale si l'option de la supprimer est activée
            If radSupprimer.Checked Then
                Kill(strFichierFullPath)
            End If

        End If
        btnAppliquer.Enabled = False
        'Rétabli le curseur
        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

    Private Sub lsvTypesImage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsvTypesImage.DoubleClick
        Convertir()
    End Sub

    Private Sub btnOk_CLick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Convertir()
        Me.Close()
    End Sub

    Private Sub lsvTypesImage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvTypesImage.SelectedIndexChanged
        btnAppliquer.Enabled = True
    End Sub

    Private Sub btnAppliquer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppliquer.Click
        Convertir()
    End Sub
End Class

Conclusion :


Si vous voulez, vous pouvez compiler le setup qui vient avec le projet et si vous l'installez, vous aurez un item ajouté au menu contextuel qui s'affiche lorsque vous cliquez sur le bouton de droit sur une image. Vous pourrez alors
convertir vos images comme si cette fonction serait intégrée dans Windows.

Note : Convert.cfg doit etre dans le répertoire de l'éxécutable (J'aurais pu arranger ca...)

C'est mon premier projet VB.NET qui sert a quelque chose, alors soyez un peu indulgents.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.