Image dragging and dropping in Silverlight
In this sample code I am going to demonstrate how we can drag and drop an image from windows explorer to a Silverlight page.
To run the test we should have a page with an Image control. Lets call our Image control as ImgShow. In the xaml, set the AllowDrop property to true. Also add a drop event to the Image control. Once the drop event is added go to the code behind and add the below code to the event.
private void ImgShow_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { FileInfo[] fi = (FileInfo[])e.Data.GetData(DataFormats.FileDrop); BitmapImage img = new BitmapImage(); using (Stream fileStream = fi[0].OpenRead()) { img.SetSource(fileStream); } ImgShow.Source = img; } }
We are done with the application. Run the application and drag and drop a file from Windows explorer to ImgShow image control.
Technorati Tags: Silverlight,File drag and drop
Leave a Reply