当前位置:网站首页>WPF 使用封装的 SharpDx 控件
WPF 使用封装的 SharpDx 控件
2022-08-04 18:38:00 【林德熙】
上一篇告诉大家如何在 WPF 使用 SharpDx ,看起来代码比较复杂,所以本文告诉大家如何使用我封装的控件。
本文是一个系列,希望大家从第一篇开始看
- WPF 使用 Direct2D1 画图入门
- WPF 使用 Direct2D1 画图 绘制基本图形
- WPF 使用 SharpDX
- WPF 使用 SharpDX 在 D3DImage 显示
- WPF 使用封装的 SharpDx 控件
在WPF 使用 SharpDX 在 D3DImage 显示我告诉大家如何在 WPF 使用,但是代码都是写在一个 MainPage ,所以下面我把代码封装一下,放在一个类。
我的代码可以复制一下放在自己的工程使用,现在我还不想做 Nuget 因为这个类还有性能问题。
使用这个类作为 Image 的 Source 会占用 3% 的 CPU ,而且这个类没有注释,关于这个类是如何写的请看WPF 使用 SharpDX 在 D3DImage 显示 。
我会把这个类的代码放在文章最后,方便大家复制。
下面来告诉大家如何使用这个类。
首先复制代码,放在一个文件
写一个类继承 SharpDxImage ,这里我随意写一个类叫 SsgnnnaTkmlo ,这个类可以重写 OnRender ,也就是在绘制需要显示什么。
public class SsgnnnaTkmlo : SharpDxImage
{
/// <inheritdoc />
protected override void OnRender(RenderTarget renderTarget)
{
//随便画一个矩形。下面的代码就是清空屏幕,参数 null 为透明,可以给其他的颜色。如何绘制请看文章。
renderTarget.Clear(null);
var brush = new SharpDX.Direct2D1.SolidColorBrush(renderTarget, new RawColor4(1, 0, 0, 1));
var kvudjuzjsHlqiv = ran.Next((int) 100 - 10);
var dfulTokpj = ran.Next((int) 100 - 10);
renderTarget.DrawRectangle(
new RawRectangleF(kvudjuzjsHlqiv, dfulTokpj, kvudjuzjsHlqiv + 10, dfulTokpj + 10), brush, 1);
}
private Random ran = new Random();
}需要告诉大家的是,传入 RenderTarget 的绘制和之前其他代码的绘制是一样,关于 SharpDx 的绘制我会在另一篇博客告诉大家。
然后打开 xaml 写入下面代码
<Image x:Name="DcwtTmmwvcr">
<Image.Source>
<local:SsgnnnaTkmlo x:Name="DrmKroh"></local:SsgnnnaTkmlo>
</Image.Source>
</Image>当然,因为只是简单的例子,大家也可以写在后台代码。
在 xaml.cs 写下面代码,在 Load 绑定
DcwtTmmwvcr.Loaded += (s, e) =>
{
DrmKroh.CreateAndBindTargets((int) ActualWidth, (int) ActualHeight);
};注意需要使用图片控件的 Load 事件,不然拿到的图片会模糊。
现在可以尝试运行一下,就可以看到一个随机出现的矩形。
下面就是封装类的代码。
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using SharpDX.Direct3D;
namespace WPFSharpDx
{
public abstract class SharpDxImage : D3DImage
{
public void CreateAndBindTargets(int actualWidth, int actualHeight)
{
var width = Math.Max(actualWidth, 100);
var height = Math.Max(actualHeight, 100);
var renderDesc = new SharpDX.Direct3D11.Texture2DDescription
{
BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
Width = width,
Height = height,
MipLevels = 1,
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
Usage = SharpDX.Direct3D11.ResourceUsage.Default,
OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared,
CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
ArraySize = 1
};
var device = new SharpDX.Direct3D11.Device(DriverType.Hardware,
SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport);
var renderTarget = new SharpDX.Direct3D11.Texture2D(device, renderDesc);
var surface = renderTarget.QueryInterface<SharpDX.DXGI.Surface>();
var d2DFactory = new SharpDX.Direct2D1.Factory();
var renderTargetProperties =
new SharpDX.Direct2D1.RenderTargetProperties(
new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown,
SharpDX.Direct2D1.AlphaMode.Premultiplied));
_d2DRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2DFactory, surface, renderTargetProperties);
SetRenderTarget(renderTarget);
device.ImmediateContext.Rasterizer.SetViewport(0, 0, width, height);
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
protected abstract void OnRender(SharpDX.Direct2D1.RenderTarget renderTarget);
private SharpDX.Direct3D9.Texture _renderTarget;
private SharpDX.Direct2D1.RenderTarget _d2DRenderTarget;
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
Rendering();
}
private void Rendering()
{
_d2DRenderTarget.BeginDraw();
OnRender(_d2DRenderTarget);
_d2DRenderTarget.EndDraw();
Lock();
AddDirtyRect(new Int32Rect(0, 0, PixelWidth, PixelHeight));
Unlock();
}
private void SetRenderTarget(SharpDX.Direct3D11.Texture2D target)
{
var format = TranslateFormat(target);
var handle = GetSharedHandle(target);
var presentParams = GetPresentParameters();
var createFlags = SharpDX.Direct3D9.CreateFlags.HardwareVertexProcessing |
SharpDX.Direct3D9.CreateFlags.Multithreaded |
SharpDX.Direct3D9.CreateFlags.FpuPreserve;
var d3DContext = new SharpDX.Direct3D9.Direct3DEx();
var d3DDevice = new SharpDX.Direct3D9.DeviceEx(d3DContext, 0, SharpDX.Direct3D9.DeviceType.Hardware,
IntPtr.Zero, createFlags,
presentParams);
_renderTarget = new SharpDX.Direct3D9.Texture(d3DDevice, target.Description.Width,
target.Description.Height, 1,
SharpDX.Direct3D9.Usage.RenderTarget, format, SharpDX.Direct3D9.Pool.Default, ref handle);
using (var surface = _renderTarget.GetSurfaceLevel(0))
{
Lock();
SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
Unlock();
}
}
private static SharpDX.Direct3D9.PresentParameters GetPresentParameters()
{
var presentParams = new SharpDX.Direct3D9.PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SharpDX.Direct3D9.SwapEffect.Discard;
presentParams.DeviceWindowHandle = NativeMethods.GetDesktopWindow();
presentParams.PresentationInterval = SharpDX.Direct3D9.PresentInterval.Default;
return presentParams;
}
private IntPtr GetSharedHandle(SharpDX.Direct3D11.Texture2D texture)
{
using (var resource = texture.QueryInterface<SharpDX.DXGI.Resource>())
{
return resource.SharedHandle;
}
}
private static SharpDX.Direct3D9.Format TranslateFormat(SharpDX.Direct3D11.Texture2D texture)
{
switch (texture.Description.Format)
{
case SharpDX.DXGI.Format.R10G10B10A2_UNorm:
return SharpDX.Direct3D9.Format.A2B10G10R10;
case SharpDX.DXGI.Format.R16G16B16A16_Float:
return SharpDX.Direct3D9.Format.A16B16G16R16F;
case SharpDX.DXGI.Format.B8G8R8A8_UNorm:
return SharpDX.Direct3D9.Format.A8R8G8B8;
default:
return SharpDX.Direct3D9.Format.Unknown;
}
}
private static class NativeMethods
{
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDesktopWindow();
}
}
}边栏推荐
- FE01_OneHot-Scala Application
- CIFAR发布《AI伦理的文化:研讨会报告》【附下载】
- Matlab drawing 1
- 如何进行自动化测试?
- Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data
- DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)
- lc marathon 8.3
- EuROC dataset format and related codes
- 基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
- 动态数组底层是如何实现的
猜你喜欢

