public static MemoryStream ToMemoryStream(this global::SharpDX.WIC.Bitmap bitmap, IDevice2DResources deviceResources, Direct2DImageFormat imageType = Direct2DImageFormat.Bmp) { if (bitmap == null) { return(null); } var systemStream = new MemoryStream(); using (var stream = new WICStream(deviceResources.WICImgFactory, systemStream)) { using (var encoder = new BitmapEncoder(deviceResources.WICImgFactory, imageType.ToWICImageFormat())) { encoder.Initialize(stream); using (var frameEncoder = new BitmapFrameEncode(encoder)) { frameEncoder.Initialize(); frameEncoder.SetSize(bitmap.Size.Width, bitmap.Size.Height); frameEncoder.WriteSource(bitmap); frameEncoder.Commit(); encoder.Commit(); return(systemStream); } } } }
public static MemoryStream CreateBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Action <RenderTarget> drawingAction) { using (var bitmap = new global::SharpDX.WIC.Bitmap(deviceResources.WICImgFactory, (int)width, (int)height, global::SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnDemand)) { using (var target = new WicRenderTarget(deviceResources.Factory2D, bitmap, new RenderTargetProperties() { DpiX = 96, DpiY = 96, MinLevel = FeatureLevel.Level_DEFAULT, PixelFormat = new global::SharpDX.Direct2D1.PixelFormat(global::SharpDX.DXGI.Format.Unknown, AlphaMode.Unknown) })) { target.Transform = Matrix3x2.Identity; target.BeginDraw(); drawingAction(target); target.EndDraw(); } var systemStream = new MemoryStream(); using (var stream = new WICStream(deviceResources.WICImgFactory, systemStream)) { using (var encoder = new BitmapEncoder(deviceResources.WICImgFactory, imageType.ToWICImageFormat())) { encoder.Initialize(stream); using (var frameEncoder = new BitmapFrameEncode(encoder)) { frameEncoder.Initialize(); frameEncoder.SetSize((int)width, (int)height); frameEncoder.WriteSource(bitmap); frameEncoder.Commit(); encoder.Commit(); return(systemStream); } } } } }