C#WinForm-SharpDX.Toolkit.Graphics绘制二维纹理

C#WinForm-SharpDX.Toolkit.Graphics绘制二维纹理,c#,winforms,sharpdx,slimdx,C#,Winforms,Sharpdx,Slimdx,我想为C#WinForms应用程序实现硬件加速。原因是我必须绘制150 x 720p的图像,而5个picturebox控件花费的时间太长(缩放+图像绘制),因此存在处理和重新加载问题。 所以我处理了ShapeDX 但是现在我被卡住了,不知道如何绘制2D纹理。为了测试代码,我只有一个测试按钮和一个图片盒 当我在PictureBox中运行代码时,也会加载DirectX(Draw或3D)。我承认黑色背景但我不明白纹理必须如何绘制。 String imageFile = "Image.J

我想为C#WinForms应用程序实现硬件加速。原因是我必须绘制150 x 720p的图像,而5个picturebox控件花费的时间太长(缩放+图像绘制),因此存在处理和重新加载问题。 所以我处理了ShapeDX

但是现在我被卡住了,不知道如何绘制2D纹理。为了测试代码,我只有一个测试按钮和一个图片盒

当我在PictureBox中运行代码时,也会加载DirectX(Draw或3D)。我承认黑色背景但我不明白纹理必须如何绘制。

        String imageFile = "Image.JPG";

        Control TargetControl = this.pictureBoxCurrentFrameL;
        int TotalWidth = TargetControl.Width;
        int TotalHeight = TargetControl.Height;

        SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.Debug);

        SharpDX.Toolkit.Graphics.GraphicsDevice graphicsDevice = SharpDX.Toolkit.Graphics.GraphicsDevice.New(defaultDevice);
        SharpDX.Toolkit.Graphics.PresentationParameters presentationParameters = new SharpDX.Toolkit.Graphics.PresentationParameters();
        presentationParameters.DeviceWindowHandle = this.pictureBoxCurrentFrameL.Handle;
        presentationParameters.BackBufferWidth = TotalWidth;
        presentationParameters.BackBufferHeight = TotalHeight;

        SharpDX.Toolkit.Graphics.SwapChainGraphicsPresenter swapChainGraphicsPresenter = new SharpDX.Toolkit.Graphics.SwapChainGraphicsPresenter(graphicsDevice, presentationParameters);
        SharpDX.Toolkit.Graphics.Texture2D texture2D = SharpDX.Toolkit.Graphics.Texture2D.Load(graphicsDevice, imageFile);

        //Now i should draw. But how?

        swapChainGraphicsPresenter.Present();/**/
在Windows 10上使用Microsoft Visual Studio Community 2015(.Net 4,C#WinForm)创建SharpDX-SDK-2.6.3


感谢您的帮助。

我只需切换到SlimDX(SlimDX运行时.NET 4.0 x64 2012年1月.msi、.Net4、Win10、MS Visual Studio Community 2015、Winforms App.)即可解决问题。有几个有用的教程

要使用SlimDX,只需将唯一一个DLL链接到您的项目!安装SlimDX后,您将在您的C驱动器的某处找到此SlimDX.dll文件

必须了解Direct2D至少需要一个工厂和一个渲染目标。RenderTarget指向要使用的对象(Control/form/etc)并接管图形

不需要交换链。可能由渲染目标在内部使用。最大的部分是将位图转换为有用的Direct2D位图(用于绘图)。否则,您也可以从MemoryStream处理位图数据

对于那些正在寻找解决方案的人:

        Control targetControl = this.pictureBoxCurrentFrameL;
        String imageFile = "Image.JPG";

        //Update control styles, works for forms, not for controls. I solve this later otherwise .
        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        //this.SetStyle(ControlStyles.Opaque, true);
        //this.SetStyle(ControlStyles.ResizeRedraw, true);

        //Get requested debug level
        SlimDX.Direct2D.DebugLevel debugLevel = SlimDX.Direct2D.DebugLevel.None;

        //Resources for Direct2D rendering
        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, debugLevel);

        //Create the render target
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() {
            Handle = targetControl.Handle,
            PixelSize = targetControl.Size,
            PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately
        });

        //Paint!
        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));

        //Convert System.Drawing.Bitmap into SlimDX.Direct2D.Bitmap!
        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Properties.Resources.Image_720p;//loaded from embedded resource, can be changed to Bitmap.FromFile(imageFile); to load from hdd!
        SlimDX.Direct2D.Bitmap d2dBitmap = null;
        System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);//TODO: PixelFormat is very important!!! Check!
        SlimDX.DataStream dataStream = new SlimDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
        SlimDX.Direct2D.PixelFormat d2dPixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, SlimDX.Direct2D.AlphaMode.Premultiplied);
        SlimDX.Direct2D.BitmapProperties d2dBitmapProperties = new SlimDX.Direct2D.BitmapProperties();
        d2dBitmapProperties.PixelFormat = d2dPixelFormat;
        d2dBitmap = new SlimDX.Direct2D.Bitmap(d2dWindowRenderTarget, new Size(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, d2dBitmapProperties);
        bitmap.UnlockBits(bitmapData);

        //Draw SlimDX.Direct2D.Bitmap
        d2dWindowRenderTarget.DrawBitmap(d2dBitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));/**/

        d2dWindowRenderTarget.EndDraw();

        //Dispose everything u dont need anymore.
        //bitmap.Dispose();//......
