gpt4 book ai didi

directx - 在 SharpDx 中保存为 png

转载 作者:行者123 更新时间:2023-12-02 17:28:32 25 4
gpt4 key购买 nike

我在 Direct2D 下使用 SharpDx 开发 Metro 应用程序。我使用 deviceContext 画了一些图片。如何将图片保存为 png 文件?

最佳答案

解决方案:

  public class Direct2DImageEncoder
{
private readonly Direct2DFactoryManager factoryManager;
private readonly Bitmap wicBitmap;
private readonly WicRenderTarget renderTarget;

private readonly int imageWidth, imageHeight;

public Direct2DImageEncoder(int imageWidth, int imageHeight, int imageDpi)
{
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;

factoryManager = new Direct2DFactoryManager();

wicBitmap = new Bitmap(factoryManager.WicFactory, imageWidth, imageHeight, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);
var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), imageDpi, imageDpi, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);
renderTarget = new WicRenderTarget(factoryManager.D2DFactory, wicBitmap, renderTargetProperties);
renderTarget.BeginDraw();
renderTarget.Clear(Colors.White);
}

public void Save(Stream systemStream, Direct2DImageFormat format)
{
renderTarget.EndDraw();

var stream = new WICStream(factoryManager.WicFactory, systemStream);
var encoder = new BitmapEncoder(factoryManager.WicFactory, Direct2DConverter.ConvertImageFormat(format));
encoder.Initialize(stream);

var bitmapFrameEncode = new BitmapFrameEncode(encoder);
bitmapFrameEncode.Initialize();
bitmapFrameEncode.SetSize(imageWidth, imageHeight);
bitmapFrameEncode.PixelFormat = SharpDX.WIC.PixelFormat.FormatDontCare;
bitmapFrameEncode.WriteSource(wicBitmap);

bitmapFrameEncode.Commit();
encoder.Commit();

bitmapFrameEncode.Dispose();
encoder.Dispose();
stream.Dispose();
}
}

public class Direct2DFactoryManager
{
private readonly SharpDX.WIC.ImagingFactory wicFactory;
private readonly SharpDX.Direct2D1.Factory d2DFactory;
private readonly SharpDX.DirectWrite.Factory dwFactory;

public Direct2DFactoryManager()
{
wicFactory = new SharpDX.WIC.ImagingFactory();
d2DFactory = new SharpDX.Direct2D1.Factory();
dwFactory = new SharpDX.DirectWrite.Factory();
}

public SharpDX.WIC.ImagingFactory WicFactory
{
get
{
return wicFactory;
}
}

public SharpDX.Direct2D1.Factory D2DFactory
{
get
{
return d2DFactory;
}
}

public SharpDX.DirectWrite.Factory DwFactory
{
get
{
return dwFactory;
}
}
}

public enum Direct2DImageFormat
{
Png, Gif, Ico, Jpeg, Wmp, Tiff, Bmp
}

public class Direct2DConverter
{
public static Guid ConvertImageFormat(Direct2DImageFormat format)
{
switch (format)
{
case Direct2DImageFormat.Bmp:
return ContainerFormatGuids.Bmp;
case Direct2DImageFormat.Ico:
return ContainerFormatGuids.Ico;
case Direct2DImageFormat.Gif:
return ContainerFormatGuids.Gif;
case Direct2DImageFormat.Jpeg:
return ContainerFormatGuids.Jpeg;
case Direct2DImageFormat.Png:
return ContainerFormatGuids.Png;
case Direct2DImageFormat.Tiff:
return ContainerFormatGuids.Tiff;
case Direct2DImageFormat.Wmp:
return ContainerFormatGuids.Wmp;
}
throw new NotSupportedException();
}

#endregion
}

您应该使用renderTarget的方法进行绘制

关于directx - 在 SharpDx 中保存为 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11859252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com