.net - 从 Mysql 异步加载图像到 WPF

标签 .net mysql wpf xaml

我不想知道在 WPF 应用程序中从 Mysql 异步加载图像的最佳方法。

谢谢。

最佳答案

使用ThreadPool从数据库加载图片。

所以,像这样:

private void button1_Click(object sender, RoutedEventArgs e)
{
    ThreadPool.QueueUserWorkItem(LoadImage, new LoadImageRequest { ImageName = "Image.png", Control = image1 });
}

private void LoadImage(object state)
{
    var request = (LoadImageRequest)state;

    byte[] data = ...; // load bytes from the database using request.ImageName

    using (var stream = new MemoryStream(data))
    {
        var imageSource = BitmapFrame.Create(stream);

        Dispatcher.BeginInvoke(
            new Action<ImageSource>(p => request.Control.Source = p), imageSource
        );
    }
}

private class LoadImageRequest
{
    public string ImageName;
    public Image Control;
}

关于.net - 从 Mysql 异步加载图像到 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171452/

相关文章:

mysql - 数据库设计 : Where to store account balance?

php - 从日期到日期 - 搜索和显示

mysql - 为什么会出现列表索引越界 (1) 错误?

wpf - 通过命令参数传递参数

c# - 同一解决方案中的不同 EntityFramework 版本

c# - SandcaSTLe 错误 BE0073 .NET Framework 4.5

c# - WPF:创建一个看起来像 Visual Studio 2010 的应用程序

c# - 控制台或图形游戏设计?

.net - 强制异常(exception)语言为英语

c# - Amazon .NET AWS SDK 的 AmazonS3 线程安全吗?