因此,使用Direct2D非常简单,所有代码都可以压缩为2条主线+绘图:

        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, SlimDX.Direct2D.DebugLevel.None);
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() { Handle = targetControl.Handle, PixelSize = targetControl.Size, PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately });

        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));
        d2dWindowRenderTarget.DrawRectangle(new SlimDX.Direct2D.SolidColorBrush(d2dWindowRenderTarget, new SlimDX.Color4(Color.Red)), new Rectangle(20,20, targetControl.Width-40, targetControl.Height-40));
        d2dWindowRenderTarget.EndDraw();

我通过简单地切换到SlimDX(SlimDX Runtime.NET 4.0 x64 2012年1月.msi、.Net4、Win10、MS Visual Studio Community 2015、Winforms App.)解决了这个问题。有几个有用的教程

要使用SlimDX,只需将唯一一个DLL链接到您的项目!安装SlimDX后,您将在您的C驱动器的某处找到此SlimDX.dll文件

必须了解Direct2D至少需要一个工厂和一个渲染目标。RenderTarget指向要使用的对象(Control/form/etc)并接管图形

不需要交换链。可能由渲染目标在内部使用。最大的部分是将位图转换为有用的Direct2D位图(用于绘图)。否则,您也可以从MemoryStream处理位图数据

对于那些正在寻找解决方案的人:

        Control targetControl = this.pictureBoxCurrentFrameL;
        String imageFile = "Image.JPG";

        //Update control styles, works for forms, not for controls. I solve this later otherwise .
        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        //this.SetStyle(ControlStyles.Opaque, true);
        //this.SetStyle(ControlStyles.ResizeRedraw, true);

        //Get requested debug level
        SlimDX.Direct2D.DebugLevel debugLevel = SlimDX.Direct2D.DebugLevel.None;

        //Resources for Direct2D rendering
        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, debugLevel);

        //Create the render target
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() {
            Handle = targetControl.Handle,
            PixelSize = targetControl.Size,
            PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately
        });

        //Paint!
        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));

        //Convert System.Drawing.Bitmap into SlimDX.Direct2D.Bitmap!
        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Properties.Resources.Image_720p;//loaded from embedded resource, can be changed to Bitmap.FromFile(imageFile); to load from hdd!
        SlimDX.Direct2D.Bitmap d2dBitmap = null;
        System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);//TODO: PixelFormat is very important!!! Check!
        SlimDX.DataStream dataStream = new SlimDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
        SlimDX.Direct2D.PixelFormat d2dPixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, SlimDX.Direct2D.AlphaMode.Premultiplied);
        SlimDX.Direct2D.BitmapProperties d2dBitmapProperties = new SlimDX.Direct2D.BitmapProperties();
        d2dBitmapProperties.PixelFormat = d2dPixelFormat;
        d2dBitmap = new SlimDX.Direct2D.Bitmap(d2dWindowRenderTarget, new Size(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, d2dBitmapProperties);
        bitmap.UnlockBits(bitmapData);

        //Draw SlimDX.Direct2D.Bitmap
        d2dWindowRenderTarget.DrawBitmap(d2dBitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));/**/

        d2dWindowRenderTarget.EndDraw();

        //Dispose everything u dont need anymore.
        //bitmap.Dispose();//......
