Diviser une image : vb.net

Résolu
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012 - 27 juil. 2012 à 11:00
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012 - 1 août 2012 à 10:21
Bonjour bonjour,
Avec une fonction de rognage d'image que j'avais récupéré sur ce site il y a quelques temps, j'essai de rogner 16 images de taille identique, le tout dans un tableau.
Pour l'exemple je rogne uniquement les 4 premieres images du haut de l'image principale, seulement, la premiere image est bien rogné, mais toute les suivantes sont noir, quelqu'un serai t-il m'indiquer ou ce trouve mon erreur ?

Cordialement et merci d'avance.

Public Class Form1
    Dim fichier
    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = fichier
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim debutx = "0"
        Dim debuty = "0"
        Dim finx = "150"
        Dim finy = "150"
        Dim NbrImage = "0"
        Dim w1 = (finx - debutx)
        Dim h1 = (finy - debuty)
        Dim x As Integer
        For x = 1 To 4
            NbrImage += "1"
            PictureBox1.ImageLocation = fichier
            PictureBox1.Image = RognImage(PictureBox1.Image, debutx, debuty, w1, h1)
            PictureBox1.Image.Save(NbrImage & ".jpg")
            debutx += "150"
            finx += "150"
        Next
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        Dim nouvImage As New Bitmap(xPixelTotal, yPixelTotal)
        Dim graph As Graphics = Graphics.FromImage(nouvImage)
        Dim rect As New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class

16 réponses

Utilisateur anonyme
31 juil. 2012 à 23:27
Disons que moi j'aurais extrait un bitmap de picturebox1 avant la boucle, puis j'aurais fais mes 16 rognages à partir de là. voilà.
3
Utilisateur anonyme
31 juil. 2012 à 23:46
D'autant plus que la classe Bitmap a sa propre méthode Save. Ce qui te permet de te passer des conversions CType que tu utilises à tour de bras.
3
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
27 juil. 2012 à 11:07
Désolé pour l'auto réponse, j'ai créé une deuxieme picturebox pour charger les images rogné, resultat ça fonctionne.
Bonne prog à tous.

Dim debutx = 0
        Dim debuty = 0
        Dim finx = 150
        Dim finy = 150
        Dim NbrImage = 0
        Dim w1 = (finx - debutx)
        Dim h1 = (finy - debuty)
        Dim x As Integer
        For x = 1 To 4
            NbrImage += 1
            PictureBox2.Image = RognImage(PictureBox1.Image, debutx, debuty, w1, h1)
            PictureBox2.Image.Save(NbrImage & ".jpg")
            debutx += 150
            finx += 150
        Next
0
Utilisateur anonyme
30 juil. 2012 à 22:25
resultat ça fonctionne

Désolé de te décevoir mais non.

Active Option Strict dans les propriétés de ton projet et tu découvriras la catastrophe.
Je crois bien te l'avoir déjà signalé tantôt
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 20:57
Salut banana32,

Je ne comprend pas tout ce que je fais, donc quand je parviens à mes fins, je ne cherche effectivement pas plus loin.

J'ai activer l'option et modifié le code.

Te parait-il ok cette fois ?

Merci pour ton intervention.

Public Class Form1
    Dim fichier As String
    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim debutx = 0
        Dim debuty = 0
        Dim finx = 150
        Dim finy = 150
        Dim NbrImage = 0
        Dim w1 = (finx - debutx)
        Dim h1 = (finy - debuty)
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx = 600 Then
                If finy = 600 Then
                    Exit For
            Else
                PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                PictureBox2.Image.Save(NbrImage & ".jpg")
                debuty += 150
                finy += 150
                debutx = 0
                finx = 150
            End If
            Else
            PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
            PictureBox2.Image.Save(NbrImage & ".jpg")
            debutx += 150
            finx += 150
            End If

        Next
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        Dim nouvImage As New Bitmap(xPixelTotal, yPixelTotal)
        Dim graph As Graphics = Graphics.FromImage(nouvImage)
        Dim rect As New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class
0
Utilisateur anonyme
31 juil. 2012 à 22:21
Bonsoir,

1/ C'est bien, maintenant on retire l'importation automatique de Microsoft.VisualBasic pour éliminer les CStr et compagnie (options projet).

