Results 1 to 4 of 4

Thread: How to drag and drop image from PictureBox to file.

  1. #1

    How to drag and drop image from PictureBox to file.

    Hi all.

    Is it possible to drag and drop a drawn image from a PictureBox and save it to file.

    EX: If I drag and drop the PictureBox to my desktop I need it to save it as a .bmp

    Here's my save to file code.
    Code:
            mPrintBitMap = New Bitmap(Piclvl1.Width, Piclvl1.Height)
            Dim lRect As System.Drawing.Rectangle
            lRect.Width = Piclvl1.Width
            lRect.Height = Piclvl1.Height
    
            Me.DrawToBitmap(mPrintBitMap, lRect)
    Brian Henry
    VB.Net 2008
    VB.Net 2010
    ISaGRAF

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,489

    Re: How to drag and drop image from PictureBox to file.

    Er ... not seeing any save to file code there.

    In principle, yes it's possible but I'm not sure how it's preferable to the standard name it, save it routine.

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: How to drag and drop image from PictureBox to file.

    Quote Originally Posted by dunfiddlin View Post
    Er ... not seeing any save to file code there.

    In principle, yes it's possible but I'm not sure how it's preferable to the standard name it, save it routine.
    Drag and drop would be quicker for the lazy people using his application.

    I would say you'd need to use the drag event handlers for that picturebox, then read the stream of the picturebox.image to the location where you've "dragged" the image to in the filesystem. Which probably would require some unmanaged API call, or Windows Messages. That's without much thought into this, but that's how you'd go about it.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,785

    Re: How to drag and drop image from PictureBox to file.

    You can only initiate the drag operation. It will be Windows Explorer that needs to handle the drop operation. As such, you need to provide data in a format that Windows Explorer will understand. The Control.DoDragDrop method says:
    The allowedEffects parameter determines which drag operations can occur. If the drag operation needs to interoperate with applications in another process, data should either be a base managed class ( String, Bitmap, or Metafile), or an object that implements ISerializable or IDataObject.
    As you're using a Bitmap, you should be good to go. There's a little more to it than this but, basically, you call DoDragDrop and pass your Bitmap to the 'data' parameter. There are lots of examples of drag & drop code around. I've added one in the CodeBank myself, although it just works within the same application. It will still show you how to call DoDragDrop and handle the appropriate events. There's a link to my CodeBank submissions in my signature.

    That said, given that the user would still have to navigate to the desired folder in Windows Explorer and still have to name the file, just using a SaveFileDialog seems less trouble to me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •