简体   繁体   English

如何通过BitmapImage中的字节写入WriteableBitmap中的像素?

[英]How to write pixels in WriteableBitmap by bytes from BitmapImage?

The goal is to store bytes of many same sized images and draw them in WriteableBitmap to create high performance video. 目标是存储许多相同大小图像的字节,并在WriteableBitmap中绘制它们以创建高性能视频。

I tried next code: 我尝试了下一个代码:

    public MainWindow()
    {
        InitializeComponent();

        Method();
    }

    private void Method()
    {
        BitmapImage bi = new BitmapImage(new Uri(@"Image.png", UriKind.Relative));
        int pw = bi.PixelWidth;
        int ph = bi.PixelHeight;

        WriteableBitmap wb = new WriteableBitmap(
            pw,
            ph,
            96,
            96,
            PixelFormats.Bgra32,
            null);

        byte[] data;
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bi));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }

        int stride = 4 * pw;

        wb.Lock();
        wb.WritePixels(new Int32Rect(0, 0, pw, ph), data, 4 * pw, 0);
        wb.Unlock();
    }

Error: 错误:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll Additional information: 'The invocation of the constructor on type 'WpfApplication2.MainWindow' that matches the specified binding constraints threw an exception.' 引发的异常:PresentationFramework.dll中的'System.Windows.Markup.XamlParseException'其他信息:'对与指定的绑定约束匹配的类型'WpfApplication2.MainWindow'的构造函数的调用引发了异常。 Line number '6' and line position '9'. 行号“ 6”和行位置“ 9”。 If there is a handler for this exception, the program may be safely continued. 如果有用于此异常的处理程序,则可以安全地继续执行该程序。

If I place the same code in UserControl, it gives next error: 如果将相同的代码放在UserControl中,则会出现下一个错误:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll Additional information: Buffer size is not sufficient. PresentationCore.dll中发生了'System.ArgumentException'类型的未处理异常。其他信息:缓冲区大小不足。

You should use CopyPixels. 您应该使用CopyPixels。

MainWindow.xaml : MainWindow.xaml

<Grid>
    <Image x:Name="image"></Image>
</Grid>

MainWindow.xaml.cs : MainWindow.xaml.cs

    private void Method()
    {
        BitmapImage bi = new BitmapImage(new Uri(@"Image.png", UriKind.Relative));

        int stride = bi.PixelWidth * (bi.Format.BitsPerPixel + 7) / 8;
        byte[] data = new byte[stride * bi.PixelHeight];

        bi.CopyPixels(data, stride, 0);

        WriteableBitmap wb = new WriteableBitmap(
            bi.PixelWidth,
            bi.PixelHeight,
            bi.DpiX, bi.DpiY,
            bi.Format, null);

        wb.WritePixels(
            new Int32Rect(0, 0, bi.PixelWidth, bi.PixelHeight),
            data, stride, 0);

        image.Source = wb; // an Image class instance from XAML.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM