//TODO: check this way to getting back texture //public BitmapSource ToBitmap() { // if (_d3D11Image == null) // return null; // // Copy back buffer to WriteableBitmap. // int width = _d3D11Image.PixelWidth; // int height = _d3D11Image.PixelHeight; // var format = EnableAlpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32; // var writeableBitmap = new WriteableBitmap(width, height, 96, 96, format, null); // writeableBitmap.Lock(); // try { // uint[] data = new uint[width * height]; // _d3D11Image.TryGetData(data); // // Get a pointer to the back buffer. // unsafe { // uint* pBackbuffer = (uint*)writeableBitmap.BackBuffer; // for (int i = 0; i < data.Length; i++) // pBackbuffer[i] = data[i]; // } // writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); // } finally { // writeableBitmap.Unlock(); // } // return writeableBitmap; //} static void Copy(Texture2D texture, Stream stream, SharpDX.Direct3D11.Device device) { var desc = 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 }; using (var factory = new SharpDX.WIC.ImagingFactory()) { using (var textureCopy = new Texture2D(device, desc)) { device.ImmediateContext.CopyResource(texture, textureCopy); var dataBox = device.ImmediateContext.MapSubresource( textureCopy, 0, 0, MapMode.Read, global::SharpDX.Direct3D11.MapFlags.None, out SharpDX.DataStream dataStream); using (dataStream) { var t = dataStream.ReadByte(); //ReadFloat(); var dataRectangle = new SharpDX.DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; //https://github.com/sharpdx/Toolkit/blob/master/Source/Toolkit/SharpDX.Toolkit.Graphics/WICHelper.cs using (var bitmap = new SharpDX.WIC.Bitmap(factory, textureCopy.Description.Width, textureCopy.Description.Height, SharpDX.WIC.PixelFormat.Format32bppRGBA, dataRectangle, 0)) { stream.Position = 0; using (var bitmapEncoder = new SharpDX.WIC.PngBitmapEncoder(factory, stream)) { using (var bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(bitmap); bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } } } device.ImmediateContext.UnmapSubresource(textureCopy, 0); } } } }
public override void Save(Stream stream) { using (var encoder = new PngBitmapEncoder(WicImagingFactory, stream)) using (var frameEncode = new SharpDX.WIC.BitmapFrameEncode(encoder)) using (var imageEncoder = new SharpDX.WIC.ImageEncoder((ImagingFactory2)WicImagingFactory, null)) { var parameters = new ImageParameters( new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), _direct2D.DotsPerInch.Width, _direct2D.DotsPerInch.Height, 0, 0, PixelWidth, PixelHeight); imageEncoder.WriteFrame(_direct2D, frameEncode, parameters); frameEncode.Commit(); encoder.Commit(); } }
public void Save(string filePath) { using (SharpDX.WIC.WICStream wICStream = new SharpDX.WIC.WICStream(imagingFactory, filePath, SharpDX.IO.NativeFileAccess.Write)) using (SharpDX.WIC.PngBitmapEncoder encoder = new SharpDX.WIC.PngBitmapEncoder(imagingFactory)) { encoder.Initialize(wICStream); using (SharpDX.WIC.BitmapFrameEncode bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(encoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize((int)this.Width, (int)this.Height); var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); } } }
/// <summary> /// Copies to a stream using WIC. The format is converted if necessary. /// </summary> /// <param name="context"></param> /// <param name="texture"></param> /// <param name="outputFormat"></param> /// <param name="stream"></param> public void ToStream(SharpDX.Direct3D11.DeviceContext context, Texture2D texture, ImageFormat outputFormat, Stream stream) { if (wicFactory == null) { wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2()); } DataStream dataStream; var dataBox = context.MapSubresource( texture, 0, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out dataStream); try { var dataRectangle = new DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; var format = PixelFormatFromFormat(texture.Description.Format); if (format == Guid.Empty) { return; } using (var bitmap = new SharpDX.WIC.Bitmap( wicFactory, texture.Description.Width, texture.Description.Height, format, dataRectangle)) { stream.Position = 0; SharpDX.WIC.BitmapEncoder bitmapEncoder = null; switch (outputFormat) { case ImageFormat.Bitmap: bitmapEncoder = new SharpDX.WIC.BmpBitmapEncoder(wicFactory, stream); break; case ImageFormat.Jpeg: bitmapEncoder = new SharpDX.WIC.JpegBitmapEncoder(wicFactory, stream); break; case ImageFormat.Png: bitmapEncoder = new SharpDX.WIC.PngBitmapEncoder(wicFactory, stream); break; default: return; } try { using (var bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = format; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); if (pixelFormat != format) { // IWICFormatConverter var converter = new SharpDX.WIC.FormatConverter(wicFactory); if (converter.CanConvert(format, pixelFormat)) { converter.Initialize(bitmap, SharpDX.WIC.PixelFormat.Format24bppBGR, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.MedianCut); bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(converter); } else { this.DebugMessage(string.Format("Unable to convert Direct3D texture format {0} to a suitable WIC format", texture.Description.Format.ToString())); return; } } else { bitmapFrameEncode.WriteSource(bitmap); } bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } } finally { bitmapEncoder.Dispose(); } } } finally { context.UnmapSubresource(texture, 0); } }
public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source) { var device = deviceManager.Direct3DDevice; var context = deviceManager.Direct3DContext; var txDesc = source.Description; txDesc.Usage = ResourceUsage.Staging; txDesc.CpuAccessFlags = CpuAccessFlags.Read; txDesc.BindFlags = BindFlags.None; txDesc.OptionFlags = ResourceOptionFlags.None; txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); Guid pixelFormat = PixelFormatFromFormat(txDesc.Format); if (pixelFormat == Guid.Empty) { return(null); } System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat"); using (var dest = new Texture2D(device, txDesc)) { if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0) { // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture // Initialize a target to resolve multi-sampled render target var resolvedDesc = source.Description; resolvedDesc.BindFlags = BindFlags.ShaderResource; resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); // if depth stencil needs to be typeless if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt) { resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless; } using (var resolvedTarget = new Texture2D(device, resolvedDesc)) { CopyToTexture(context, source, resolvedTarget); // Now we can copy to the destination CopyToTexture(context, source, dest); } } else { CopyToTexture(context, source, dest); } var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None); var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory); var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory); SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8; using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write)) { var destPtr = l.Data.DataPointer; var sourcePtr = sourceData.DataPointer; for (int y = 0; y < bm.Size.Height; y++) { SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel); sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch); destPtr = IntPtr.Add(destPtr, l.Data.Pitch); } } context.UnmapSubresource(dest, 0); return(bm); } }
public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source) { var device = deviceManager.Direct3DDevice; var context = deviceManager.Direct3DContext; var txDesc = source.Description; txDesc.Usage = ResourceUsage.Staging; txDesc.CpuAccessFlags = CpuAccessFlags.Read; txDesc.BindFlags = BindFlags.None; txDesc.OptionFlags = ResourceOptionFlags.None; txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); Guid pixelFormat = PixelFormatFromFormat(txDesc.Format); if (pixelFormat == Guid.Empty) { return null; } System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat"); using (var dest = new Texture2D(device, txDesc)) { if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0) { // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture // Initialize a target to resolve multi-sampled render target var resolvedDesc = source.Description; resolvedDesc.BindFlags = BindFlags.ShaderResource; resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); // if depth stencil needs to be typeless if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt) resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless; using (var resolvedTarget = new Texture2D(device, resolvedDesc)) { CopyToTexture(context, source, resolvedTarget); // Now we can copy to the destination CopyToTexture(context, source, dest); } } else CopyToTexture(context, source, dest); var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None); var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory); var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory); SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8; using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write)) { var destPtr = l.Data.DataPointer; var sourcePtr = sourceData.DataPointer; for (int y = 0; y < bm.Size.Height; y++) { SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel); sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch); destPtr = IntPtr.Add(destPtr, l.Data.Pitch); } } context.UnmapSubresource(dest, 0); return bm; } }