c# - Windows Phone 上的 WriteableBitmap 高速绘图

标签 c# wpf xaml windows-phone-8 windows-phone

我正在 Windows Phone 上试验 WritableBitmapEx。我创建了一个简单的示例,一个上下移动的简单盒子。

有一个绘制函数,可以在每一帧重绘矩形:

    int y = 0;
    int dy = 15;
    public void draw()
    {
        y += dy;

        if (y > 500 || y < 0)
            dy = -dy;

        writeableBmp.Clear(System.Windows.Media.Colors.Black);
        writeableBmp.FillRectangle(0, y, 100, y + 100, System.Windows.Media.Colors.Green);
    }

Loaded 事件创建 Writable 位图并在每一帧上调用 draw()

    WriteableBitmap writeableBmp;
    private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        writeableBmp = BitmapFactory.New((int)ContentPanel.ActualWidth, (int)ContentPanel.ActualHeight);
        image.Source = writeableBmp;
        writeableBmp.GetBitmapContext();
        CompositionTarget.Rendering += CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        draw();
    }

但这最多给了我 ~30FPS,所以动画不流畅。

知道有一些更好的方法可以在 xaml 中创建此类动画(例如,创建一个矩形对象并使用 xaml 动画制作动画),但是有一个游戏 (在另一种语言中)以这种方式重新绘制每一帧,我的最终目标是将该代码移植到 Windows Phone。因此,找到一种足够快的重绘方法可以让移植真正更容易。

那么,有没有办法提高它的性能呢?还是有更好的方法手动绘制每一帧,但速度足够快(60fps)?

最佳答案

试用 Microsoft Win2D。您可以在此处使用 NuGet 或他们的 GitHub 获取它:Microsoft Win2D GitHub .它基本上是 Direct2D 的包装器,使用起来非常简单。

功能(复制自:https://github.com/Microsoft/Win2D/wiki/Features)

Easy-to-use Windows Runtime API

•Available from .NET and C++

•Supports Windows 10, Windows 8.1, and Windows Phone 8.1

Immediate mode 2D graphics rendering with GPU acceleration

•Implemented as a layer on top of Direct2D, DirectImage, and DirectWrite

•Interop to and from underlying types, so you can mix & match Win2D with native D2D

Bitmap graphics

•Load, save, and draw bitmap images

•Render to texture

•Use bitmaps as opacity masks

•Sprite batch API for efficiently drawing large numbers of bitmaps

•Use block compressed bitmap formats to save memory

•Load, save, and draw virtual bitmaps, which can be larger than the maximum GPU texture size and are automatically split into tiles

Vector graphics

•Draw primitive shapes (lines, rectangles, circles, etc.) or arbitrarily complex geometry

•Fill shapes using solid colors, image brushes, or linear and radial gradients

•Draw lines of any width with flexible stroke styles (dotted, dashed, etc.)

•High quality antialiasing

•Rich geometry manipulation (union, intersect, compute point on path, tessellate, etc.)

•Clip drawing to arbitrary geometric regions

•Capture drawing operations in command lists for later replay

•Rasterize ink strokes (from a stylus)

Powerful image processing effects

•Blurs

•Blends

•Color adjustments (brightness, contrast, exposure, highlights & shadows, etc.)

•Filters (convolve, edge detection, emboss, sharpen)

•Lighting

•Custom pixel shaders

•And many more...

Text

•Fully internationalized Unicode text rendering

•Text layouts can be drawn, measured, or hit-tested against

•Convert text outlines to geometry

•Enumerate fonts and query their metrics

•Draw or manipulate individual glyph runs to create custom text layouts

UI integration

•XAML CanvasControl make it easy to get up and running

•Can also create advanced things like owner-draw XAML controls

•XAML CanvasAnimatedControl provides Update/Draw game loop programming model

•XAML CanvasVirtualControl for drawing to very large virtualized surfaces

•Draw onto Windows.UI.Composition drawing surfaces and swapchains

•Can also draw directly to a CoreWindow

•Printing

关于c# - Windows Phone 上的 WriteableBitmap 高速绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983803/

相关文章:

c# - 后台工作人员更新用户界面上的日志

c# - 是否可以绑定(bind)对象列表?

c# - 用 Observable.Create 包装 Observable.FromEventPattern

c# - winforms 或 wpf 应用程序的脚手架功能

c# - LINQ 无延迟加载

c# - 在父 View 模型中公开 subview 模型的属性

wpf 触发器 Datagrid RowCount

c# - 从另一个页面读取 IsolatedStorage 中的设置

wpf - ListView 组标题显示多次

c# - SQL CommandText 语法不正确