I am trying to get some simple functionality of getting an image from a file, adding it to a Canvas, and then allowing a user to left-click (and hold) on the image and then drag it around the Canvas (i.e. updating the image's location)
Here's what I have so far, what should I be adding?
private void btnAddImage_Click(object sender, RoutedEventArgs e) {
try {
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(open.FileName);
myCanvas.children.add(PictureBox1);
}
}
catch (Exception) { throw new ApplicationException("Failed loading image"); }
}
You may add an Image control to the Canvas and modify its
Left
andTop
properties on mouse input.XAML:
Code behind:
You need to add drag and drop support in your code, by handling drag and drop routed events in the WPF controls you use.
If you use .NET 4.0 and above (Visual Studio 2010 and above), please see this MSDN Library of WPF Drag and Drop Overview.
But please take note on what kind of data that is being dragged and dropped as well.