Drag and Drop images from Filesystem to Silverlight 4 Applications

Thu, Nov 26, 2009

Silverlight, Tutorials, Video Tutorials



In this video I show how to drag and drop images from the filesystem into a Silverlight 4 application.
On an image component simply add a drop event and set AllowDrop=”True”
You will need to download the tools to build Silverlight 4 beta applications, make sure to download Microsoft Expression Blend 4.

XAML code


<Image x:Name="img" Height="640" Width="426" Source="Image2.png" Stretch="Fill" Drop="OnDropEvent" AllowDrop="True"/>

C# code


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;

namespace SilverlightApplication14
{
	public partial class IPhoneMain : UserControl
	{
		public IPhoneMain()
		{
			InitializeComponent();
		}

		private void OnDropEvent(object sender, DragEventArgs e)
		{
			FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

			foreach (FileInfo fileInfo in files)
			{
				FileStream fs = fileInfo.OpenRead();
				BitmapImage bi = new BitmapImage();
				bi.SetSource(fs);
				img.Source = bi;
				fs.Close();
			}
		}
	}
}
, , ,

Leave a Reply

You must be logged in to post a comment.