ECCV 2022 | FPN错位对齐,实现高效半监督目标检测(PseCo)

EasyCVR如何通过接口调用设备录像的倍速回放?

2019 Haidian District Youth Programming Challenge Activity Elementary Group Rematch Test Questions Detailed Answers

阿里云技术专家秦隆:云上如何进行混沌工程?

(ECCV-2022)GaitEdge:超越普通的端到端步态识别,提高实用性

CPU突然飙高系统反应慢,是怎么导致的?有什么办法排查?

DHCP&OSPF组合实验演示(Huawei路由交换设备配置)

The CPU suddenly soars and the system responds slowly, what is the cause?Is there any way to check?

Day018 Inheritance

八一建军节 | 致敬中国人民解放军
随机推荐
什么是网站监控,网站监控软件有什么用?
情绪的波动起伏
当前最快的实例分割模型:YOLACT 和 YOLACT++
c语言进阶篇:自定义类型--结构体
EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
[Distributed Advanced] Let's fill in those pits in Redis distributed locks.
July 31, 2022 Summary of the third week of summer vacation
Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel
After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
ATF中断处理的设计模型
入选爱分析·银行数字化厂商全景报告,网易数帆助力金融数字化场景落地
Day018 Inheritance
数据集成:holo数据同步至redis。redis必须是集群模式?
火灾报警联网FC18中CAN光端机常见问题解答和使用指导
数仓建模面试
CAN光纤转换器CAN光端机解决消防火灾报警
DHCP&OSPF组合实验演示(Huawei路由交换设备配置)
win10 uwp ping
老电脑怎么重装系统win10
数仓相关,总结