Coordonnées Pointeur pour DrawImage - Drag and drop

Naudrey43 Messages postés 17 Date d'inscription lundi 6 décembre 2010 Statut Membre Dernière intervention 29 février 2012 - 27 févr. 2012 à 16:09
Naudrey43 Messages postés 17 Date d'inscription lundi 6 décembre 2010 Statut Membre Dernière intervention 29 février 2012 - 29 févr. 2012 à 21:02
Bonjour, j'ai posté la semaine derniére un probléme pour faire mon application de drag and drop. Bon j'ai pas mal avancée Mais aujourd'hui je suis confrontée a un nouveau probléme.
Pour dessiner mon image déplacer sur mon autre image (drag and drop)
j'utilise :

Private Sub pictureBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
 
        Dim picbox As PictureBox = CType(sender, PictureBox)
        Dim g As Graphics
        g = Graphics.FromImage(picbox.Image)
        g.DrawImage(CType(e.Data.GetData(DataFormats.Bitmap), Image), New Rectangle(e.X, e.Y, 100, 100))
        picbox.Invalidate()
 
    End Sub


Mon probléme ce situe a ce niveau la :
New Rectangle(e.X, e.Y, 100, 100))


Je veux pouvoir coller mon image déplacer à l'endroit ou se trouve mon curseur de souris... le probléme c'est que ça ne marche pas comme je veux, l'image et bien déplacer mais elle se colle un peu nimporte ou... et pas la ou j'ai mis le curseur ... j'ai lu pas mal de chose sur des forums et fais des tests ... mais je n'arrives toujours pas à mettre mon image déplacée à l'endroit ou le curseur est placé et quand je relache le bouton de la souris...

Savez vous comment faire pour que mon image soit placer a l'endroit ou mon curseur et placer sur ma deuxiéme picturebox?

Merci d'avance ...
A voir également:

24 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
29 févr. 2012 à 13:57
correction

Imports System
Imports System.Drawing.Imaging

Public Class Form1
#Region "declaration"
    Private WithEvents mytoolstripbar As New ToolStrip
    Private WithEvents planimage As New ToolStripButton
    Private WithEvents barimage As New ToolStripButton
    Private WithEvents saveimage As New ToolStripButton
    Private WithEvents Eraseall As New ToolStripButton
    Private xp, yp As Integer
    Private flagplan As Boolean
    Private counter As Integer

    Private Structure barcodeimage
        Public imagebarcode As Bitmap
        Public idimage As Integer
    End Structure
    Private Structure barcodedata
        Public locationbarcode As Point
        Public barcodeid As Integer
    End Structure
    Private listbarcodeimage As New List(Of barcodeimage)
    Private lastmouselocation As New Point
    Private listlocationbarcode As New List(Of barcodedata)
#End Region
#Region "design"
    Private Sub DesignToolstripBar()
        mytoolstripbar.BackColor = Color.Cornsilk
        Me.Controls.Add(mytoolstripbar)
        With planimage
            .Font = New Font("arial", 12, FontStyle.Bold)
            .BackColor = Color.PowderBlue
            .Text = "Plan image"
            .ToolTipText = "loading plan image"
        End With
        mytoolstripbar.Items.Add(planimage)
        mytoolstripbar.Items.Add(New ToolStripSeparator)
        With barimage
            .Font = New Font("arial", 12, FontStyle.Bold)
            .BackColor = Color.PowderBlue
            .Text = "Les barcodes"
            .ToolTipText = "loading barcode image"
            .Enabled = False
        End With
        mytoolstripbar.Items.Add(barimage)
        mytoolstripbar.Items.Add(New ToolStripSeparator)
        With saveimage
            .Font = New Font("arial", 12, FontStyle.Bold)
            .BackColor = Color.PowderBlue
            .Text = "Save image"
            .ToolTipText = "save image"
            .Enabled = False
        End With
        mytoolstripbar.Items.Add(saveimage)
        mytoolstripbar.Items.Add(New ToolStripSeparator)
        With Eraseall
            .Font = New Font("arial", 12, FontStyle.Bold)
            .BackColor = Color.PowderBlue
            .Text = "Nouveau"
            .ToolTipText = "Nouveau"
            .Enabled = False
        End With
        mytoolstripbar.Items.Add(Eraseall)
    End Sub
