private Bitmap CreateOutputImage(Action <WicRenderTarget> action) { try { var bitmap = new Bitmap(deviceRes2D.WICImgFactory, OutputWidth, OutputHeight, global::SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand); using (var target = new WicRenderTarget(deviceRes2D.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(); action(target); target.EndDraw(); } return(bitmap); } catch { return(null); } }
public void Save(Stream systemStream, Direct2DImageFormat format, string text, string faceName, float fontSize, out int width, out int height) { #if BENCHMARK using (var handler = Benchmark.Instance.Start("DirectWrite", "Save")) #endif using (var layout = new TextLayout(factoryManager.DwFactory, text, new TextFormat(factoryManager.DwFactory, faceName, fontSize * 1.3f), 4000, 4000)) { width = (int)Math.Ceiling(layout.Metrics.WidthIncludingTrailingWhitespace); height = (int)Math.Ceiling(layout.Metrics.Height); using (var wicBitmap = new SharpDX.WIC.Bitmap(factoryManager.WicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPRGBA, BitmapCreateCacheOption.CacheOnLoad)) { var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Unknown), imageDpi, imageDpi, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); using (var renderTarget = new WicRenderTarget(factoryManager.D2DFactory, wicBitmap, renderTargetProperties)) using (var brush = new SolidColorBrush(renderTarget, SharpDX.Color.White)) using (var encoder = new BitmapEncoder(factoryManager.WicFactory, Direct2DConverter.ConvertImageFormat(format))) { renderTarget.BeginDraw(); renderTarget.Clear(new Color4(1, 1, 1, 0)); renderTarget.DrawTextLayout(Vector2.Zero, layout, brush); renderTarget.EndDraw(); var stream = new WICStream(factoryManager.WicFactory, systemStream); encoder.Initialize(stream); using (var bitmapFrameEncode = new BitmapFrameEncode(encoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(width, height); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); } encoder.Commit(); } } } }
public InfoText(SharpDX.Direct3D11.Device device, int width, int height) { _immediateContext = device.ImmediateContext; _rect.Size = new Size2F(width, height); _bitmapSize = width * height * 4; IsEnabled = true; using (var factoryWic = new SharpDX.WIC.ImagingFactory()) { _wicBitmap = new SharpDX.WIC.Bitmap(factoryWic, width, height, _pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); } var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); using (var factory2D = new SharpDX.Direct2D1.Factory()) { _wicRenderTarget = new WicRenderTarget(factory2D, _wicBitmap, renderTargetProperties); _wicRenderTarget.TextAntialiasMode = TextAntialiasMode.Default; } using (var factoryDWrite = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)) { _textFormat = new TextFormat(factoryDWrite, "Tahoma", 20); } Color4 color = new Color4(1, 1, 1, 1); _sceneColorBrush = new SolidColorBrush(_wicRenderTarget, color); _clearColor = color; _clearColor.Alpha = 0; _renderTexture = new Texture2D(device, new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Dynamic }); OverlayBufferView = new ShaderResourceView(device, _renderTexture, new ShaderResourceViewDescription() { Format = _renderTexture.Description.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1, MostDetailedMip = 0 } }); }
public static global::SharpDX.WIC.Bitmap CreateBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Action <RenderTarget> drawingAction) { if (width <= 0 || height <= 0) { return(null); } var bitmap = new global::SharpDX.WIC.Bitmap(deviceResources.WICImgFactory, width, 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(); } return(bitmap); }
private void CreateBitmap(int width, int height) { if (_bitmap == null || (_bitmap.Size.Width < width || _bitmap.Size.Height < height)) { //RenderTargetProperties rtp = new RenderTargetProperties(RenderTargetType.Default, // new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), // Factory2D.DesktopDpi.Width, // Factory2D.DesktopDpi.Height, // RenderTargetUsage.None, // FeatureLevel.Level_DEFAULT); try { var pixelFormat = SharpDX.WIC.PixelFormat.Format32bppPRGBA; _bitmap = new SharpDX.WIC.Bitmap(FactoryImaging, width, height, pixelFormat, BitmapCreateCacheOption.CacheOnLoad); _renderTarget = new WicRenderTarget(Factory2D, _bitmap, new RenderTargetProperties()); //_renderTarget = new WicRenderTarget(Factory2D, _bitmap, rtpGDI); if (_brush == null) { _brush = new SolidColorBrush(_renderTarget, new Color4(new Color3(1, 1, 1), 1.0f)); } } catch (Exception exc) { var ss = exc.Message; } } }
public void Dispose() { if (stream != null) { stream.Dispose(); stream = null; } if (d2dRenderTarget != null) { d2dRenderTarget.Dispose(); d2dRenderTarget = null; } if (wicBitmap != null) { wicBitmap.Dispose(); wicBitmap = null; } if (d2dFactory != null) { d2dFactory.Dispose(); d2dFactory = null; } if (wicFactory != null) { wicFactory.Dispose(); wicFactory = null; } if (memStream != null) { memStream.Dispose(); } }
protected void ReleaseResources() { _bitmap.Dispose(); _bitmap = null; _renderTarget.Dispose(); _renderTarget = null; }
private static void Main() { var wicFactory = new ImagingFactory(); var d2dFactory = new SharpDX.Direct2D1.Factory(); string filename = "output.jpg"; const int width = 512; const int height = 512; var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2) }); var wicBitmap = new Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Black); d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null); d2dRenderTarget.EndDraw(); if (File.Exists(filename)) { File.Delete(filename); } var stream = new WICStream(wicFactory, filename, NativeFileAccess.Write); // Initialize a Jpeg encoder with this stream var encoder = new JpegBitmapEncoder(wicFactory); encoder.Initialize(stream); // Create a Frame encoder var bitmapFrameEncode = new BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(width, height); var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); System.Diagnostics.Process.Start(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, filename))); }
public MemoryStream RenderToPngStream(FrameworkElement fe) { var width = (int)Math.Ceiling(fe.ActualWidth); var height = (int)Math.Ceiling(fe.ActualHeight); // pixel format with transparency/alpha channel and RGB values premultiplied by alpha var pixelFormat = WicPixelFormat.Format32bppPRGBA; // pixel format without transparency, but one that works with Cleartype antialiasing //var pixelFormat = WicPixelFormat.Format32bppBGR; var wicBitmap = new Bitmap( this.WicFactory, width, height, pixelFormat, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties( RenderTargetType.Default, new D2DPixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), //new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), // use this for non-alpha, cleartype antialiased text 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var renderTarget = new WicRenderTarget( this.D2DFactory, wicBitmap, renderTargetProperties) { //TextAntialiasMode = TextAntialiasMode.Cleartype // this only works with the pixel format with no alpha channel TextAntialiasMode = TextAntialiasMode.Grayscale // this is the best we can do for bitmaps with alpha channels }; Compose(renderTarget, fe); // TODO: There is no need to encode the bitmap to PNG - we could just copy the texture pixel buffer to a WriteableBitmap pixel buffer. var ms = new MemoryStream(); var stream = new WICStream( this.WicFactory, ms); var encoder = new PngBitmapEncoder(WicFactory); encoder.Initialize(stream); var frameEncoder = new BitmapFrameEncode(encoder); frameEncoder.Initialize(); frameEncoder.SetSize(width, height); var format = WicPixelFormat.Format32bppBGRA; //var format = WicPixelFormat.FormatDontCare; frameEncoder.SetPixelFormat(ref format); frameEncoder.WriteSource(wicBitmap); frameEncoder.Commit(); encoder.Commit(); frameEncoder.Dispose(); encoder.Dispose(); stream.Dispose(); ms.Position = 0; return(ms); }
public DXGI2DContext(int width, int height) { this.width = width; this.height = height; wicFactory = new SharpDX.WIC.ImagingFactory(); d2dFactory = new SharpDX.Direct2D1.Factory(); wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); }
protected override void DrawOntoOutputTarget(WicRenderTarget target) { foreach (var text in ItemArray) { var location = ImagePlacement[text.Key]; var t = text.Value; using (var brush = new SolidColorBrush(target, t.Background)) target.FillRectangle(location, brush); using (var brush = new SolidColorBrush(target, t.Foreground)) target.DrawTextLayout(new Vector2(location.Left + text.Value.Padding.X, location.Top + text.Value.Padding.Y), t.TextLayout, brush, DrawTextOptions.None); } }
public Direct2DImageEncoder(int imageWidth, int imageHeight, int imageDpi) { this.imageWidth = imageWidth; this.imageHeight = imageHeight; factoryManager = new Direct2DFactoryManager(); wicBitmap = new SharpDX.WIC.Bitmap(factoryManager.WicFactory, imageWidth, imageHeight, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, AlphaMode.Unknown), imageDpi, imageDpi, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); renderTarget = new WicRenderTarget(factoryManager.D2DFactory, wicBitmap, renderTargetProperties); renderTarget.BeginDraw(); renderTarget.Clear(SharpDX.Color.Yellow); }
protected override void DrawOntoOutputTarget(WicRenderTarget target) { // draw all the images into the output image foreach (var image in ItemArray) { var location = ImagePlacement[image.Key]; var img = image.Value; using (var bmp = global::SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, img)) { target.DrawBitmap(bmp, new RawRectangleF(location.Left, location.Top, location.Right, location.Bottom), 1, global::SharpDX.Direct2D1.BitmapInterpolationMode.Linear); } } }
public RenderTargetBitmapImpl( ImagingFactory imagingFactory, Factory d2dFactory, int width, int height) : base(imagingFactory, width, height) { this.target = new WicRenderTarget( d2dFactory, this.WicImpl, new RenderTargetProperties { DpiX = 96, DpiY = 96, }); }
public WicRenderTargetBitmapImpl( PixelSize size, Vector dpi, Platform.PixelFormat?pixelFormat = null) : base(size, dpi, pixelFormat) { var props = new RenderTargetProperties { DpiX = (float)dpi.X, DpiY = (float)dpi.Y, }; _renderTarget = new WicRenderTarget( Direct2D1Platform.Direct2D1Factory, WicImpl, props); }
public RenderTargetBitmapImpl( ImagingFactory imagingFactory, Factory d2dFactory, int width, int height) : base(imagingFactory, width, height) { var props = new RenderTargetProperties { DpiX = 96, DpiY = 96, }; _target = new WicRenderTarget( d2dFactory, WicImpl, props); }
public WicRenderTargetBitmapImpl( int width, int height, double dpiX, double dpiY, Platform.PixelFormat?pixelFormat = null) : base(width, height, pixelFormat) { var props = new RenderTargetProperties { DpiX = (float)dpiX, DpiY = (float)dpiY, }; _renderTarget = new WicRenderTarget( Direct2D1Platform.Direct2D1Factory, WicImpl, props); }
/// <summary> /// Creates an instance of D2dWicGraphics that can be used for rendering to off screen surfaces /// and can be copied to main memory</summary> /// <param name="width">Width of the off screen surface.</param> /// <param name="height">Height of the off screen surface.</param> /// <returns>D2dWicGraphics that can be used for rendering to off screen surfaces</returns> public static D2dWicGraphics CreateWicGraphics(int width, int height) { var wicBitmap = new SharpDX.WIC.Bitmap(s_wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var rtprops = new RenderTargetProperties { Type = RenderTargetType.Default, DpiX = 96.0f, DpiY = 96.0f, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Unknown), Usage = RenderTargetUsage.None, MinLevel = FeatureLevel.Level_DEFAULT }; var rt = new WicRenderTarget(s_d2dFactory, wicBitmap, rtprops); return(new D2dWicGraphics(rt, wicBitmap)); }
public Direct2D1RenderTargetBitmap( Factory d2dFactory, ImagingFactory wicFactory, SharpDX.WIC.Bitmap bitmap) : base(wicFactory, bitmap) { double dpiX; double dpiY; this.d2dFactory = d2dFactory; bitmap.GetResolution(out dpiX, out dpiY); this.target = new WicRenderTarget( d2dFactory, bitmap, new RenderTargetProperties { DpiX = (float)dpiX, DpiY = (float)dpiY, }); }
public RenderTarget Create(Factory factory, Graphics g, Map map) { var wicBitmap = new WICBitmap(_wicFactory, map.Size.Width, map.Size.Height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnDemand); var rtp = new RenderTargetProperties(RenderTargetType.Default, new D2D1PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var res = new WicRenderTarget(factory, wicBitmap, rtp) { Tag = wicBitmap }; res.BeginDraw(); res.Clear(SharpDX.Color.Transparent); return(res); }
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); } } } } }
public DrawingImage(Size size) { pw.Set(); PixelSize = size; buffer = new WICBitmap( iFactory, (int)size.Width, (int)size.Height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad); View = new WicRenderTarget(DxFac, buffer, new RenderTargetProperties( RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT)); load_buffer = new WICBitmap(iFactory, buffer, BitmapCreateCacheOption.CacheOnLoad); }
public void Dispose() { if (_renderTexture != null) { _renderTexture.Dispose(); _renderTexture = null; } if (_wicBitmap != null) { _wicBitmap.Dispose(); _wicBitmap = null; } if (_wicRenderTarget != null) { _wicRenderTarget.Dispose(); _wicRenderTarget = null; } if (_sceneColorBrush != null) { _sceneColorBrush.Dispose(); _sceneColorBrush = null; } if (OverlayBufferRes != null) { OverlayBufferRes.Dispose(); OverlayBufferRes = null; } if (_textFormat != null) { _textFormat.Dispose(); _textFormat = null; } }
public RenderTargetBitmapImpl( ImagingFactory imagingFactory, Factory d2dFactory, DirectWriteFactory dwriteFactory, int width, int height, double dpiX, double dpiY) : base(imagingFactory, width, height) { var props = new RenderTargetProperties { DpiX = (float)dpiX, DpiY = (float)dpiY, }; _target = new WicRenderTarget( d2dFactory, WicImpl, props); _dwriteFactory = dwriteFactory; }
// // http://stackoverflow.com/questions/9151615/how-does-one-use-a-memory-stream-instead-of-files-when-rendering-direct2d-images // // Identical to above SO question, except that we are rendering to MemoryStream because it was added to the API // private MemoryStream RenderStaticTextToBitmap() { var width = 400; var height = 100; var pixelFormat = WicPixelFormat.Format32bppBGR; var wicFactory = new ImagingFactory(); var dddFactory = new SharpDX.Direct2D1.Factory(); var dwFactory = new SharpDX.DirectWrite.Factory(); var wicBitmap = new Bitmap( wicFactory, width, height, pixelFormat, BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new RenderTargetProperties( RenderTargetType.Default, new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var renderTarget = new WicRenderTarget( dddFactory, wicBitmap, renderTargetProperties) { TextAntialiasMode = TextAntialiasMode.Cleartype }; renderTarget.BeginDraw(); var textFormat = new TextFormat(dwFactory, "Consolas", 48) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }; var textBrush = new SharpDX.Direct2D1.SolidColorBrush( renderTarget, SharpDX.Colors.Blue); renderTarget.Clear(Colors.White); renderTarget.DrawText( "Hi, mom!", textFormat, new RectangleF(0, 0, width, height), textBrush); renderTarget.EndDraw(); var ms = new MemoryStream(); var stream = new WICStream( wicFactory, ms); var encoder = new PngBitmapEncoder(wicFactory); encoder.Initialize(stream); var frameEncoder = new BitmapFrameEncode(encoder); frameEncoder.Initialize(); frameEncoder.SetSize(width, height); frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare; frameEncoder.WriteSource(wicBitmap); frameEncoder.Commit(); encoder.Commit(); frameEncoder.Dispose(); encoder.Dispose(); stream.Dispose(); ms.Position = 0; return(ms); }
public dx_resources() { d3dDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); wicFactory = new SharpDX.WIC.ImagingFactory(); d2dFactory = new SharpDX.Direct2D1.Factory(); Texture2DDescription colordesc = new Texture2DDescription { BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = Format.B8G8R8A8_UNorm, Width = 1, Height = 1, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; this.RenderTarget = new Texture2D(d3dDevice, colordesc); Surface surface = this.RenderTarget.QueryInterface <Surface>(); RenderTargetProperties rtp = new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)); d2d_render_target = new RenderTarget(d2dFactory, surface, rtp); //d2d_render_target.AntialiasMode = AntialiasMode.PerPrimitive; descriptor_image_plus = new SharpDX.Direct2D1.Bitmap[6]; int bitmap_index = 0; for (int width = 16; width <= 512; width *= 2) { SharpDX.WIC.Bitmap point_image_wic = new SharpDX.WIC.Bitmap(wicFactory, (int)width, (int)width, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad); RenderTargetProperties renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); WicRenderTarget d2dRenderTarget = new WicRenderTarget(d2dFactory, point_image_wic, renderTargetProperties); SolidColorBrush solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.Black); d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Transparent); if (width >= 256) { d2dRenderTarget.DrawEllipse(new Ellipse(new Vector2(width / 2f, width / 2f), width / 2f - 2f, width / 2f - 2f), solidColorBrush, 2f); } else { d2dRenderTarget.DrawEllipse(new Ellipse(new Vector2(width / 2f, width / 2f), width / 2f - 2f, width / 2f - 2f), solidColorBrush, 1f); } d2dRenderTarget.EndDraw(); BitmapProperties props = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); descriptor_image_plus[bitmap_index] = SharpDX.Direct2D1.Bitmap.FromWicBitmap(d2d_render_target, point_image_wic, props); solidColorBrush.Dispose(); d2dRenderTarget.Dispose(); bitmap_index++; } descriptor_image_minus = new SharpDX.Direct2D1.Bitmap[6]; bitmap_index = 0; for (int width = 16; width <= 512; width *= 2) { SharpDX.WIC.Bitmap point_image_wic = new SharpDX.WIC.Bitmap(wicFactory, (int)width, (int)width, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad); RenderTargetProperties renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); WicRenderTarget d2dRenderTarget = new WicRenderTarget(d2dFactory, point_image_wic, renderTargetProperties); SolidColorBrush solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White); d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Transparent); if (width >= 256) { d2dRenderTarget.DrawEllipse(new Ellipse(new Vector2(width / 2f, width / 2f), width / 2f - 2f, width / 2f - 2f), solidColorBrush, 2f); } else { d2dRenderTarget.DrawEllipse(new Ellipse(new Vector2(width / 2f, width / 2f), width / 2f - 2f, width / 2f - 2f), solidColorBrush, 1f); } d2dRenderTarget.EndDraw(); BitmapProperties props = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); descriptor_image_minus[bitmap_index] = SharpDX.Direct2D1.Bitmap.FromWicBitmap(d2d_render_target, point_image_wic, props); solidColorBrush.Dispose(); d2dRenderTarget.Dispose(); bitmap_index++; } float point_size = 10; SharpDX.WIC.Bitmap wic_point_image = new SharpDX.WIC.Bitmap(wicFactory, (int)point_size, (int)point_size, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad); RenderTargetProperties renderTargetProperties_point_image = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); WicRenderTarget d2dRenderTarget_point = new WicRenderTarget(d2dFactory, wic_point_image, renderTargetProperties_point_image); SolidColorBrush solidColorBrush_point = new SolidColorBrush(d2dRenderTarget_point, new Color4(137f / 255f, 201f / 255f, 238f / 255f, 1f)); SolidColorBrush solidColorBrush_point2 = new SolidColorBrush(d2dRenderTarget_point, Color.Black); d2dRenderTarget_point.BeginDraw(); d2dRenderTarget_point.Clear(Color.Transparent); d2dRenderTarget_point.FillEllipse(new Ellipse(new Vector2(point_size / 2f, point_size / 2f), point_size / 2f - 2f, point_size / 2f - 2f), solidColorBrush_point); d2dRenderTarget_point.DrawEllipse(new Ellipse(new Vector2(point_size / 2f, point_size / 2f), point_size / 2f - 2f, point_size / 2f - 2f), solidColorBrush_point2); d2dRenderTarget_point.EndDraw(); BitmapProperties props_point = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); point_image = SharpDX.Direct2D1.Bitmap.FromWicBitmap(d2d_render_target, wic_point_image, props_point); solidColorBrush_point2.Dispose(); solidColorBrush_point.Dispose(); d2dRenderTarget_point.Dispose(); surface.Dispose(); }
/// <summary> /// Draws the onto output target. /// </summary> /// <param name="target">The target.</param> protected abstract void DrawOntoOutputTarget(WicRenderTarget target);
// private static SharpDX.Direct2D1.DeviceContext d2dContext; public async static Task <MemoryStream> RenderStaticTextToBitmap(StorageFile imageFile) { var bitmap = new BitmapImage(); using (var strm = await imageFile.OpenAsync(FileAccessMode.Read)) { bitmap.SetSource(strm); } var width = bitmap.PixelWidth; var height = bitmap.PixelHeight; var pixelFormat = WicPixelFormat.Format32bppBGR; var wicFactory = new ImagingFactory2(); var dddFactory = new SharpDX.Direct2D1.Factory(); var dwFactory = new SharpDX.DirectWrite.Factory(); WicRenderTarget renderTarget; Bitmap wicBitmap; using (var bitmapSource = LoadBitmap(wicFactory, imageFile.Path)) { wicBitmap = new Bitmap(wicFactory, bitmapSource, BitmapCreateCacheOption.CacheOnLoad); int pixelWidth = (int)(wicBitmap.Size.Width * DisplayProperties.LogicalDpi / 96.0); int pixelHeight = (int)(wicBitmap.Size.Height * DisplayProperties.LogicalDpi / 96.0); var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); renderTarget = new WicRenderTarget( dddFactory, wicBitmap, renderTargetProperties) { TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype }; } renderTarget.BeginDraw(); var textFormat = new TextFormat(dwFactory, "Segoe UI Light", 25) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Far }; var textBrush = new SharpDX.Direct2D1.SolidColorBrush( renderTarget, SharpDX.Color.DarkBlue); StringBuilder sb = new StringBuilder(); var dstamp = StampPersistData.Instance.DataStamp; sb.Append(dstamp.KMReading).Append("\n"); sb.Append(dstamp.DateOfFirstReg).Append("\n"); sb.Append(dstamp.Gps).Append("\n"); sb.Append(dstamp.VehRegNo).Append("\n"); sb.Append(dstamp.Make).Append("\n"); sb.Append(dstamp.CusName).Append("\n"); sb.Append(dstamp.InspectorName).Append("\n"); sb.Append(dstamp.CaseNo); renderTarget.DrawText( sb.ToString(), textFormat, new SharpDX.RectangleF(1, 1, width + 50, height + 25), textBrush); //new RectangleF(width - 150, 0, width, height + 25), renderTarget.EndDraw(); var ms = new MemoryStream(); var stream = new WICStream( wicFactory, ms); BitmapEncoder encoder = null; if (imageFile.FileType == ".png") { encoder = new PngBitmapEncoder(wicFactory); } else if (imageFile.FileType == ".jpg") { encoder = new JpegBitmapEncoder(wicFactory); } encoder.Initialize(stream); var frameEncoder = new BitmapFrameEncode(encoder); frameEncoder.Initialize(); frameEncoder.SetSize(width, height); // frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare; frameEncoder.WriteSource(wicBitmap); frameEncoder.Commit(); encoder.Commit(); frameEncoder.Dispose(); encoder.Dispose(); stream.Dispose(); ms.Position = 0; return(ms); }
internal D2dWicGraphics(WicRenderTarget renderTarget, SharpDX.WIC.Bitmap wicBitmap) : base(renderTarget) { m_wicBitmap = wicBitmap; }
internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition) { if (string.IsNullOrEmpty(text)) { return(new CCTexture2D()); } int imageWidth; int imageHeight; var textDef = textDefinition; var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width; var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height; textDef.FontSize *= (int)contentScaleFactorWidth; textDef.Dimensions.Width *= contentScaleFactorWidth; textDef.Dimensions.Height *= contentScaleFactorHeight; bool hasPremultipliedAlpha; var font = CreateFont(textDef.FontName, textDef.FontSize); var _currentFontSizeEm = textDef.FontSize; var _currentDIP = ConvertPointSizeToDIP(_currentFontSizeEm); var fontColor = textDef.FontFillColor; var fontAlpha = textDef.FontAlpha; var foregroundColor = new Color4(fontColor.R / 255.0f, fontColor.G / 255.0f, fontColor.B / 255.0f, fontAlpha / 255.0f); // alignment var horizontalAlignment = textDef.Alignment; var verticleAlignement = textDef.LineAlignment; var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? TextAlignment.Trailing : (CCTextAlignment.Center == horizontalAlignment) ? TextAlignment.Center : TextAlignment.Leading; var paragraphAlign = (CCVerticalTextAlignment.Bottom == vertAlignment) ? ParagraphAlignment.Far : (CCVerticalTextAlignment.Center == vertAlignment) ? ParagraphAlignment.Center : ParagraphAlignment.Near; // LineBreak var lineBreak = (CCLabelLineBreak.Character == textDef.LineBreak) ? WordWrapping.Wrap : (CCLabelLineBreak.Word == textDef.LineBreak) ? WordWrapping.Wrap : WordWrapping.NoWrap; // LineBreak // TODO: Find a way to specify the type of line breaking if possible. var dimensions = new CCSize(textDef.Dimensions.Width, textDef.Dimensions.Height); var layoutAvailable = true; if (dimensions.Width <= 0) { dimensions.Width = 8388608; layoutAvailable = false; } if (dimensions.Height <= 0) { dimensions.Height = 8388608; layoutAvailable = false; } var fontName = font.FontFamily.FamilyNames.GetString(0); var textFormat = new TextFormat(FactoryDWrite, fontName, _currentFontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, _currentDIP); textFormat.TextAlignment = textAlign; textFormat.ParagraphAlignment = paragraphAlign; var textLayout = new TextLayout(FactoryDWrite, text, textFormat, dimensions.Width, dimensions.Height); var boundingRect = new RectangleF(); // Loop through all the lines so we can find our drawing offsets var textMetrics = textLayout.Metrics; var lineCount = textMetrics.LineCount; // early out if something went wrong somewhere and nothing is to be drawn if (lineCount == 0) { return(new CCTexture2D()); } // Fill out the bounding rect width and height so we can calculate the yOffset later if needed boundingRect.X = 0; boundingRect.Y = 0; boundingRect.Width = textMetrics.Width; boundingRect.Height = textMetrics.Height; if (!layoutAvailable) { if (dimensions.Width == 8388608) { dimensions.Width = boundingRect.Width; } if (dimensions.Height == 8388608) { dimensions.Height = boundingRect.Height; } } imageWidth = (int)dimensions.Width; imageHeight = (int)dimensions.Height; // Recreate our layout based on calculated dimensions so that we can draw the text correctly // in our image when Alignment is not Left. if (textAlign != TextAlignment.Leading) { textLayout.MaxWidth = dimensions.Width; textLayout.MaxHeight = dimensions.Height; } // Line alignment var yOffset = (CCVerticalTextAlignment.Bottom == verticleAlignement || boundingRect.Bottom >= dimensions.Height) ? dimensions.Height - boundingRect.Bottom // align to bottom : (CCVerticalTextAlignment.Top == verticleAlignement) ? 0 // align to top : (imageHeight - boundingRect.Bottom) * 0.5f; // align to center SharpDX.WIC.Bitmap sharpBitmap = null; WicRenderTarget sharpRenderTarget = null; SolidColorBrush solidBrush = null; try { // Select our pixel format var pixelFormat = SharpDX.WIC.PixelFormat.Format32bppPRGBA; // create our backing bitmap sharpBitmap = new SharpDX.WIC.Bitmap(FactoryImaging, imageWidth, imageHeight, pixelFormat, BitmapCreateCacheOption.CacheOnLoad); // Create the render target that we will draw to sharpRenderTarget = new WicRenderTarget(Factory2D, sharpBitmap, new RenderTargetProperties()); // Create our brush to actually draw with solidBrush = new SolidColorBrush(sharpRenderTarget, foregroundColor); // Begin the drawing sharpRenderTarget.BeginDraw(); if (textDefinition.isShouldAntialias) { sharpRenderTarget.AntialiasMode = AntialiasMode.Aliased; } // Clear it sharpRenderTarget.Clear(TransparentColor); // Draw the text to the bitmap sharpRenderTarget.DrawTextLayout(new Vector2(boundingRect.X, yOffset), textLayout, solidBrush); // End our drawing which will commit the rendertarget to the bitmap sharpRenderTarget.EndDraw(); // Debugging purposes //var s = "Label4"; //SaveToFile(@"C:\Xamarin\" + s + ".png", _bitmap, _renderTarget); // The following code creates a .png stream in memory of our Bitmap and uses it to create our Textue2D Texture2D tex = null; using (var memStream = new MemoryStream()) { using (var encoder = new PngBitmapEncoder(FactoryImaging, memStream)) using (var frameEncoder = new BitmapFrameEncode(encoder)) { frameEncoder.Initialize(); frameEncoder.WriteSource(sharpBitmap); frameEncoder.Commit(); encoder.Commit(); } // Create the Texture2D from the png stream tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, memStream); } // Return our new CCTexture2D created from the Texture2D which will have our text drawn on it. return(new CCTexture2D(tex)); } catch (Exception exc) { CCLog.Log("CCLabel-Windows: Unable to create the backing image of our text. Message: {0}", exc.StackTrace); } finally { if (sharpBitmap != null) { sharpBitmap.Dispose(); sharpBitmap = null; } if (sharpRenderTarget != null) { sharpRenderTarget.Dispose(); sharpRenderTarget = null; } if (solidBrush != null) { solidBrush.Dispose(); solidBrush = null; } if (textFormat != null) { textFormat.Dispose(); textFormat = null; } if (textLayout != null) { textLayout.Dispose(); textLayout = null; } } // If we have reached here then something has gone wrong. return(new CCTexture2D()); }