/// <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(); } }
public byte[] extractRawBitmap() { // use a cpu bound resource var textureDesc = new Texture2DDescription { MipLevels = 1, ArraySize = 1, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, Format = Format.B8G8R8A8_UNorm, OptionFlags = ResourceOptionFlags.None, Width = _width, Height = _height, Usage = ResourceUsage.Default, SampleDescription = new SampleDescription(1, 0) }; using (var cpuTexture = new Texture2D(_device, textureDesc)) { _device.CopyResource(_texture, cpuTexture); var res = new byte[4 * _width * _height]; var data = cpuTexture.Map(0, MapMode.Read, MapFlags.None); try { IntPtr sourcePtr = data.DataPointer; int targetOffset = 0; for (int i = 0; i != _height; ++i) { Marshal.Copy(sourcePtr, res, targetOffset, _width); sourcePtr += data.Pitch; targetOffset += _width; } return res; } finally { cpuTexture.Unmap(0); } } }
public byte[] ExtractRawBitmap() { // use a cpu bound resource var textureDesc = new Texture2DDescription { MipLevels = 1, ArraySize = 1, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, Format = Format.B8G8R8A8_UNorm, OptionFlags = ResourceOptionFlags.None, Width = _width, Height = _height, Usage = ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0) }; using (var cpuTexture = new Texture2D(_device, textureDesc)) { #if NETFX_CORE _device.ImmediateContext.CopyResource(_texture, cpuTexture); #else _device.CopyResource(_texture, cpuTexture); #endif var bytesPerLine = _width * 4; var res = new byte[bytesPerLine * _height]; #if NETFX_CORE var data = _device.ImmediateContext.MapSubresource(cpuTexture, 0, MapMode.Read, MapFlags.None); #else var data = cpuTexture.Map(0, MapMode.Read, MapFlags.None); #endif try { IntPtr sourcePtr = data.DataPointer; int targetOffset = 0; for (int i = 0; i != _height; ++i) { Marshal.Copy(sourcePtr, res, targetOffset, bytesPerLine); #if NETFX_CORE sourcePtr += data.RowPitch; #else sourcePtr += data.Pitch; #endif targetOffset += bytesPerLine; } return res; } finally { #if NETFX_CORE _device.ImmediateContext.UnmapSubresource(cpuTexture, 0); #else cpuTexture.Unmap(0); #endif } } }