2/ Tu récupères, convertis, traites, et renvoie en permanence tes images sur tes contrôles picturebox. Là, ça va car il n'y en a que deux. Prends l'habitude de travailler sur des bitmap plutôt que d'extraire des images, de les convertir, de les modifier, de les reconvertir, et enfin de les remettre dans un contrôle. Tout cela est un gaspillage de ressources.
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 23:11
Bonsoir,

1/ J'ai désactivé l'import auto, et aucune erreur ne m'as était signalé :s

2/ Tu as peut-être mal compris mon code car tu as précèdemment dis : la ça va car c'est pour deux images.
En réalité il découpe 16 images. Quand il arrive au bord droit, il descend de 150px, ainsi de suite jusqu'a arrivé à destination.
J'ai rajouté dispose à la fin de chaque chargement d'images (enfin je crois) peut-être que c'était ce dont tu parlé ?

Merci
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 23:12
J'ai oublié le code

Public Class Form1
    Dim fichier As String
    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim debutx = 0
        Dim debuty = 0
        Dim finx = 150
        Dim finy = 150
        Dim NbrImage = 0
        Dim w1 = (finx - debutx)
        Dim h1 = (finy - debuty)
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx = 600 Then
                If finy = 600 Then
                    Exit For
            Else
                PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                PictureBox2.Image.Save(NbrImage & ".jpg")
                debuty += 150
                finy += 150
                debutx = 0
                finx = 150
            End If
            Else
            PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
            PictureBox2.Image.Save(NbrImage & ".jpg")
            debutx += 150
            finx += 150
            End If
            PictureBox2.Image.Dispose()
        Next
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        Dim nouvImage As New Bitmap(xPixelTotal, yPixelTotal)
        Dim graph As Graphics = Graphics.FromImage(nouvImage)
        Dim rect As New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 23:50
Re bonsoir,

Je l'ai est implanté via l'aide proposé lors d'erreur dans le code quand tu m'as fait désactiver les options.
Maintenant un problème s'offre à moi, il ne sauvegarde plus que 15 images.
Je creuse et te tiens informé si je trouve.

Merci

Public Class Form1
    Dim fichier As String
    Dim debutx As Integer = 0
    Dim debuty As Integer = 0
    Dim finx As Integer = 150
    Dim finy As Integer = 150
    Dim NbrImage As Integer = 0
    Dim w1 As Integer = (finx - debutx)
    Dim h1 As Integer = (finy - debuty)

    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx 600 And finy 600 Then
                    Exit For
            Else


                If finx < 600 Then
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    PictureBox2.Image.Save(NbrImage & ".jpg")
                    debutx += 150
                    finx += 150
                Else
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    PictureBox2.Image.Save(NbrImage & ".jpg")
                    debuty += 150
                    finy += 150
                    debutx = 0
                    finx = 150
                End If
            End If
            PictureBox2.Image.Dispose()
        Next
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        Dim nouvImage As New Bitmap(xPixelTotal, yPixelTotal)
        Dim graph As Graphics = Graphics.FromImage(nouvImage)
        Dim rect As New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 23:52
Pour l'histoire des 16 images c'est résolu.

  Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx > 600 And finy > 600 Then
                Exit For
            Else


                If finx < 600 Then
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    PictureBox2.Image.Save(NbrImage & ".jpg")
                    debutx += 150
                    finx += 150
                Else
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    PictureBox2.Image.Save(NbrImage & ".jpg")
                    debuty += 150
                    finy += 150
                    debutx = 0
                    finx = 150
                End If
            End If
            PictureBox2.Image.Dispose()
        Next
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
31 juil. 2012 à 23:59
J'ai réussi à sauvegarder directement le bitmap !

Merci

Public Class Form1
    Dim fichier As String
    Dim debutx As Integer = 0
    Dim debuty As Integer = 0
    Dim finx As Integer = 150
    Dim finy As Integer = 150
    Dim NbrImage As Integer = 0
    Dim w1 As Integer = (finx - debutx)
    Dim h1 As Integer = (finy - debuty)
    Dim nouvImage As Bitmap

    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx > 600 And finy > 600 Then
                Exit For
            Else
                If finx < 600 Then
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    nouvImage.Save(NbrImage & ".jpg")
                    debutx += 150
                    finx += 150
                Else
                    PictureBox2.Image = RognImage(CType(PictureBox1.Image, Bitmap), debutx, debuty, w1, h1)
                    nouvimage.Save(NbrImage & ".jpg")
                    debuty += 150
                    finy += 150
                    debutx = 0
                    finx = 150
                End If
            End If
            PictureBox2.Image.Dispose()
        Next
        PictureBox2.Dispose()
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        nouvImage = New Bitmap(xPixelTotal, yPixelTotal)
        Dim graph As Graphics = Graphics.FromImage(nouvImage)
        Dim rect As New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
