Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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
C# 在WPF中创建自定义图像显示作为优化机制_C#_Wpf_Optimization_Wpf Controls_Custom Controls - Fatal编程技术网

C# 在WPF中创建自定义图像显示作为优化机制

C# 在WPF中创建自定义图像显示作为优化机制,c#,wpf,optimization,wpf-controls,custom-controls,C#,Wpf,Optimization,Wpf Controls,Custom Controls,我正在一个自定义类上进行一些图像处理,该类表示16位灰度图像。 像素的强度存储在一个一维ushort数组中:ushort[]data 我也有宽度,高度,dpi,步幅,如果必要的话 我的动机如下:我非常快地显示了某些操作的结果,但是从数组到bitmapsource再到image对象的转换太长了,所以我想到了一个直接从数组中提取“源”的image对象。因此,我可以编写一个方法“update()”,而不是进行多次转换 1/这可能吗 2/会更快吗 3/我该怎么做 我目前绘制图像的方式是使用以下代码(还有

我正在一个自定义类上进行一些图像处理,该类表示16位灰度图像。 像素的强度存储在一个一维ushort数组中:
ushort[]data
我也有宽度,高度,dpi,步幅,如果必要的话

我的动机如下:我非常快地显示了某些操作的结果,但是从数组到bitmapsource再到image对象的转换太长了,所以我想到了一个直接从数组中提取“源”的image对象。因此,我可以编写一个方法“update()”,而不是进行多次转换

1/这可能吗

2/会更快吗

3/我该怎么做

我目前绘制图像的方式是使用以下代码(还有更多,但本质上是它的核心)


谢谢大家!

与其使用单独的数组来存储像素,然后创建位图源来显示结果,我认为最好使用可写位图,这样您就可以将像素数据(16位灰度值)直接存储在其内部。因此,您可以编写如下代码:

    // You create "bmp" just once and then update its content when needed
    var bmp = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Gray16, null);
    var imgRect = new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);
    unsafe public void ApplySomeFilter(void* imgBuffer, ImageFilter filter)
    {
        // code that modifies pixels goes here
    }
然后更新图像:

    bmp.Lock();
    ApplySomeFilter(bmp.BackBuffer, ImageFilter.Blur);   // this is just an example ;)
    bmp.AddDirtyRect(imgRect);
    bmp.Unlock();
ApplySomeFilter方法可以使用不安全的代码修改可写位图的像素数据。
例如,如果ApplySomeFilter的定义如下:

    // You create "bmp" just once and then update its content when needed
    var bmp = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Gray16, null);
    var imgRect = new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);
    unsafe public void ApplySomeFilter(void* imgBuffer, ImageFilter filter)
    {
        // code that modifies pixels goes here
    }
那么你可以这样称呼它:

    ApplySomeFilter(bmp.BackBuffer.ToPointer(), ImageFilter.Blur);

非常感谢。不直接回答问题,但提供了一个非常好的解决方法@EdwinG,如果您喜欢使用单独的数组来存储像素信息并更新BitmapSource(不是每次都创建一个新的BitmapSource),您仍然可以使用WriteableBitmap并使用该方法将数组转储到其中。