本文整理汇总了C#中Bitmap.?.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.?.Dispose方法的具体用法?C# Bitmap.?.Dispose怎么用?C# Bitmap.?.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.?.Dispose方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveToStream
/// <summary>
/// Saves a texture to a stream as an image.
/// </summary>
/// <param name="texture">The texture to save.</param>
/// <param name="imageFormat">The image format of the saved image.</param>
/// <param name="imageResolutionInDpi">The image resolution in dpi.</param>
/// <param name="toStream">The stream to save the texture to.</param>
public static void SaveToStream(this Texture2D texture, System.Drawing.Imaging.ImageFormat imageFormat, double imageResolutionInDpi, System.IO.Stream toStream)
{
Texture2D textureCopy = null;
ImagingFactory imagingFactory = null;
Bitmap bitmap = null;
BitmapEncoder bitmapEncoder = null;
try
{
textureCopy = new Texture2D(texture.Device, new Texture2DDescription
{
Width = (int)texture.Description.Width,
Height = (int)texture.Description.Height,
MipLevels = 1,
ArraySize = 1,
Format = texture.Description.Format,
Usage = ResourceUsage.Staging,
SampleDescription = new SampleDescription(1, 0),
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Read,
OptionFlags = ResourceOptionFlags.None
});
texture.Device.CopyResource(texture, textureCopy);
DataRectangle dataRectangle = textureCopy.Map(0, MapMode.Read, SharpDX.Direct3D10.MapFlags.None);
imagingFactory = new ImagingFactory();
bitmap = new Bitmap(
imagingFactory,
textureCopy.Description.Width,
textureCopy.Description.Height,
PixelFormat.Format32bppBGRA,
dataRectangle);
toStream.Position = 0;
if (imageFormat == System.Drawing.Imaging.ImageFormat.Png)
bitmapEncoder = new PngBitmapEncoder(imagingFactory, toStream);
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Bmp)
bitmapEncoder = new BmpBitmapEncoder(imagingFactory, toStream);
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Gif)
bitmapEncoder = new GifBitmapEncoder(imagingFactory, toStream);
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Jpeg)
bitmapEncoder = new JpegBitmapEncoder(imagingFactory, toStream);
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Tiff)
bitmapEncoder = new TiffBitmapEncoder(imagingFactory, toStream);
else
bitmapEncoder = new PngBitmapEncoder(imagingFactory, toStream);
using (var bitmapFrameEncode = new BitmapFrameEncode(bitmapEncoder))
{
bitmapFrameEncode.Initialize();
bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height);
var pixelFormat = PixelFormat.FormatDontCare;
bitmapFrameEncode.SetPixelFormat(ref pixelFormat);
bitmapFrameEncode.SetResolution(imageResolutionInDpi, imageResolutionInDpi);
bitmapFrameEncode.WriteSource(bitmap);
bitmapFrameEncode.Commit();
bitmapEncoder.Commit();
}
}
finally
{
bitmapEncoder?.Dispose();
textureCopy?.Unmap(0);
textureCopy?.Dispose();
bitmap?.Dispose();
imagingFactory?.Dispose();
}
}