|
Add Images to SQL Server
Drag images from the file system over a WinForms PictureBox and trap their file information when you drop them.
by Jason Beres
Posted May 3, 2004
Technology Toolbox: VB.NET, SQL Server 2000
Drag-and-drop is one of the most productive computer features since the Apple Lisa. You're sure to keep your users happy if you implement drag-and-drop features in your applications. In this article, I'll show you several cool and useful tricks. First, I'll demonstrate how to drag images from the file system over a Windows Forms PictureBox control and trap the file information for the images when you drop them. From there, I'll discuss how to add the images from the file system to a SQL Server database. I've coined the end product the "Cool and Fancy Image Manager" (or just "Image Manager" for short), and I've included a figure that shows you what it looks like (see Figure 1). I won't go into the internals about drag-and-drop or image manipulation, but will teach you practical ways to apply some effective user interface (UI) techniques, as well as some other good stuff along the way.
I created the Image Manager for an internal application at my company so that anyone in the company would be able to add image data to the Web site easily. We had an internal application that managed all the pages for our sites, but the "Add Image" button was a drawback. It didn't provide an effective way for users to associate items with a page. I wanted something better—thus, the Image Manager.
The user interface for Image Manager is simple. It includes a PictureBox (named pb) that facilitates drag-and-drop operations. This control is key to the application. It handles the capture of the Drop event, which fires off the events that save the image to the database. It also includes two or more PictureBox controls that display a full-size image from the database, and a thumbnail image of the full-size image. Last, it includes a ListView control that displays the items stored in the database.
Note that many controls don't display the AllowDrop property in the Properties window at design time in Visual Studio .NET. For this reason, you need to set this property to True, as this code demonstrates for the PictureBox named pb in the Form1_Load event of the MainForm in the Image Manager app:
Private Sub Form1_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Make sure you set the
' AllowDrop property to True,
' This is not available
' at design time
pb.AllowDrop = True
End Sub
I've noted other examples that explain how to do a simple drag-and-drop operation from a PictureBox (see Additional Resources). You can accept multiple files in the Image Manager and save them to SQL Server all at once.
Back to top
|