public static IWICBitmapEncoder CreateEncoder(this IWICImagingFactory factory, Guid guidContainerFormat, Guid?pguidVendor = null) { using (var pguidVendorPtr = CoTaskMemPtr.From(pguidVendor)) { return(factory.CreateEncoder(guidContainerFormat, pguidVendorPtr)); } }
public static void Save(this IWICImagingFactory factory, IWICBitmapSource imageSource, IWICStream stream, Guid encoderId) { if (factory == null) { throw new ArgumentNullException("factory"); } if (imageSource == null) { throw new ArgumentNullException("imageSource"); } if (stream == null) { throw new ArgumentNullException("stream"); } IWICBitmapEncoder iwicbitmapEncoder = factory.CreateEncoder(ref encoderId, null); iwicbitmapEncoder.Initialize(stream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache); IWICBitmapFrameEncode iwicbitmapFrameEncode; iwicbitmapEncoder.CreateNewFrame(out iwicbitmapFrameEncode, null); iwicbitmapFrameEncode.Initialize(null); iwicbitmapFrameEncode.WriteSource(imageSource, null); iwicbitmapFrameEncode.Commit(); iwicbitmapEncoder.Commit(); GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapEncoder); GraphicsInteropNativeMethods.SafeReleaseComObject(iwicbitmapFrameEncode); }
public static void Main() { var wicFactory = new IWICImagingFactory(); D2D1.D2D1CreateFactory(FactoryType.SingleThreaded, out ID2D1Factory d2dFactory); const string fileName = "output.jpg"; const int width = 512; const int height = 512; var rectangleGeometry = d2dFactory.CreateRoundedRectangleGeometry(new RoundedRectangle(new RectangleF(128, 128, width - 128 * 2, height - 128 * 2), 32, 32)); var wicBitmap = wicFactory.CreateBitmap(width, height, Vortice.DirectX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties(new Vortice.DirectX.Direct2D.PixelFormat(Format.Unknown, Vortice.DirectX.Direct2D.AlphaMode.Unknown)); var d2dRenderTarget = d2dFactory.CreateWicBitmapRenderTarget(wicBitmap, renderTargetProperties); var solidColorBrush = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f)); d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(new Color4(0.0f, 0.0f, 0.0f, 1.0f)); d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); d2dRenderTarget.EndDraw(); if (File.Exists(fileName)) { File.Delete(fileName); } using (var stream = wicFactory.CreateStream()) { stream.Initialize(fileName, FileAccess.Write); // Initialize a Jpeg encoder with this stream using (var encoder = wicFactory.CreateEncoder(ContainerFormat.Jpeg)) { encoder.Initialize(stream); // Create a Frame encoder var props = new SharpGen.Runtime.Win32.PropertyBag(IntPtr.Zero); var bitmapFrameEncode = encoder.CreateNewFrame(props); bitmapFrameEncode.Initialize(null); bitmapFrameEncode.SetSize(width, height); var pixelFormatGuid = Vortice.DirectX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap, null); bitmapFrameEncode.Commit(); encoder.Commit(); } } using (var app = new TestApplication()) { app.Run(); } }
public static IComObject <IWICBitmapEncoder> CreateEncoder(this IWICImagingFactory factory, Guid containerFormat) { if (factory == null) { throw new ArgumentNullException(nameof(factory)); } factory.CreateEncoder(containerFormat, IntPtr.Zero, out var value).ThrowOnError(); return(new ComObject <IWICBitmapEncoder>(value)); }
public static void SaveD2DBitmap(IWICImagingFactory wic, IWICBitmap wicBitmap, Stream outputStream) { using (IWICBitmapEncoder encoder = wic.CreateEncoder(ContainerFormat.Png)) { encoder.Initialize(outputStream); using (IWICBitmapFrameEncode frame = encoder.CreateNewFrame(out IPropertyBag2 props)) { frame.Initialize(); frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height); var pixelFormat = wicBitmap.PixelFormat; frame.SetPixelFormat(ref pixelFormat); frame.WriteSource(wicBitmap); frame.Commit(); encoder.Commit(); } } }
private unsafe void SaveScreenshot(string path, ContainerFormat format = ContainerFormat.Jpeg) { var d3d11GraphicsDevice = ((D3D11GraphicsDevice)_graphicsDevice !); ID3D11Texture2D source = Headless ? d3d11GraphicsDevice !.OffscreenTexture : d3d11GraphicsDevice !.BackBufferTexture; using (ID3D11Texture2D staging = d3d11GraphicsDevice !.CaptureTexture(source)) { staging.DebugName = "STAGING"; var textureDesc = staging !.Description; // Determine source format's WIC equivalent Guid pfGuid = default; bool sRGB = false; switch (textureDesc.Format) { case Vortice.DXGI.Format.R32G32B32A32_Float: pfGuid = WICPixelFormat.Format128bppRGBAFloat; break; //case DXGI_FORMAT_R16G16B16A16_FLOAT: pfGuid = GUID_WICPixelFormat64bppRGBAHalf; break; //case DXGI_FORMAT_R16G16B16A16_UNORM: pfGuid = GUID_WICPixelFormat64bppRGBA; break; //case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM: pfGuid = GUID_WICPixelFormat32bppRGBA1010102XR; break; // DXGI 1.1 //case DXGI_FORMAT_R10G10B10A2_UNORM: pfGuid = GUID_WICPixelFormat32bppRGBA1010102; break; //case DXGI_FORMAT_B5G5R5A1_UNORM: pfGuid = GUID_WICPixelFormat16bppBGRA5551; break; //case DXGI_FORMAT_B5G6R5_UNORM: pfGuid = GUID_WICPixelFormat16bppBGR565; break; //case DXGI_FORMAT_R32_FLOAT: pfGuid = GUID_WICPixelFormat32bppGrayFloat; break; //case DXGI_FORMAT_R16_FLOAT: pfGuid = GUID_WICPixelFormat16bppGrayHalf; break; //case DXGI_FORMAT_R16_UNORM: pfGuid = GUID_WICPixelFormat16bppGray; break; //case DXGI_FORMAT_R8_UNORM: pfGuid = GUID_WICPixelFormat8bppGray; break; //case DXGI_FORMAT_A8_UNORM: pfGuid = GUID_WICPixelFormat8bppAlpha; break; case Vortice.DXGI.Format.R8G8B8A8_UNorm: pfGuid = WICPixelFormat.Format32bppRGBA; break; case Vortice.DXGI.Format.R8G8B8A8_UNorm_SRgb: pfGuid = WICPixelFormat.Format32bppRGBA; sRGB = true; break; case Vortice.DXGI.Format.B8G8R8A8_UNorm: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGRA; break; case Vortice.DXGI.Format.B8G8R8A8_UNorm_SRgb: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGRA; sRGB = true; break; case Vortice.DXGI.Format.B8G8R8X8_UNorm: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGR; break; case Vortice.DXGI.Format.B8G8R8X8_UNorm_SRgb: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGR; sRGB = true; break; default: //Console.WriteLine("ERROR: ScreenGrab does not support all DXGI formats (%u). Consider using DirectXTex.\n", static_cast<uint32_t>(desc.Format)); return; } // Screenshots don't typically include the alpha channel of the render target Guid targetGuid = default; switch (textureDesc.Format) { case Vortice.DXGI.Format.R32G32B32A32_Float: case Vortice.DXGI.Format.R16G16B16A16_Float: //if (_IsWIC2()) { targetGuid = WICPixelFormat.Format96bppRGBFloat; } //else //{ // targetGuid = WICPixelFormat.Format24bppBGR; //} break; case Vortice.DXGI.Format.R16G16B16A16_UNorm: targetGuid = WICPixelFormat.Format48bppBGR; break; case Vortice.DXGI.Format.B5G5R5A1_UNorm: targetGuid = WICPixelFormat.Format16bppBGR555; break; case Vortice.DXGI.Format.B5G6R5_UNorm: targetGuid = WICPixelFormat.Format16bppBGR565; break; case Vortice.DXGI.Format.R32_Float: case Vortice.DXGI.Format.R16_Float: case Vortice.DXGI.Format.R16_UNorm: case Vortice.DXGI.Format.R8_UNorm: case Vortice.DXGI.Format.A8_UNorm: targetGuid = WICPixelFormat.Format8bppGray; break; default: targetGuid = WICPixelFormat.Format24bppBGR; break; } using var wicFactory = new IWICImagingFactory(); using IWICBitmapDecoder decoder = wicFactory.CreateDecoderFromFileName(path); using Stream stream = File.OpenWrite(path); using IWICStream wicStream = wicFactory.CreateStream(stream); using IWICBitmapEncoder encoder = wicFactory.CreateEncoder(format, wicStream); // Create a Frame encoder var props = new SharpGen.Runtime.Win32.PropertyBag(); var frame = encoder.CreateNewFrame(props); frame.Initialize(props); frame.SetSize(textureDesc.Width, textureDesc.Height); frame.SetResolution(72, 72); frame.SetPixelFormat(targetGuid); var context = d3d11GraphicsDevice !.DeviceContext; //var mapped = context.Map(staging, 0, MapMode.Read, MapFlags.None); Span <Color> colors = context.Map <Color>(staging, 0, 0, MapMode.Read, MapFlags.None); // Check conversion if (targetGuid != pfGuid) { // Conversion required to write using (IWICBitmap bitmapSource = wicFactory.CreateBitmapFromMemory( textureDesc.Width, textureDesc.Height, pfGuid, colors)) { using (IWICFormatConverter formatConverter = wicFactory.CreateFormatConverter()) { formatConverter.CanConvert(pfGuid, targetGuid, out RawBool canConvert); if (!canConvert) { context.Unmap(staging, 0); return; } formatConverter.Initialize(bitmapSource, targetGuid, BitmapDitherType.None, null, 0, BitmapPaletteType.MedianCut); frame.WriteSource(formatConverter, new Rectangle(textureDesc.Width, textureDesc.Height)); } } } else { // No conversion required int stride = WICPixelFormat.GetStride(pfGuid, textureDesc.Width); frame.WritePixels(textureDesc.Height, stride, colors); } context.Unmap(staging, 0); frame.Commit(); encoder.Commit(); } }
public static void Main() { var wicFactory = new IWICImagingFactory(); D2D1.D2D1CreateFactory(out ID2D1Factory d2dFactory); DWrite.DWriteCreateFactory(out IDWriteFactory dwriteFactory).CheckError(); var textFormat = dwriteFactory.CreateTextFormat("Calibri", 20); const string fileName = "output.jpg"; const int width = 512; const int height = 512; var rectangleGeometry = d2dFactory.CreateRoundedRectangleGeometry(new RoundedRectangle(new RectangleF(128, 128, width - 128 * 2, height - 128 * 2), 32, 32)); var wicBitmap = wicFactory.CreateBitmap(width, height, Vortice.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties(Vortice.Direct2D1.PixelFormat.Unknown); var d2dRenderTarget = d2dFactory.CreateWicBitmapRenderTarget(wicBitmap, renderTargetProperties); var solidColorBrush = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f, 1.0f, 1.0f, 1.0f)); var redSolidColorBrush = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f, 0.0f, 0.0f, 1.0f)); d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(new Color4(0.0f, 0.0f, 0.0f, 1.0f)); d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); d2dRenderTarget.DrawText("Hello", textFormat, new Rect(0, 0, 120, 24), redSolidColorBrush); d2dRenderTarget.EndDraw(); if (File.Exists(fileName)) { File.Delete(fileName); } using (var stream = wicFactory.CreateStream(fileName, FileAccess.Write)) { // Initialize a Jpeg encoder with this stream using (var encoder = wicFactory.CreateEncoder(ContainerFormat.Jpeg, stream)) { // Create a Frame encoder var props = new SharpGen.Runtime.Win32.PropertyBag(); var bitmapFrameEncode = encoder.CreateNewFrame(props); bitmapFrameEncode.Initialize(null); bitmapFrameEncode.SetSize(width, height); bitmapFrameEncode.SetPixelFormat(Vortice.WIC.PixelFormat.FormatDontCare); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); } } /*using(var stream = File.OpenRead(fileName)) * { * GetTextureDimensions(wicFactory, stream); * }*/ using (var app = new TestApplication()) { app.Run(); } }