#End Region
#Region "events"
    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim thebarcodedata As New barcodedata
        If e.Cancel = True Then Exit Sub

        If flagplan Then
            Panelframe.Visible = True
            Panelframe.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
            Panelframe.Width = Panelframe.BackgroundImage.Width
            Panelframe.Height = Panelframe.BackgroundImage.Height
            Panelframe.Location = New Point(0, mytoolstripbar.Bottom + 2)
            Panelframe.BackgroundImageLayout = ImageLayout.None
            planimage.Enabled = False
            barimage.Enabled = True
            Eraseall.Enabled = True
            Exit Sub
        End If
        Dim mypic As New PictureBox
        Dim thebarcodeimage As New barcodeimage
        With mypic
            .BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
            .Location = New Point(xp, yp)
            .BorderStyle = BorderStyle.Fixed3D
            .Width = .BackgroundImage.Width
            .Height = .BackgroundImage.Height
            .Tag = counter
            .BringToFront()
            AddHandler mypic.MouseMove, AddressOf mypicmousemove
            AddHandler mypic.MouseDown, AddressOf mypicMouseDown
            AddHandler mypic.MouseUp, AddressOf mypicMouseup
        End With
        thebarcodeimage.idimage = counter
        thebarcodeimage.imagebarcode = DirectCast(mypic.BackgroundImage, Bitmap)
        thebarcodedata.barcodeid = counter
        thebarcodedata.locationbarcode = mypic.Location
        listbarcodeimage.Add(thebarcodeimage)
        listlocationbarcode.Add(thebarcodedata)
        counter += 1
        Panelframe.Controls.Add(mypic)
    End Sub

    


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        xp = 1
        yp = 1
        counter = 0
        DesignToolstripBar()
       
        

    End Sub

    Private Sub mypicmousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim pt As New Point
        pt = e.Location
        If e.Button = Windows.Forms.MouseButtons.Left Then

            pt.X = (pt.X + DirectCast(sender, PictureBox).Left) - lastmouselocation.X
            pt.Y = (pt.Y + DirectCast(sender, PictureBox).Top) - lastmouselocation.Y
            If pt.X < 0 Then

                pt.X = 0
            End If
            If pt.Y < 0 Then
                pt.Y = 0
            End If
            If pt.X > Me.Width - DirectCast(sender, PictureBox).Width Then
                pt.X = Me.Width - DirectCast(sender, PictureBox).Width
            End If
            If pt.Y > Me.Height - DirectCast(sender, PictureBox).Height Then
                pt.Y = Me.Height - DirectCast(sender, PictureBox).Height
            End If
            DirectCast(sender, PictureBox).Location = pt
            DirectCast(sender, PictureBox).BringToFront()
        End If
    End Sub
    Private Sub mypicMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        lastmouselocation = e.Location
    End Sub
    Private Sub mypicMouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim relativepoint As New Point
        Dim thebarcodedata As New barcodedata
        Dim idbarcode, iter, iterimage As Integer
        relativepoint.X = DirectCast(sender, PictureBox).Left
        relativepoint.Y = DirectCast(sender, PictureBox).Top
        For iterimage = 0 To listbarcodeimage.Count - 1
            If DirectCast(sender, PictureBox).Tag.ToString = listbarcodeimage(iterimage).idimage.ToString Then
                idbarcode = listbarcodeimage(iterimage).idimage
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    For iter = 0 To listlocationbarcode.Count - 1
                        If listlocationbarcode(iter).barcodeid = idbarcode Then
                            thebarcodedata.barcodeid = idbarcode
                            thebarcodedata.locationbarcode = relativepoint
                            listlocationbarcode.Remove(listlocationbarcode(iter))
                            listlocationbarcode.Insert(iter, thebarcodedata)
                            Exit Sub
                        End If
                    Next
                Else
                    listbarcodeimage.RemoveAt(iterimage)
                    For iter = 0 To listlocationbarcode.Count - 1
                        If listlocationbarcode(iter).barcodeid = idbarcode Then
                            listlocationbarcode.RemoveAt(iter)
                            Panelframe.Controls.Remove(DirectCast(sender, PictureBox))
                            Exit Sub
                        End If
                    Next
                End If
            End If


        Next
    End Sub
    

    Private Sub planimage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles planimage.MouseDown
        flagplan = True
        OpenFileDialog1.ShowDialog()
        
        'DirectCast(sender, ToolStripButton).Enabled = False
    End Sub
    Private Sub barimage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles barimage.MouseDown
        flagplan = False
        OpenFileDialog1.ShowDialog()
        saveimage.Enabled = True
        Eraseall.Enabled = True
    End Sub
    Private Sub saveimage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles saveimage.MouseDown
        SaveFileDialog1.ShowDialog()
        
    End Sub
    Private Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        JoinImage()
        MessageBox.Show("New image was created")
    End Sub
    Private Sub Eraseall_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Eraseall.MouseDown
        listbarcodeimage.Clear()
        listlocationbarcode.Clear()
        Panelframe.Controls.Clear()
        Panelframe.Visible = False
        barimage.Enabled = False
        planimage.Enabled = True
        saveimage.Enabled = False
    End Sub
