How to convert text to image using Win2D in a UWP Desktop application?

126 Views Asked by At

I have a UWP Desktop application in which I need to transform a few lines of text into an image and then use that image in other operations. How do I do this? Any help is most welcome. Thanks.

    private Image TextToImage(string text, float width, float height)
    {
        CanvasDevice device = CanvasDevice.GetSharedDevice();
        CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, width, height, 96);
        using (var ds = renderTarget.CreateDrawingSession())
        {
            ds.Clear(Colors.White);

            using (var format = new CanvasTextFormat())
            {
                format.FontFamily = "Segoe UI";
                format.FontSize = 18;
                format.WordWrapping = CanvasWordWrapping.Wrap;
                format.TrimmingGranularity = CanvasTextTrimmingGranularity.None;
                format.HorizontalAlignment = CanvasHorizontalAlignment.Left;
                ds.DrawText(text, 0, 0, Colors.Black, format);
            }

        }

        return ?????;
    }
0

There are 0 best solutions below