开发者

How to render a series of computed bitmaps as fast as possible in Silverlight?

I'm interested in displaying a series of computed bitmaps to the screen in Silverlight as fast as possible for the purpose of animation. Right now this is the strategy I am using which results in mid 50ies FPS on my laptop for a 1200x700 pixel image.

Can you recommend a better way?

public partial class MainPage : UserControl
{
    private int _height;
    private int _width;
    private WriteableBitmap _bitmap;
    private DateTime _start;
    private int _count = 0;

    public MainPage()
    {
        InitializeComponent();
        _width = (int)this.MainImage.Width;
        _height = (int)this.MainImage.Height;

        _bitmap = new WriteableBitmap(_width, _height);
        this.MainImage.Source = _bitmap;

        _s开发者_开发问答tart = DateTime.Now;
        RenderFrame();
    }

    private void RenderFrame()
    {
        Dispatcher.BeginInvoke(RenderFrameHelp);
    }

    private void RenderFrameHelp()
    {
        int solid = -16777216;
        for (int i = 0; i < _width * _height; i++)
        {
            _bitmap.Pixels[i] = _count % 2 == 0 ? 255 : 100 | solid;
        }
        _bitmap.Invalidate();
        this.FPS.Text = (_count++ / (DateTime.Now - _start).TotalSeconds).ToString();
        RenderFrame();
    }

}


QuakeLight uses roughly the following solution:

Instead of using a WriteableBitmap, you can make a very basic PNG encoder (raw bitmap, no compression, take it from QuakeLight if you must). Fill a normal array with pixel data, encode it as PNG in memory, then wrap it in a MemoryStream and attach it to an Image. Making an uncompressed PNG basically means slapping a fixed size header in front of your array.

You could even use a producer-consumer queue so that you can build your PNG in a separate thread, letting you utilize multi-core systems for better performance.

Hope this helps. Share your experience if you try this method.


The fastest approach will probably be to pre-render the images in your animation to a list of WriteableBitmaps and then selectively set each of them as the source of an Image control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