因此,使用Direct2D非常简单,所有代码都可以压缩为2条主线+绘图:

        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, SlimDX.Direct2D.DebugLevel.None);
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() { Handle = targetControl.Handle, PixelSize = targetControl.Size, PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately });

        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));
        d2dWindowRenderTarget.DrawRectangle(new SlimDX.Direct2D.SolidColorBrush(d2dWindowRenderTarget, new SlimDX.Color4(Color.Red)), new Rectangle(20,20, targetControl.Width-40, targetControl.Height-40));
        d2dWindowRenderTarget.EndDraw();

我通过简单地切换到SlimDX(SlimDX Runtime.NET 4.0 x64 2012年1月.msi、.Net4、Win10、MS Visual Studio Community 2015、Winforms App.)解决了这个问题。有几个有用的教程

要使用SlimDX,只需将唯一一个DLL链接到您的项目!安装SlimDX后,您将在您的C驱动器的某处找到此SlimDX.dll文件

必须了解Direct2D至少需要一个工厂和一个渲染目标。RenderTarget指向要使用的对象(Control/form/etc)并接管图形

不需要交换链。可能由渲染目标在内部使用。最大的部分是将位图转换为有用的Direct2D位图(用于绘图)。否则,您也可以从MemoryStream处理位图数据

对于那些正在寻找解决方案的人:

        Control targetControl = this.pictureBoxCurrentFrameL;
        String imageFile = "Image.JPG";

        //Update control styles, works for forms, not for controls. I solve this later otherwise .
        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        //this.SetStyle(ControlStyles.Opaque, true);
        //this.SetStyle(ControlStyles.ResizeRedraw, true);

        //Get requested debug level
        SlimDX.Direct2D.DebugLevel debugLevel = SlimDX.Direct2D.DebugLevel.None;

        //Resources for Direct2D rendering
        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, debugLevel);

        //Create the render target
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() {
            Handle = targetControl.Handle,
            PixelSize = targetControl.Size,
            PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately
        });

        //Paint!
        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));

        //Convert System.Drawing.Bitmap into SlimDX.Direct2D.Bitmap!
        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Properties.Resources.Image_720p;//loaded from embedded resource, can be changed to Bitmap.FromFile(imageFile); to load from hdd!
        SlimDX.Direct2D.Bitmap d2dBitmap = null;
        System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);//TODO: PixelFormat is very important!!! Check!
        SlimDX.DataStream dataStream = new SlimDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
        SlimDX.Direct2D.PixelFormat d2dPixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, SlimDX.Direct2D.AlphaMode.Premultiplied);
        SlimDX.Direct2D.BitmapProperties d2dBitmapProperties = new SlimDX.Direct2D.BitmapProperties();
        d2dBitmapProperties.PixelFormat = d2dPixelFormat;
        d2dBitmap = new SlimDX.Direct2D.Bitmap(d2dWindowRenderTarget, new Size(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, d2dBitmapProperties);
        bitmap.UnlockBits(bitmapData);

        //Draw SlimDX.Direct2D.Bitmap
        d2dWindowRenderTarget.DrawBitmap(d2dBitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));/**/

        d2dWindowRenderTarget.EndDraw();

        //Dispose everything u dont need anymore.
        //bitmap.Dispose();//......
因此,使用Direct2D非常简单,所有代码都可以压缩为2条主线+绘图:

        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, SlimDX.Direct2D.DebugLevel.None);
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() { Handle = targetControl.Handle, PixelSize = targetControl.Size, PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately });

        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));
        d2dWindowRenderTarget.DrawRectangle(new SlimDX.Direct2D.SolidColorBrush(d2dWindowRenderTarget, new SlimDX.Color4(Color.Red)), new Rectangle(20,20, targetControl.Width-40, targetControl.Height-40));
        d2dWindowRenderTarget.EndDraw();

我通过简单地切换到SlimDX(SlimDX Runtime.NET 4.0 x64 2012年1月.msi、.Net4、Win10、MS Visual Studio Community 2015、Winforms App.)解决了这个问题。有几个有用的教程

要使用SlimDX,只需将唯一一个DLL链接到您的项目!安装SlimDX后,您将在您的C驱动器的某处找到此SlimDX.dll文件

必须了解Direct2D至少需要一个工厂和一个渲染目标。RenderTarget指向要使用的对象(Control/form/etc)并接管图形

