0

The goal is to store bytes of many same sized images and draw them in WriteableBitmap to create high performance video.

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.' Line number '6' and line position '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:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll Additional information: Buffer size is not sufficient.

5
  • You can't write an encoded image buffer (a PNG here) into a WriteableBitmap. Instead, you'll have to write raw pixel data. Get those by CopyPixels from the source bitmap.
    – Clemens
    Commented Aug 20, 2016 at 12:05
  • Besides that the whole approach doesn't seem to make much sense. Why copy all the pixels of a source bitmap into a target WriteabelBitmap, when you could instead simply show the source bitmap?
    – Clemens
    Commented Aug 20, 2016 at 12:07
  • @Clemens, I am new to WriteableBitmap and don't know how it works. I want to know how to do it, i.e. store bytes of different images and then write them by WriteableBitmap to create a high performance video. I need a working example similar to my code. Thanks, I will try CopyPixels. Meanwhile, I have a video updated by an array of cached BitmapImage instances. Commented Aug 20, 2016 at 12:15
  • The whole point of the WriteableBitmap is that you write the pixel data into it directly, and only the parts that need to be updated. If you copy an entire bitmap into it, it seems you have defeated the purpose.
    – doubleYou
    Commented Aug 20, 2016 at 13:46
  • @doubleYou, Thanks for clarifying, but anyway, I want to check it to see what happens. I will check CopyPixels. Commented Aug 20, 2016 at 15:23

1 Answer 1

2

You should use CopyPixels.

MainWindow.xaml:

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

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.
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.