如何使用 SharpDX 将 Direct 2D BitmapRenderTarget 的位图绘制到 WicRenderTarget?

问题描述 投票:0回答:1

我正在尝试创建一个 BitmapRenderTarget,绘制到它,然后绘制创建到 WicRenderTarget 的位图,最终目标是将其保存为 png。问题是,当我尝试将 BitmapRenderTarget 的位图绘制到创建的 WicRenderTarget 时,代码失败,没有任何解释。我怎样才能实现这个目标?

Size2F size = new((float)(_deviceContext.Size.Width * Zoom), (float)(_deviceContext.Size.Height * Zoom));
BitmapRenderTarget bitmapRenderTarget = new(_deviceContext, CompatibleRenderTargetOptions.None, size)
{
    DotsPerInch = size,
    AntialiasMode = AntialiasMode.PerPrimitive
};
bitmapRenderTarget.BeginDraw();
bitmapRenderTarget.Clear(new RawColor4(1, 1, 1, 1));
bitmapRenderTarget.DrawLine(new RawVector2(0, 0), new RawVector2(100, 100), new SolidColorBrush(bitmapRenderTarget, new RawColor4(0, 0, 0, 1)), 1);
bitmapRenderTarget.EndDraw();

_imagingFactory = new();
_wicBitmap = new SharpDX.WIC.Bitmap(_imagingFactory, (int)(_deviceContext.Size.Width * Zoom), (int)(_deviceContext.Size.Height * Zoom), SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad);

_wicRenderTarget = new(bitmapRenderTarget.Factory, _wicBitmap, new RenderTargetProperties(bitmapRenderTarget.PixelFormat))
{
    DotsPerInch = size,
    AntialiasMode = AntialiasMode.PerPrimitive
};
_wicRenderTarget.BeginDraw();
_wicRenderTarget.DrawBitmap(bitmapRenderTarget.Bitmap, 1, BitmapInterpolationMode.Linear);

_wicRenderTarget.EndDraw();

Bitmap = Bitmap.FromWicBitmap(_deviceContext, _wicBitmap);
c# sharpdx
1个回答
0
投票

这是一个分步示例:

// Step 1: Create a WIC bitmap.
var imagingFactory = new ImagingFactory2();
var wicBitmap = new SharpDX.WIC.Bitmap(imagingFactory, (int)(_deviceContext.Size.Width * Zoom), (int)(_deviceContext.Size.Height * Zoom), SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad);

// Step 2: Create a WicRenderTarget using the WIC bitmap.
var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Premultiplied), 96, 96, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);
var wicRenderTarget = new WicRenderTarget(new Factory(), wicBitmap, renderTargetProperties);

// Step 3: Render to the WicRenderTarget.
wicRenderTarget.BeginDraw();
wicRenderTarget.Clear(new RawColor4(1, 1, 1, 1));
var brush = new SolidColorBrush(wicRenderTarget, new RawColor4(0, 0, 0, 1));
wicRenderTarget.DrawLine(new RawVector2(0, 0), new RawVector2(100, 100), brush, 1);
wicRenderTarget.EndDraw();

// Step 4: Save the WIC bitmap to a file.
var stream = new WICStream(imagingFactory, "output.png", SharpDX.IO.NativeFileAccess.Write);
var pngEncoder = new PngBitmapEncoder(imagingFactory);
pngEncoder.Initialize(stream);

var frameEncoder = new BitmapFrameEncode(pngEncoder);
frameEncoder.Initialize();
frameEncoder.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height);
frameEncoder.SetPixelFormat(ref SharpDX.WIC.PixelFormat.Format32bppPBGRA);
frameEncoder.WriteSource(wicBitmap);
frameEncoder.Commit();
pngEncoder.Commit();

stream.Dispose();
frameEncoder.Dispose();
pngEncoder.Dispose();
wicBitmap.Dispose();
imagingFactory.Dispose();
© www.soinside.com 2019 - 2024. All rights reserved.