0

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 ?????;
    }
4
  • 1
    Why Win2D? Consider using a RenderTargetBitmap. Commented Aug 4, 2021 at 20:57
  • Converting a Win2D CanvasRenderTarget to a BitmapImage Commented Aug 6, 2021 at 2:53
  • @Clemens, I tried it, but the text was too distorted in the image. Do you have any suggestions for improving the quality? Commented Aug 6, 2021 at 18:35
  • Have you checked @MehrzadChehraz's post? You could CanvasRenderTarget.SaveAsync() method to save it as steam and set it as the source of bitmap image. Commented Aug 10, 2021 at 7:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.