public SharpDX.Direct2D1.Bitmap1 RenderLabel(
            float imageWidth,
            float imageHeight,
            Color4 foregroundColor,
            Vector2 origin,
            TextLayout textLayout,
            float dpi = DEFAULT_DPI,
            SharpDX.DXGI.Format format        = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            SharpDX.Direct2D1.AlphaMode alpha = AlphaMode.Premultiplied)
        {
            var renderTarget = CreateRenderTarget(imageWidth, imageHeight, dpi, format, alpha);

            using (var drawingContext = CreateDrawingContext(renderTarget))
            {
                // Begin our drawing
                drawingContext.BeginDraw();

                // Clear to transparent
                drawingContext.Clear(TransparentColor);

                // Create our brush to actually draw with
                var solidBrush = new SolidColorBrush(drawingContext, foregroundColor);
                // Draw the text to the bitmap
                drawingContext.DrawTextLayout(origin, textLayout, solidBrush);

                // End our drawing
                drawingContext.EndDraw();
            }

            return(renderTarget);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new texture description for a <see cref="BitmapTarget" />.
        /// </summary>
        /// <param name="dpiX">The width.</param>
        /// <param name="dpiY">The height.</param>
        /// <param name="format">Describes the format to use.</param>
        /// <param name="alphaMode">The <see cref="AlphaMode"/>.</param>
        /// <param name="options">Sets the bitmap options.</param>
        /// <param name="colorContext">The <see cref="ColorContext"/>.</param>
        /// <returns>A new instance of <see cref="BitmapTarget" /> class.</returns>
        public static BitmapProperties1 CreateDescription(float dpiX, float dpiY, PixelFormat format,
                                                          AlphaMode alphaMode   = AlphaMode.Premultiplied,
                                                          BitmapOptions options = BitmapOptions.Target | BitmapOptions.CannotDraw, ColorContext colorContext = null)
        {
            // Make sure that the texture to create is a render target
            options |= BitmapOptions.Target;
            var description = NewDescription(dpiX, dpiY, format, alphaMode, options, colorContext);

            return(description);
        }
        public SharpDX.Direct2D1.Bitmap1 CreateRenderTarget(
            float width,
            float height,
            float dpi = DEFAULT_DPI,
            SharpDX.DXGI.Format format        = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            SharpDX.Direct2D1.AlphaMode alpha = AlphaMode.Premultiplied)
        {
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1()
            {
                BitmapOptions = BitmapOptions.Target,
                DpiX          = dpi,
                DpiY          = dpi,
                PixelFormat   = new SharpDX.Direct2D1.PixelFormat(format, alpha)
            };

            var pixelWidth  = SizeDipsToPixels(width, dpi);
            var pixelHeight = SizeDipsToPixels(height, dpi);

            var bitmap = new SharpDX.Direct2D1.Bitmap1(d2dResourceCreationDeviceContext,
                                                       new Size2(pixelWidth, pixelHeight), bitmapProperties);

            return(bitmap);
        }
        public async Task <System.IO.MemoryStream> RenderLabelToStream(
            float imageWidth,
            float imageHeight,
            Color4 foregroundColor,
            Vector2 origin,
            TextLayout textLayout,
            float dpi = DEFAULT_DPI,
            SharpDX.DXGI.Format format        = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
            SharpDX.Direct2D1.AlphaMode alpha = AlphaMode.Premultiplied)
        {
            // Get stream
            var pngStream = new MemoryStream();

            using (var renderTarget = RenderLabel(
                       imageWidth,
                       imageHeight,
                       foregroundColor,
                       origin,
                       textLayout,
                       dpi,
                       format,
                       alpha))
            {
                pngStream.Position = 0;

                // Create a WIC outputstream
                using (var wicStream = new WICStream(FactoryImaging, pngStream))
                {
                    var size = renderTarget.PixelSize;

                    // Initialize a Png encoder with this stream
                    using (var wicBitmapEncoder = new PngBitmapEncoder(FactoryImaging, wicStream))
                    {
                        // Create a Frame encoder
                        using (var wicFrameEncoder = new BitmapFrameEncode(wicBitmapEncoder))
                        {
                            wicFrameEncoder.Initialize();

                            // Create image encoder
                            ImageEncoder    wicImageEncoder;
                            ImagingFactory2 factory2 = new ImagingFactory2();
                            wicImageEncoder = new ImageEncoder(factory2, D2DDevice);


                            var imgParams = new ImageParameters();
                            imgParams.PixelFormat =
                                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                                  AlphaMode.Premultiplied);

                            imgParams.PixelHeight = (int)size.Height;
                            imgParams.PixelWidth  = (int)size.Width;

                            wicImageEncoder.WriteFrame(renderTarget, wicFrameEncoder, imgParams);

                            //// Commit changes
                            wicFrameEncoder.Commit();
                            wicBitmapEncoder.Commit();

                            byte[] buffer = new byte[pngStream.Length];
                            pngStream.Position = 0;
                            await pngStream.ReadAsync(buffer, 0, (int)pngStream.Length);
                        }
                    }
                }
            }

            return(pngStream);
        }
Beispiel #5
0
        protected static BitmapProperties1 NewDescription(float dpiX, float dpiY, PixelFormat format, AlphaMode alphaMode,
                                                          BitmapOptions bitmapOptions, ColorContext colorContext)
        {
            var pixelFormat = new SharpDX.Direct2D1.PixelFormat(format, alphaMode);

            return(new BitmapProperties1(pixelFormat, dpiX, dpiY, bitmapOptions, colorContext));
        }