相关疑难解决方法(0)

如何在非UI线程中编辑WritableBitmap.BackBuffer?

我的应用程序运行CPU密集的algorythms来编辑放置在WPF窗口的图像.我需要在后台线程中完成编辑.但是,尝试在非UI线程中编辑WritableBitmap的BackBuffer会引发InvalidOperationException.

    private WriteableBitmap writeableBitmap;

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // Create WritableBitmap in UI thread.
        this.writeableBitmap = new WriteableBitmap(10, 10, 96, 96, PixelFormats.Bgr24, null);
        this.image1.Source = this.writeableBitmap;

        // Run code in non UI thread.
        new Thread(
            () =>
            {
                // 'Edit' bitmap in non UI thread.
                this.writeableBitmap.Lock(); // Exception: The calling thread cannot access this object because a different thread owns it.

                // ... At this place the CPU is highly loaded, we edit this.writeableBitmap.BackBuffer.

                this.writeableBitmap.Unlock();
            }).Start();
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf

6
推荐指数
1
解决办法
8239
查看次数

标签 统计

.net ×1

c# ×1

wpf ×1