public static Bitmap LoadPngFromStream(Stream stream) { // Read input var wicStream = new WICStream(DXGraphicsService.FactoryImaging, stream); var decoder = new PngBitmapDecoder(DXGraphicsService.FactoryImaging); decoder.Initialize(wicStream, DecodeOptions.CacheOnDemand); var bitmapFrameDecode = decoder.GetFrame(0); var width = bitmapFrameDecode.Size.Width; var height = bitmapFrameDecode.Size.Height; //Convert WIC pixel format to D2D1 format var formatConverter = new FormatConverter(DXGraphicsService.FactoryImaging); formatConverter.Initialize(bitmapFrameDecode, PixelFormat.Format32bppBGRA, BitmapDitherType.None, null, 0f, BitmapPaletteType.MedianCut); // Bitmaps and render target settings var wicBitmap = new Bitmap( DXGraphicsService.FactoryImaging, width, height, PixelFormat.Format32bppBGRA, BitmapCreateCacheOption.CacheOnLoad); bitmapFrameDecode.Dispose(); decoder.Dispose(); wicStream.Dispose(); return(wicBitmap); }
internal SharpDX.DataStream CreateBitmapFromJpeg(MemoryStream stream, out SharpDX.Size2 size) { using (var istream = new WICStream(factory, stream)) { using (var decoder = new PngBitmapDecoder(factory)) { decoder.Initialize(istream, DecodeOptions.CacheOnDemand); using (var formatConverter = new FormatConverter(factory)) { formatConverter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA); var stride = formatConverter.Size.Width * 4; var dataStream = new SharpDX.DataStream(formatConverter.Size.Height * stride, true, true); formatConverter.CopyPixels(stride, dataStream); size = formatConverter.Size; return(dataStream); } } } }