Tuesday, February 23, 2010

Browse Image and Drag It Silverlight

Hello Guys,

Early this morning I’m thinking about the draggable Image in Silverlight. I created a sample with simple snippets.

1. Create one Image, rectangle, and button.

2. Create an event handler for Image.

this.image1.MouseMove += new MouseEventHandler(image1_MouseMove);
this.image1.MouseLeftButtonDown += new MouseButtonEventHandler(image1_MouseLeftButtonDown);
this.image1.MouseLeftButtonUp += new MouseButtonEventHandler(image1_MouseLeftButtonUp);

3. Setting a variable

private bool isDragging = false; //dragging?
private Point mousePosition; //position of mouse

4. And now the events

void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.image1.ReleaseMouseCapture();
isDragging = false;
} //Release the mouse

void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mousePosition = e.GetPosition(sender as UIElement);
this.image1.CaptureMouse();
isDragging = true;
} //Pressed the left mouse button

void image1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.imgTranslateTransform.X = e.GetPosition(this).X - mousePosition.X;
this.imgTranslateTransform.Y = e.GetPosition(this).Y - mousePosition.Y;
}
} //Dragging the Image


private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();

BitmapImage imgSrc = new BitmapImage();
imgSrc.SetSource(ofd.File.OpenRead());
image1.Source = imgSrc;
textBlock1.Text = "You can now drag the Image";
} // Open/Browsing the Image (binding Image)

4 comments:

  1. Hi.
    I try to use your snippet but I can't use the imgTranslateTransform... Is this snippet for SL 3.0 or 4.0?
    I try for 3.0, but it seems like I need to use a special Using or dll?
    should you help me please?

    Thak you.
    Att
    Sergio

    ReplyDelete
  2. You can also use Canvas and change the Canvas.Top and Left properties. I've implemented lot of features about objects (de)selecting, moving, dragging, even (group) resizing in our internal project. It's quite a fun, but there is so many limitations in Silverlight, for example there's Control.CaptureMouse(), but there's no IsMouseCaptured property just like in WPF. Also there's no way to change the coordinate system of Canvas without the need of manually flipping the RenderTransform of Canvas, but such transform brings problems with proportional object like texts, objects with borders of specific size and so on...

    ReplyDelete
  3. Hi Leap,

    Sorry for late reply too busy at work. I'm only using SL3. I didnt us SL4 unless there a final release on it. By the way I dont have any dll used in this sample. imgTranslateTransform located at between image tag. Then

    Image.RenderTransform
    TranslateTransform x:Name="imgTranslateTransform" X="0" Y="0">
    TranslateTransform
    Image.RenderTransform

    ReplyDelete
  4. excellent job
    nice program
    i like it very much .....
    thanks a lot

    ReplyDelete