Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    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
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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 2011
    Posts
    696

    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.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width