Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 从桌面拖动图像并将其放到位图查看器_Wpf_Drag And Drop_Bitmapimage - Fatal编程技术网

Wpf 从桌面拖动图像并将其放到位图查看器

Wpf 从桌面拖动图像并将其放到位图查看器,wpf,drag-and-drop,bitmapimage,Wpf,Drag And Drop,Bitmapimage,我想从桌面拖动一个图像(人物图像),然后将其放到我的wpf应用程序中 有什么资源吗?XAML: <Window x:Class="_13378018.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWind

我想从桌面拖动一个图像(人物图像),然后将其放到我的wpf应用程序中

有什么资源吗?

XAML:

<Window x:Class="_13378018.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" AllowDrop="True" Drop="OnDrop">
    <Grid>
        <Image x:Name="imageViewer"/>
    </Grid>
</Window>
XAML:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private BitmapImage LoadImageFromFile(string filename)
    {
        using (var fs = File.OpenRead(filename))
        {
            var img = new BitmapImage();
            img.BeginInit();
            img.CacheOption = BitmapCacheOption.OnLoad;
            // Downscaling to keep the memory footprint low
            img.DecodePixelWidth = (int)SystemParameters.PrimaryScreenWidth;
            img.StreamSource = fs;
            img.EndInit();
            return img;
        }
    }

    private void OnDrop(object sender, DragEventArgs e)
    {
        var data = e.Data as DataObject;
        if (data.ContainsFileDropList())
        {
            var files = data.GetFileDropList();
            imageViewer.Source = LoadImageFromFile(files[0]);
        }
    }
}