dx绘制2d图像_如何在UWP中使用SharpDx绘制2d图像

本文介绍如何在UWP应用中利用SharpDX库创建一个名为ImageSource2D的类,继承自SurfaceImageSource,从而实现在XAML元素(如Image和Ellipse)上动态绘制2D图像。通过创建Direct3D和Direct2D设备,实现绘制并更新SurfaceImageSource的内容。
摘要由CSDN通过智能技术生成

如何在UWP中使用SharpDX(查看github库 SharpDX-Samples-master总结)

使用 Windows.UI.Xaml.Media.Imaging.SurfaceImageSource 给前台的control显示。

新建一个对象ImageSource2D继承SurfaceImageSource,通过使用sharpdx绘制SurfaceImageSource

显示在前端xaml元素。(可以是UIElement的fill或者image的source)

----------

xaml前端有控件Image1,Ellipse1绘制他们的SurfaceImageSource,代码如下

namespace MyFirstDxGame

{

///

/// win2d 测试

///

public sealed partial class BlankPage1 : Page

{

private ImageSource2D MyDrawing;

public BlankPage1()

{

this.InitializeComponent();

MyDrawing = new ImageSource2D((int)Image1.Width, (int)Image1.Height, true);

// Use Scenario1Drawing as a source for the Image control

Image1.Source = MyDrawing;

// Use Scenario1Drawing as a source for the Ellipse shape's fill

Ellipse1.Fill = new Windows.UI.Xaml.Media.ImageBrush() { ImageSource = MyDrawing };

}

protected override void OnNavigatedTo(NavigationEventArgs e)

{

Image1_Tapped(null,null);

}

private void Image1_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)

{

// Begin updating the SurfaceImageSource

MyDrawing.BeginDraw();

// Clear background

MyDrawing.Clear(Colors.Bisque);

// Create a new pseudo-random number generator

Random randomGenerator = new Random();

byte[] pixelValues = new byte[3]; // Represents the red, green, and blue channels of a color

// Draw 50 random retangles

for (int i = 0; i < 50; i++)

{

// Generate a new random color

randomGenerator.NextBytes(pixelValues);

Windows.UI.Color color = new Windows.UI.Color() { R = pixelValues[0], G = pixelValues[1], B = pixelValues[2], A = 255 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值