How do you drag and drop a JPG image into a picturebox? 
Author Message
 How do you drag and drop a JPG image into a picturebox?

    I can't seem to drag and drop an image into a picturebox control using
vb.net and I haven't found any examples of this while searching the web.

    Has anybody here seen vb.net example for dragging and dropping an image
into a picturebox control?



Sat, 30 Apr 2005 07:03:29 GMT  
 How do you drag and drop a JPG image into a picturebox?
Hello Jim, you could inherit from the existing PictureBox control and add
some code to handle the drop. Here's a quick example, you'll probably want
to improve the feedback in OnDragOver and also improve the error handling
...

Public Class MyPictureBox
    Inherits PictureBox

    Public Sub New()
        Me.AllowDrop = True
    End Sub

    Protected Overrides Sub OnDragOver(ByVal e As DragEventArgs)
        e.Effect = DragDropEffects.Copy
    End Sub

    Protected Overrides Sub OnDragDrop(ByVal e As DragEventArgs)
        If (e.Data.GetDataPresent("FileNameW") = True) Then
            Dim fileNameArray As Array = e.Data.GetData("FileNameW", True)

            If fileNameArray.Length = 1 Then
                Dim fileName = CType(fileNameArray.GetValue(0), String)

                Try
                    Dim image As Image = image.FromFile(fileName)
                    Me.Image = image
                Catch ex As Exception
                    MessageBox.Show("Error loading image file: " &
ex.Message)
                End Try
            End If
        End If
    End Sub
End Class

You can then put it on your form by declaring a private variable of type
MyPictureBox and in the Form's constructor put some code similar to this:

pictureBox = New MyPictureBox
pictureBox.Location = New Point(10, 10)
pictureBox.Size = New Size(280, 280)
pictureBox.BorderStyle = BorderStyle.FixedSingle
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
Me.Controls.Add(pictureBox)

HTH

Antoine
Microsoft Visual Basic .NET

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
>     I can't seem to drag and drop an image into a picturebox control using
> vb.net and I haven't found any examples of this while searching the web.

>     Has anybody here seen vb.net example for dragging and dropping an
image
> into a picturebox control?



Sun, 01 May 2005 04:44:43 GMT  
 How do you drag and drop a JPG image into a picturebox?
    Found it!

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4...
=10

    My problem was not taking the time to get to know the DataFormats data
types.  I needed to use the DataTypes.FileDrop data type.

    Its really quite simple.  The following 2 code snippets are all that you
need to allow a PictureBox control (named MyPic) on Form1 to accept drag and
drop pictures from Windows Explorer.

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

    Me.MyPic.AllowDrop = True

End Sub
--------------------------------------------------------------

--------------------------------------------------------------
Private Sub MyPic_DragEnter(ByVal sender As Object, ByVal e
  As System.Windows.Forms.DragEventArgs) Handles picMyBailjumper.DragEnter

   If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
     ' Allow drop.
     e.Effect = DragDropEffects.Copy
   Else
     ' Do not allow drop.
     e.Effect = DragDropEffects.None
   End If

End Sub
--------------------------------------------------------------

    That's all there is to it!  (Strange how simple something is once you
know how it's done....)

    Thanks for your reply, Antoine!

    Jim Hubbard
        "The brighter you are, the more you have to learn." - Don Herold



Quote:
> Hello Jim, you could inherit from the existing PictureBox control and add
> some code to handle the drop. Here's a quick example, you'll probably want
> to improve the feedback in OnDragOver and also improve the error handling
> ...

> Public Class MyPictureBox
>     Inherits PictureBox

>     Public Sub New()
>         Me.AllowDrop = True
>     End Sub

>     Protected Overrides Sub OnDragOver(ByVal e As DragEventArgs)
>         e.Effect = DragDropEffects.Copy
>     End Sub

>     Protected Overrides Sub OnDragDrop(ByVal e As DragEventArgs)
>         If (e.Data.GetDataPresent("FileNameW") = True) Then
>             Dim fileNameArray As Array = e.Data.GetData("FileNameW", True)

>             If fileNameArray.Length = 1 Then
>                 Dim fileName = CType(fileNameArray.GetValue(0), String)

>                 Try
>                     Dim image As Image = image.FromFile(fileName)
>                     Me.Image = image
>                 Catch ex As Exception
>                     MessageBox.Show("Error loading image file: " &
> ex.Message)
>                 End Try
>             End If
>         End If
>     End Sub
> End Class

> You can then put it on your form by declaring a private variable of type
> MyPictureBox and in the Form's constructor put some code similar to this:

> pictureBox = New MyPictureBox
> pictureBox.Location = New Point(10, 10)
> pictureBox.Size = New Size(280, 280)
> pictureBox.BorderStyle = BorderStyle.FixedSingle
> pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
> Me.Controls.Add(pictureBox)

> HTH

> Antoine
> Microsoft Visual Basic .NET

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.



> >     I can't seem to drag and drop an image into a picturebox control
using
> > vb.net and I haven't found any examples of this while searching the web.

> >     Has anybody here seen vb.net example for dragging and dropping an
> image
> > into a picturebox control?



Sun, 01 May 2005 12:38:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. I need to drag and drop a jpg file from windows to an image control

2. how to drag and drop an image from a picturebox

3. Drag-n-Drop Explorer Image file into PictureBox

4. Error loading GIF or JPG image in PictureBox or Image controls

5. Doing Drag&Drop from IE

6. Need to Drag&Drop a bitmap into a PictureBox

7. Drag and Drop with a Picturebox

8. Using a PictureBox as a "viewer of jpg images

9. Saving Image or PictureBox control's contents to file (BMP/JPG etc)

10. preventing print scrn and drag and drop of image in IE 5.5

11. drag and drop to an image box

12. Drag and drop to an image box

 

 
Powered by phpBB® Forum Software