canvas (SkiaSharp) 中的 DrawText:文本不显示
DrawText in canvas (SkiaSharp) : Text does not display
我的问题是:canvas
上不显示文本
SKPaint _paint = new SKPaint();
using(Stream stream = _assembly.GetManifestResourceStream(fontPath))
{
_paint.Typeface = SKTypeface.FromStream(stream);
}
canvas.DrawText ("12345",X,Y,_paint);
如果我不使用 "using block" 或 "dispose",它就像一个魅力。
但是如果我这样做,在达到 canvas.DrawText 之前,_paint 不会为空,但 FontMetrics、FontSpacing 或 TextSize 中的所有值都等于 0。
然后 canvas 上没有文字。
我很困惑,你能给我一些建议吗?
谢谢。
发生这种情况的原因是您正在处理流,然后尝试使用它。 SKTypeface
获得流的所有权,您不得自行关闭它。一旦字体不再使用,它可能会决定关闭流。
正确的使用方法是打开流,将其传递给 SKTypeface
,然后只关心字体:
SKPaint _paint = new SKPaint();
Stream stream = _assembly.GetManifestResourceStream(fontPath);
_paint.Typeface = SKTypeface.FromStream(stream);
canvas.DrawText ("12345", X, Y,_paint);
Returns a new typeface given a stream. Ownership of the stream is transferred, so the caller must not reference it again.
我的问题是:canvas
上不显示文本SKPaint _paint = new SKPaint();
using(Stream stream = _assembly.GetManifestResourceStream(fontPath))
{
_paint.Typeface = SKTypeface.FromStream(stream);
}
canvas.DrawText ("12345",X,Y,_paint);
如果我不使用 "using block" 或 "dispose",它就像一个魅力。
但是如果我这样做,在达到 canvas.DrawText 之前,_paint 不会为空,但 FontMetrics、FontSpacing 或 TextSize 中的所有值都等于 0。
然后 canvas 上没有文字。
我很困惑,你能给我一些建议吗?
谢谢。
发生这种情况的原因是您正在处理流,然后尝试使用它。 SKTypeface
获得流的所有权,您不得自行关闭它。一旦字体不再使用,它可能会决定关闭流。
正确的使用方法是打开流,将其传递给 SKTypeface
,然后只关心字体:
SKPaint _paint = new SKPaint();
Stream stream = _assembly.GetManifestResourceStream(fontPath);
_paint.Typeface = SKTypeface.FromStream(stream);
canvas.DrawText ("12345", X, Y,_paint);
Returns a new typeface given a stream. Ownership of the stream is transferred, so the caller must not reference it again.