-1

My target is to generate image with A5 format and place some text on it. At the beggining i was trying to accomplish that using System.Drawing.Imaging but seems like Xamarin is not able to work with this. Then I found out SkiaSharp library which seems like would be valid to do this job but i am a bit lost here. How can i correctly generate an image, put some text on it and send via Stream. Note that if there is more text that couldn't fit on single A5 next image should be created.

This is what i got so far:

private void CreateBitmapFromText(List<string> texts)
{
     using var surface = SKSurface.Create(width: 640, height: 480, SKColorType.Gray8, SKAlphaType.Premul);
     SKCanvas myCanvas = surface.Canvas;

     // clear the canvas / fill with white
     myCanvas.DrawColor(SKColors.White);

     // set up drawing tools
     using (var paint = new SKPaint())
     {
           paint.TextSize = 64.0f;
           paint.IsAntialias = true;
           paint.Color = new SKColor(0x42, 0x81, 0xA4);
           paint.IsStroke = false;

           // draw the text
           foreach (var text in texts)
           {
               myCanvas.DrawText(text, ??, ??, paint);
           }
      }
}

EDIT: with help of @Jason i did this:

var aaa = ToStream(CreateBitmapFromText(new List<string>() {"asas", "vvvv"}), SKEncodedImageFormat.Png);
_printService.PrintImage(aaa);

public Stream ToStream(SKImage image, SKEncodedImageFormat format)
{
    SKData encoded = image.Encode(format, 90);
    return encoded.AsStream();
}

public SKImage CreateBitmapFromText(List<string> texts)
{
     using var surface = SKSurface.Create(width: 640, height: 480, SKColorType.Gray8, SKAlphaType.Premul);
     SKCanvas myCanvas = surface.Canvas;

     // clear the canvas / fill with white
     myCanvas.DrawColor(SKColors.White);

     // set up drawing tools
     using (var paint = new SKPaint())
     {
           paint.TextSize = 64.0f;
           paint.IsAntialias = true;
           paint.Color = new SKColor(0x42, 0x81, 0xA4);
           paint.IsStroke = false;

           // draw the text
           foreach (var text in texts)
           {
               myCanvas.DrawText(text, 0.0f, 0.0f, paint);
           }
     }

     return surface.Snapshot();
}

When i send it to printer i see this (white paper with some little black chars ?? at the top?)

enter image description here

Arie
  • 3,041
  • 7
  • 32
  • 63
  • 1
    what exactly do you need help with? You appear to already be creating a canvas and drawing text on it. There are plenty of existing questions and docs about saving a canvas as a jpg or png. – Jason Mar 27 '21 at 23:53
  • @Jason Initialy i wanted to use SystemDrawing, but you adviced to use SkiSharp as mentioned library doesnt work in xamarin so i follow up and now here i am with this canvas.. TO be honest i really even didnt kow what is canvas and whether its possible to save it as img. Generally does my approach fine? My target simply is to write some text on image (a5 format) and send as stream (if there is more text which couldnt fit on single a5 next image should be created etc.. All inside Xamarin. Can you point me out generally. – Arie Mar 28 '21 at 20:18
  • 1
    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/text – Jason Mar 28 '21 at 20:20
  • @Jason Thanks, however still cannot find how to convert my canvas to image. Can you help? – Arie Mar 28 '21 at 20:42
  • https://stackoverflow.com/questions/59392658/skiasharp-export-canvas-to-image – Jason Mar 28 '21 at 20:44
  • @Jason yes, i saw this but my method has myCanvas which is of type SkCanvas and in this link it requires surface (SKSurface). – Arie Mar 28 '21 at 20:51
  • @Jason i mean as you see i have already a surface create din my code, but then myCanvas is created, should i then creat another surface or? I am mixed a bit – Arie Mar 28 '21 at 20:56
  • 1
    if you already have an SKSurface then what is the problem? – Jason Mar 28 '21 at 21:02
  • @Jason lok at my code, that means 'myCanvsas' from my code is written into surface and this surface i can pass to method from the link? – Arie Mar 28 '21 at 21:04
  • 1
    why don't you just try it and see if it works? – Jason Mar 28 '21 at 21:06
  • @Jason I've tested - plz check my main post in EDIT section. after all i see that. What am i doing wrong? – Arie Mar 28 '21 at 22:05
  • @Jason I've tested - plz check my main post in EDIT section. after all i see that. What am i doing wrong? – Arie Mar 28 '21 at 22:22
  • @Jason hmm i am just thinking i am am only intrested about to display text, have you heard of: Forms9Patch ? Seems like its free and ready to be used. What you think is it worth? Have you heard about it? – Arie Mar 28 '21 at 22:43

1 Answers1

1

I was able to easily modify the Xamarin FramedText sample to save a bitmap by adding this to the end of OnCanvasViewPaintSurface

// get the SKImage from SKSurface
var image = surface.Snapshot();
// Encode as PNG, returns SKData
var data = image.Encode(SKEncodedImageFormat.Png, 100);
string path = Path.GetTempFileName();
// write byte[] to file, verify data with image viewer
File.WriteAllBytes(path, data.ToArray());

you should be able to easily modify this to return a byte[] or stream

Jason
  • 86,222
  • 15
  • 131
  • 146