【问题标题】:Load and draw png with Direct2D使用 Direct2D 加载和绘制 png
【发布时间】:2017-05-30 15:31:30
【问题描述】:

我开始在 c# 中使用 Direct2D。

我已经下载并使用了this example app

该应用程序运行良好,但我还需要它从硬盘加载 png 图像并显示它们。

在示例中,这将在函数OnRender 中完成,可能使用RenderTarget.DrawBitmap

但是,我在将 png 实际加载到内存时遇到了问题。我知道我应该使用类似WICImagingFactory 的东西,但我找不到类,我不知道如何使用它来获取RenderTarget.DrawBitmap 所需的D2DBitmap 对象

谁能提供一个 c# 示例如何将图像加载到 Direct2D 中?

【问题讨论】:

  • 你看过使用SharpDXWin2D吗?
  • 是的,刚玩完 SharpDX

标签: c# directx direct2d wic


【解决方案1】:

我最终使用了 SharpDX。

这是一个很好的例子,如何使用 SharpDX 加载图像:https://english.r2d2rigo.es/2014/08/12/loading-and-drawing-bitmaps-with-direct2d-using-sharpdx/

ImagingFactory imagingFactory = new ImagingFactory();
NativeFileStream fileStream = new NativeFileStream(@"D:\path\myfile.png",
    NativeFileMode.Open, NativeFileAccess.Read);

BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);
BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

FormatConverter converter = new FormatConverter(imagingFactory);
converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

newBitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(target, converter); 

target.DrawBitmap(newBitmap,new RawRectangleF(0,0,target.Size.Width,target.Size.Height),1,BitmapInterpolationMode.Linear );

另外,如果你想用 SharpDX 来完成整个程序(这样你最终不会使用 2 个不同的半不兼容框架),这里有一个工作示例如何做到这一点:https://github.com/dalance/D2dControl

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2020-08-10
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多