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); Guid fdc = SharpDX.WIC.PixelFormat.FormatDontCare; //fdc = Direct2DConverter.ConvertImageFormat(Direct2DImageFormat.Gif); bitmapFrameEncode.SetPixelFormat(ref fdc); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); try { encoder.Commit(); }catch(Exception ex){ var f = ex.Message; } bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); }
//------------------------------------------------------------------------------------- // Encodes an image array //------------------------------------------------------------------------------------- private static void EncodeMultiframe(PixelBuffer[] images, int count, WICFlags flags, Guid guidContainerFormat, Stream stream) { if (images.Length < 2) { throw new ArgumentException("Cannot encode to multiple frame. Image doesn't have multiple frame"); } using (var encoder = new WIC.BitmapEncoder(Factory, guidContainerFormat)) { using (var eInfo = encoder.EncoderInfo) { if (!eInfo.IsMultiframeSupported) { throw new NotSupportedException("Cannot encode to multiple frame. Format is not supporting multiple frame"); } } encoder.Initialize(stream); for (int i = 0; i < Math.Min(images.Length, count); i++) { var pixelBuffer = images[i]; using (var frame = new WIC.BitmapFrameEncode(encoder)) EncodeImage(pixelBuffer, flags, frame); } encoder.Commit(); } }
public void Save(Stream stream, ImageFormat format) { using (var encoder = new s.WIC.BitmapEncoder( SDFactory.WicImagingFactory, format.ToWic())) { encoder.Initialize(stream); using (var frameEncoder = new s.WIC.BitmapFrameEncode(encoder)) { frameEncoder.WriteSource(Control); frameEncoder.Commit(); } encoder.Commit(); } }
public void Save(WIC.Bitmap wicBitmap, Stream stream) { using (var encoder = new WIC.BitmapEncoder(WicFactory, WIC.ContainerFormatGuids.Jpeg, stream)) using (var frame = new WIC.BitmapFrameEncode(encoder)) { frame.Initialize(); var pfx = WIC.PixelFormat.Format128bppRGBAFloat; //Format64bppRGBA; //FormatDontCare; frame.SetResolution(600, 600); frame.SetPixelFormat(ref pfx); frame.WriteSource(wicBitmap); frame.Commit(); encoder.Commit(); } }
public static void EncodeImage(this d2.Bitmap target, ImageFormat imageFormat, Stream outputStream) { var width = target.PixelSize.Width; var height = target.PixelSize.Height; var wicBitmap = new wic.Bitmap(DXGraphicsService.FactoryImaging, width, height, wic.PixelFormat.Format32bppBGR, wic.BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new d2.RenderTargetProperties(d2.RenderTargetType.Default, new d2.PixelFormat(Format.Unknown, d2.AlphaMode.Unknown), 0, 0, d2.RenderTargetUsage.None, d2.FeatureLevel.Level_DEFAULT); var d2DRenderTarget = new d2.WicRenderTarget(target.Factory, wicBitmap, renderTargetProperties); d2DRenderTarget.BeginDraw(); d2DRenderTarget.Clear(global::SharpDX.Color.Transparent); d2DRenderTarget.DrawBitmap(target, 1, d2.BitmapInterpolationMode.Linear); d2DRenderTarget.EndDraw(); var stream = new wic.WICStream(DXGraphicsService.FactoryImaging, outputStream); // Initialize a Jpeg encoder with this stream var encoder = new wic.BitmapEncoder(DXGraphicsService.FactoryImaging, GetImageFormat(imageFormat)); encoder.Initialize(stream); // Create a Frame encoder var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(width, height); Guid pixelFormatGuid = wic.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); d2DRenderTarget.Dispose(); wicBitmap.Dispose(); }
private static void SaveD2DBitmap(WIC.ImagingFactory2 wic, WIC.Bitmap wicBitmap, Stream outputStream) { using (var encoder = new WIC.BitmapEncoder(wic, WIC.ContainerFormatGuids.Png)) { encoder.Initialize(outputStream); using (var frame = new WIC.BitmapFrameEncode(encoder)) { 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 void WriteToEncoder(wic.BitmapEncoder encoder, wic.WICStream wicStream) { encoder.Initialize(wicStream); using (var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(Width, Height); var format = wic.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref format); // this is the trick to write D2D1 bitmap to WIC var imageEncoder = new wic.ImageEncoder(DXGraphicsService.FactoryImaging, _device2D); var parameters = new wic.ImageParameters(_pixelFormat, Dpi, Dpi, 0, 0, Width, Height); imageEncoder.WriteFrame(_bitmap, bitmapFrameEncode, parameters); bitmapFrameEncode.Commit(); encoder.Commit(); } }
private static void EncodeSingleFrame(PixelBuffer pixelBuffer, WICFlags flags, Guid guidContainerFormat, Stream stream) { using (var encoder = new WIC.BitmapEncoder(Factory, guidContainerFormat, stream)) { using (var frame = new WIC.BitmapFrameEncode(encoder)) { if (guidContainerFormat == WIC.ContainerFormatGuids.Bmp) { try { frame.Options.Set("EnableV5Header32bppBGRA", true); } catch { } } EncodeImage(pixelBuffer, flags, frame); encoder.Commit(); } } }
public bool GetThumbnail(Stream stream, int width, int height, bool cachedOnly, out byte[] imageData, out ImageType imageType) { imageData = null; imageType = ImageType.Unknown; // No support for cache if (cachedOnly) return false; try { if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin); // open the image file for reading using (var factory = new ImagingFactory2()) using (var inputStream = new WICStream(factory, stream)) using (var decoder = new BitmapDecoder(factory, inputStream, DecodeOptions.CacheOnLoad)) using (var scaler = new BitmapScaler(factory)) using (var output = new MemoryStream()) using (var encoder = new BitmapEncoder(factory, ContainerFormatGuids.Jpeg)) { // decode the loaded image to a format that can be consumed by D2D BitmapSource source = decoder.GetFrame(0); // Scale down larger images int sourceWidth = source.Size.Width; int sourceHeight = source.Size.Height; if (width > 0 && height > 0 && (sourceWidth > width || sourceHeight > height)) { if (sourceWidth <= height) width = sourceWidth; int newHeight = sourceHeight * height / sourceWidth; if (newHeight > height) { // Resize with height instead width = sourceWidth * height / sourceHeight; newHeight = height; } scaler.Initialize(source, width, newHeight, BitmapInterpolationMode.Fant); source = scaler; } encoder.Initialize(output); using (var bitmapFrameEncode = new BitmapFrameEncode(encoder)) { // Create image encoder var wicPixelFormat = PixelFormat.FormatDontCare; bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(source.Size.Width, source.Size.Height); bitmapFrameEncode.SetPixelFormat(ref wicPixelFormat); bitmapFrameEncode.WriteSource(source); bitmapFrameEncode.Commit(); encoder.Commit(); } imageData = output.ToArray(); imageType = ImageType.Jpeg; return true; } } catch (Exception e) { // ServiceRegistration.Get<ILogger>().Warn("WICThumbnailProvider: Error loading bitmapSource from file data stream", e); return false; } }
//------------------------------------------------------------------------------------- // Encodes an image array //------------------------------------------------------------------------------------- private static void EncodeMultiframe( PixelBuffer[] images, int count, WICFlags flags, Guid guidContainerFormat, Stream stream ) { if ( images.Length < 2 ) throw new ArgumentException("Cannot encode to multiple frame. Image doesn't have multiple frame"); using (var encoder = new BitmapEncoder(Factory, guidContainerFormat)) { using (var eInfo = encoder.EncoderInfo) { if (!eInfo.IsMultiframeSupported) throw new NotSupportedException("Cannot encode to multiple frame. Format is not supporting multiple frame"); } encoder.Initialize(stream); for (int i = 0; i < Math.Min(images.Length, count); i++) { var pixelBuffer = images[i]; using (var frame = new BitmapFrameEncode(encoder)) EncodeImage(pixelBuffer, flags, frame); } encoder.Commit(); } }
private static void EncodeSingleFrame( PixelBuffer pixelBuffer, WICFlags flags, Guid guidContainerFormat, Stream stream ) { using (var encoder = new BitmapEncoder(Factory, guidContainerFormat, stream)) { using (var frame = new BitmapFrameEncode(encoder)) { if (guidContainerFormat == ContainerFormatGuids.Bmp) { try { frame.Options.Set("EnableV5Header32bppBGRA", true); } catch { } } EncodeImage(pixelBuffer, flags, frame); encoder.Commit(); } } }
private static void EncodeSingleframe(ImagingFactory imagingFactory, Image image, Stream stream, Guid containerFormat, WicFlags flags) { using (var encoder = new BitmapEncoder(imagingFactory, containerFormat, stream)) { using (var frame = new BitmapFrameEncode(encoder)) { if (containerFormat == ContainerFormatGuids.Bmp) { #pragma warning disable 168 // ReSharper disable once EmptyGeneralCatchClause try { frame.Options.Set("EnableV5Header32bppBGRA", true); } catch (Exception exception) { // WIC2 is available on Windows 8 and Windows 7 SP1 with KB 2670838 installed // SharpDX: ImagingFactory2 is only available in Windows 8 build. } #pragma warning restore 168 } EncodeImage(imagingFactory, image, flags, containerFormat, frame); encoder.Commit(); } } }
private static void EncodeMultiframe(ImagingFactory imagingFactory, IList<Image> images, Stream stream, Guid containerFormat, WicFlags flags) { using (var encoder = new BitmapEncoder(imagingFactory, containerFormat)) { using (var encoderInfo = encoder.EncoderInfo) { if (!encoderInfo.IsMultiframeSupported) throw new NotSupportedException("The specified image format does not support multiple frames."); } encoder.Initialize(stream); for (int i = 0; i < images.Count; i++) { var image = images[i]; using (var frame = new BitmapFrameEncode(encoder)) EncodeImage(imagingFactory, image, flags, containerFormat, frame); } encoder.Commit(); } }
private Result SaveWICTextureToFileFix( DeviceContext context, Texture2D source, ref Guid guidContainerFormat, string fileName) { if (fileName == null) { return(Result.InvalidArg); } Result res = CaptureTextureFix(context, source, out Texture2DDescription desc, out Texture2D staging); if (res.Failure) { return(res); } Guid pfGuid; //bool sRGB = false; Guid targetGuid; switch (desc.Format) { case DXGI.Format.R32G32B32A32_Float: pfGuid = WIC.PixelFormat.Format128bppRGBAFloat; break; case DXGI.Format.R16G16B16A16_Float: pfGuid = WIC.PixelFormat.Format64bppRGBAHalf; break; case DXGI.Format.R16G16B16A16_UNorm: pfGuid = WIC.PixelFormat.Format64bppRGBA; break; case DXGI.Format.R10G10B10_Xr_Bias_A2_UNorm: pfGuid = WIC.PixelFormat.Format32bppRGBA1010102XR; break; // DXGI 1.1 case DXGI.Format.R10G10B10A2_UNorm: pfGuid = WIC.PixelFormat.Format32bppRGBA1010102; break; case DXGI.Format.B5G5R5A1_UNorm: pfGuid = WIC.PixelFormat.Format16bppBGRA5551; break; case DXGI.Format.B5G6R5_UNorm: pfGuid = WIC.PixelFormat.Format16bppBGR565; break; case DXGI.Format.R32_Float: pfGuid = WIC.PixelFormat.Format32bppGrayFloat; break; case DXGI.Format.R16_Float: pfGuid = WIC.PixelFormat.Format16bppGrayHalf; break; case DXGI.Format.R16_UNorm: pfGuid = WIC.PixelFormat.Format16bppGray; break; case DXGI.Format.R8_UNorm: pfGuid = WIC.PixelFormat.Format8bppGray; break; case DXGI.Format.A8_UNorm: pfGuid = WIC.PixelFormat.Format8bppAlpha; break; case DXGI.Format.R8G8B8A8_UNorm: pfGuid = WIC.PixelFormat.Format32bppRGBA; break; case DXGI.Format.R8G8B8A8_UNorm_SRgb: pfGuid = WIC.PixelFormat.Format32bppRGBA; //sRGB = true; break; case DXGI.Format.B8G8R8A8_UNorm: // DXGI 1.1 pfGuid = WIC.PixelFormat.Format32bppBGRA; break; case DXGI.Format.B8G8R8A8_UNorm_SRgb: // DXGI 1.1 pfGuid = WIC.PixelFormat.Format32bppBGRA; //sRGB = true; break; case DXGI.Format.B8G8R8X8_UNorm: // DXGI 1.1 pfGuid = WIC.PixelFormat.Format32bppBGR; break; case DXGI.Format.B8G8R8X8_UNorm_SRgb: // DXGI 1.1 pfGuid = WIC.PixelFormat.Format32bppBGR; //sRGB = true; break; default: return(Result.GetResultFromWin32Error(unchecked ((int)0x80070032))); } // Create file FileStream fs = new FileStream(fileName, FileMode.Create); WIC.BitmapEncoder encoder = new WIC.BitmapEncoder(DirectX.ImageFactory, guidContainerFormat); encoder.Initialize(fs); WIC.BitmapFrameEncode frameEncode = new WIC.BitmapFrameEncode(encoder); frameEncode.Initialize(); frameEncode.SetSize(desc.Width, desc.Height); frameEncode.SetResolution(72.0, 72.0); switch (desc.Format) { case DXGI.Format.R32G32B32A32_Float: case DXGI.Format.R16G16B16A16_Float: targetGuid = WIC.PixelFormat.Format24bppBGR; break; case DXGI.Format.R16G16B16A16_UNorm: targetGuid = WIC.PixelFormat.Format48bppBGR; break; case DXGI.Format.B5G5R5A1_UNorm: targetGuid = WIC.PixelFormat.Format16bppBGR555; break; case DXGI.Format.B5G6R5_UNorm: targetGuid = WIC.PixelFormat.Format16bppBGR565; break; case DXGI.Format.R32_Float: case DXGI.Format.R16_Float: case DXGI.Format.R16_UNorm: case DXGI.Format.R8_UNorm: case DXGI.Format.A8_UNorm: targetGuid = WIC.PixelFormat.Format8bppGray; break; default: targetGuid = WIC.PixelFormat.Format24bppBGR; break; } frameEncode.SetPixelFormat(ref targetGuid); #region Write DataBox db = context.MapSubresource(staging, 0, MapMode.Read, MapFlags.None, out DataStream stream); if (pfGuid != targetGuid) { WIC.FormatConverter formatCoverter = new WIC.FormatConverter(DirectX.ImageFactory); if (formatCoverter.CanConvert(pfGuid, targetGuid)) { WIC.Bitmap src = new WIC.Bitmap(DirectX.ImageFactory, desc.Width, desc.Height, pfGuid, new DataRectangle(db.DataPointer, db.RowPitch)); formatCoverter.Initialize(src, targetGuid, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom); frameEncode.WriteSource(formatCoverter, new Rectangle(0, 0, desc.Width, desc.Height)); } } else { frameEncode.WritePixels(desc.Height, new DataRectangle(db.DataPointer, db.RowPitch)); } context.UnmapSubresource(staging, 0); frameEncode.Commit(); encoder.Commit(); #endregion frameEncode.Dispose(); encoder.Dispose(); fs.Close(); return(Result.Ok); }