不需要交换链。可能由渲染目标在内部使用。最大的部分是将位图转换为有用的Direct2D位图(用于绘图)。否则,您也可以从MemoryStream处理位图数据

对于那些正在寻找解决方案的人:

        Control targetControl = this.pictureBoxCurrentFrameL;
        String imageFile = "Image.JPG";

        //Update control styles, works for forms, not for controls. I solve this later otherwise .
        //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        //this.SetStyle(ControlStyles.Opaque, true);
        //this.SetStyle(ControlStyles.ResizeRedraw, true);

        //Get requested debug level
        SlimDX.Direct2D.DebugLevel debugLevel = SlimDX.Direct2D.DebugLevel.None;

        //Resources for Direct2D rendering
        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, debugLevel);

        //Create the render target
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() {
            Handle = targetControl.Handle,
            PixelSize = targetControl.Size,
            PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately
        });

        //Paint!
        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));

        //Convert System.Drawing.Bitmap into SlimDX.Direct2D.Bitmap!
        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Properties.Resources.Image_720p;//loaded from embedded resource, can be changed to Bitmap.FromFile(imageFile); to load from hdd!
        SlimDX.Direct2D.Bitmap d2dBitmap = null;
        System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);//TODO: PixelFormat is very important!!! Check!
        SlimDX.DataStream dataStream = new SlimDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
        SlimDX.Direct2D.PixelFormat d2dPixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, SlimDX.Direct2D.AlphaMode.Premultiplied);
        SlimDX.Direct2D.BitmapProperties d2dBitmapProperties = new SlimDX.Direct2D.BitmapProperties();
        d2dBitmapProperties.PixelFormat = d2dPixelFormat;
        d2dBitmap = new SlimDX.Direct2D.Bitmap(d2dWindowRenderTarget, new Size(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, d2dBitmapProperties);
        bitmap.UnlockBits(bitmapData);

        //Draw SlimDX.Direct2D.Bitmap
        d2dWindowRenderTarget.DrawBitmap(d2dBitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));/**/

        d2dWindowRenderTarget.EndDraw();

        //Dispose everything u dont need anymore.
        //bitmap.Dispose();//......
因此,使用Direct2D非常简单,所有代码都可以压缩为2条主线+绘图:

        SlimDX.Direct2D.Factory d2dFactory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.Multithreaded, SlimDX.Direct2D.DebugLevel.None);
        SlimDX.Direct2D.WindowRenderTarget d2dWindowRenderTarget = new SlimDX.Direct2D.WindowRenderTarget(d2dFactory, new SlimDX.Direct2D.WindowRenderTargetProperties() { Handle = targetControl.Handle, PixelSize = targetControl.Size, PresentOptions = SlimDX.Direct2D.PresentOptions.Immediately });

        d2dWindowRenderTarget.BeginDraw();
        d2dWindowRenderTarget.Clear(new SlimDX.Color4(Color.LightSteelBlue));
        d2dWindowRenderTarget.DrawRectangle(new SlimDX.Direct2D.SolidColorBrush(d2dWindowRenderTarget, new SlimDX.Color4(Color.Red)), new Rectangle(20,20, targetControl.Width-40, targetControl.Height-40));
        d2dWindowRenderTarget.EndDraw();


您需要多久更新一次UI?每秒多少次?你可能只需要使用GDI,我每秒可以获得150x720p的帧数。是5倍不同的震源(30 fps)。2x 720p视频(30 fps)2x 720p视频帧(具有已识别的模式)1x 720p视频帧(具有最终结果模式识别)分为多个线程。最终计算也在单独的螺纹上执行。所有图像都是从不同的线程生成的,并且是异步的。显然,性能与控制类似。我认为使用GDI比使用控件更好。因此,您可以控制绘图(例如放置框架、处置等),但我更喜欢使用直接绘图。有人能帮我解决直接绘图问题吗?有人知道吗?如果你只画2D的东西,你应该使用Direct2D。我很确定SharpDX已经包装好了。DirectDraw已被标记为不推荐使用大约十年了。你使用它的方式与我过去的做法不同。而且它的实现并不简单,所以我不知道在这里包含所有代码是否合适。我看看能不能把东西拼起来,但可能需要一段时间。应该有一个带有SharpDX的Direct2D示例。尝试一下,然后让我知道你是否需要帮助。你需要多久更新一次你的用户界面?每秒多少次?你可能只想使用