#End Region
    

    Private Sub JoinImage()
        Try
            Dim imageplan As Bitmap = DirectCast(Panelframe.BackgroundImage, Bitmap)
            Dim size1, size2, sizefinal As New Size
            size1 = imageplan.Size
            '   size2 = imageplan2.Size
            sizefinal.Height = size1.Height
            sizefinal.Width = size1.Width
            Dim imgFinal As New Bitmap(sizefinal.Width, sizefinal.Height)
            Dim gfx As Graphics = Graphics.FromImage(imgFinal)
            gfx.Clear(Color.White)
            gfx.DrawImage(imageplan, 0, 0, New Rectangle(0, 0, size1.Width, size1.Height), GraphicsUnit.Pixel)
            '  gfx.DrawImage(imageplan2, New Point(20, size1.Height \ 2))
            For iter = 0 To listbarcodeimage.Count - 1
                gfx.DrawImage(listbarcodeimage(iter).imagebarcode, New Point(listlocationbarcode(iter).locationbarcode.X, listlocationbarcode(iter).locationbarcode.Y))
            Next
            imgFinal.Save(SaveFileDialog1.FileName, ImageFormat.Jpeg)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Panelframe.Visible = False
        ' Add any initialization after the InitializeComponent() call.

    End Sub

   
End Class
0
Naudrey43 Messages postés 17 Date d'inscription lundi 6 décembre 2010 Statut Membre Dernière intervention 29 février 2012
29 févr. 2012 à 20:28
Bonsoir ShayW, j'ai un peu mieux examiné ton code.

Il m'a l'air vraiment intéressant, je risque de te piquer quelques idées...

Mon code n'est pas construit de la même manière, mais il reprend les même idées que le tiens...
Si ça t’intéresse je te ferais passer mon code ... mais j'ai encore quelques petits réglages à faire dessus, notamment pour "zoomer" sur l'image de mon picturebox ... voila donc je suis toujours en plein travail

Je te remercie pour m'avoir transmis ton code, car il se pourrais bien qu'il m'aide pour certaines chose.
Mais avant tout faut que j'arrive a bien le comprendre

Voila merci encore pour ton aide !

bonne soirée
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
29 févr. 2012 à 20:57
L'as tu testé pour voir ?
Si tu as des questions n'hésite pas ?
C'est pour un projet ?
0
Naudrey43 Messages postés 17 Date d'inscription lundi 6 décembre 2010 Statut Membre Dernière intervention 29 février 2012
29 févr. 2012 à 21:02
Je l'ai pas tester non... j'ai juste étudié le code... mais j'ésérais de l'adapter a mon projet prochainement, histoire de voir un peux comment fonctionne le tiens...

et oui c'est pour un projet, en faite je le développe pour une entreprise, je suis en stage actuellement.

et ce que je développe fera parti de mes projets de BTS voila
0
Rejoignez-nous