C# WPF - 拖动图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17035225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:58:50  来源:igfitidea点击:

C# WPF -Drag an image

c#wpfimagewpf-controls

提问by Ace

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"); }
}

回答by Clemens

You may add an Imagecontrol to the Canvas and modify its Leftand Topproperties on mouse input.

您可以将Image控件添加到 Canvas 并在鼠标输入时修改其LeftTop属性。

XAML:

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Canvas x:Name="canvas"
            MouseLeftButtonDown="CanvasMouseLeftButtonDown"
            MouseLeftButtonUp="CanvasMouseLeftButtonUp"
            MouseMove="CanvasMouseMove"/>
    <Button Grid.Row="1" Content="Add Image" Click="AddButtonClick"/>
</Grid>

Code behind:

后面的代码:

private void AddButtonClick(object sender, RoutedEventArgs e)
{
    var dialog = new Microsoft.Win32.OpenFileDialog();
    dialog.Filter =
        "Image Files (*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

    if ((bool)dialog.ShowDialog())
    {
        var bitmap = new BitmapImage(new Uri(dialog.FileName));
        var image = new Image { Source = bitmap };
        Canvas.SetLeft(image, 0);
        Canvas.SetTop(image, 0);
        canvas.Children.Add(image);
    }
}

private Image draggedImage;
private Point mousePosition;

private void CanvasMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var image = e.Source as Image;

    if (image != null && canvas.CaptureMouse())
    {
        mousePosition = e.GetPosition(canvas);
        draggedImage = image;
        Panel.SetZIndex(draggedImage, 1); // in case of multiple images
    }
}

private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (draggedImage != null)
    {
        canvas.ReleaseMouseCapture();
        Panel.SetZIndex(draggedImage, 0);
        draggedImage = null;
    }
}

private void CanvasMouseMove(object sender, MouseEventArgs e)
{
    if (draggedImage != null)
    {
        var position = e.GetPosition(canvas);
        var offset = position - mousePosition;
        mousePosition = position;
        Canvas.SetLeft(draggedImage, Canvas.GetLeft(draggedImage) + offset.X);
        Canvas.SetTop(draggedImage, Canvas.GetTop(draggedImage) + offset.Y);
    }
}

回答by Eriawan Kusumawardhono

You need to add drag and drop support in your code, by handling drag and drop routed events in the WPF controls you use.

您需要通过处理您使用的 WPF 控件中的拖放路由事件,在您的代码中添加拖放支持。

If you use .NET 4.0 and above (Visual Studio 2010 and above), please see this MSDN Library of WPF Drag and Drop Overview.

如果您使用 .NET 4.0 及更高版本(Visual Studio 2010 及更高版本),请参阅此 WPF 拖放概述的MSDN 库。

But please take note on what kind of data that is being dragged and dropped as well.

但请注意拖放的数据类型。