1 août 2012 à 00:18
Encore un peu d'avancement, j'ai donc déclaré ceci tout en haut du code
 Dim firstImg As Bitmap


ensuite :

Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
        firstImg = New Bitmap(fichier)
    End Sub


et pour finir :


 Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx > 600 And finy > 600 Then
                Exit For
            Else
                If finx < 600 Then
                    PictureBox2.Image = RognImage(firstImg, debutx, debuty, w1, h1)
                    nouvImage.Save(NbrImage & ".jpg")
                    debutx += 150
                    finx += 150
                Else
                    PictureBox2.Image = RognImage(firstImg, debutx, debuty, w1, h1)
                    nouvimage.Save(NbrImage & ".jpg")
                    debuty += 150
                    finy += 150
                    debutx = 0
                    finx = 150
                End If
            End If
            PictureBox2.Image.Dispose()
        Next
        PictureBox2.Dispose()
    End Sub


C'est mieu ? :p

Merci pour ton aide.
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
1 août 2012 à 00:21
Le code final :

Public Class Form1
    Dim fichier As String
    Dim debutx As Integer = 0
    Dim debuty As Integer = 0
    Dim finx As Integer = 150
    Dim finy As Integer = 150
    Dim NbrImage As Integer = 0
    Dim w1 As Integer = (finx - debutx)
    Dim h1 As Integer = (finy - debuty)
    Dim nouvImage As Bitmap
    Dim firstImg As Bitmap
    Dim graph As Graphics
    Dim rect As Rectangle

    Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        fichier = OpenFileDialog1.FileName
        PictureBox1.ImageLocation = CStr(fichier)
        firstImg = New Bitmap(fichier)
    End Sub


    Private Sub EnregistrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregistrerSousToolStripMenuItem.Click
        Dim x As Integer
        For x = 1 To 16
            NbrImage += 1
            If finx > 600 And finy > 600 Then
                Exit For
            Else
                If finx < 600 Then
                    PictureBox2.Image = RognImage(firstImg, debutx, debuty, w1, h1)
                    nouvImage.Save(NbrImage & ".jpg")
                    debutx += 150
                    finx += 150
                Else
                    PictureBox2.Image = RognImage(firstImg, debutx, debuty, w1, h1)
                    nouvimage.Save(NbrImage & ".jpg")
                    debuty += 150
                    finy += 150
                    debutx = 0
                    finx = 150
                End If
            End If
            PictureBox2.Image.Dispose()
        Next
        PictureBox2.Dispose()
    End Sub

    Private Function RognImage(ByVal ImaSource As Bitmap, ByVal xPixelDep As Int32, ByVal yPixelDep As Int32, ByVal xPixelTotal As Int32, ByVal yPixelTotal As Int32) As Bitmap
        nouvImage = New Bitmap(xPixelTotal, yPixelTotal)
        graph = Graphics.FromImage(nouvImage)
        rect = New Rectangle(0, 0, xPixelTotal, yPixelTotal)
        graph.DrawImage(ImaSource, rect, xPixelDep, yPixelDep, xPixelTotal, yPixelTotal, GraphicsUnit.Pixel)
        Return nouvImage
    End Function
End Class
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
1 août 2012 à 00:27
Après avoir fait PictureBox2.Dispose j'ai juste ajouté ceci :
PictureBox2.Dispose()
        NbrImage = 0
        debutx = 0
        debuty = 0
        finx = 150
        finy = 150
0
Utilisateur anonyme
1 août 2012 à 07:21
Tu pourrais maintenant te passer de ton PictureBox2 (qui ne te sers à rien dans ce code).
Puisque ta fonction renvoie un Bitmap, tu peux écrire :
nouvimage = RognImage(firstImg, debutx, debuty, w1, h1)
nouvImage.Save(NbrImage.tostring & ".jpg")
0
KcHeY Messages postés 261 Date d'inscription dimanche 23 mai 2004 Statut Membre Dernière intervention 22 septembre 2012
1 août 2012 à 10:21
C'est fait merci banana

Bonne journée et bonne prog.
0
Rejoignez-nous