How To Drag And Drop Images Inside the Form In VB.NET

this is something very useful , i think you know what this actually do . the title says it . so lets get started making it just make a new project and name it whatever you want and double click your form and add this code


Public Class Form1

    'Author : Mohamed Shimran
    'Blog : http://www.ultimateprogrammingtutorials.blogspot.com

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AllowDrop = True
        Me.BackgroundImageLayout = ImageLayout.Stretch
        AddHandler Me.DragDrop, AddressOf Form_DragDrop
        AddHandler Me.DragEnter, AddressOf Form_DragEnter
    End Sub

    Private Sub Form_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim Files() As String
            Files = e.Data.GetData(DataFormats.FileDrop)
            Dim img As Image
            img = Image.FromFile(Files(0))
            Me.Width = img.Width
            Me.Height = img.Height
            Me.BackgroundImage = img
        End If
    End Sub
    Private Sub Form_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub
End Class

now debug and test it

here is a video preview of how it works

Post a Comment

Note: Only a member of this blog may post a comment.