「SkiaSharpで日本語文字列を描画する」の版間の差分
imported>Administrator 編集の要約なし |
|||
| 15行目: | 15行目: | ||
== 実装例 == | == 実装例 == | ||
<source lang="csharp"> | <source lang="csharp"> | ||
// まず描画先のキャンバスを作る | |||
SKBitmap bitmap = new SKBitmap(512, 512, isOpaque: false); | SKBitmap bitmap = new SKBitmap(512, 512, isOpaque: false); | ||
SKCanvas canvas = new SKCanvas(bitmap); | SKCanvas canvas = new SKCanvas(bitmap); | ||
// | // キャンバスを白く塗りつぶす | ||
canvas.Clear(SKColors.White); | canvas.Clear(SKColors.White); | ||
var paint = new SKPaint(); | var paint = new SKPaint(); | ||
// | // フォントを明示的に設定する(日本語の描画時は必須) | ||
paint.Typeface = SKTypeface.FromFile(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "ipag.ttf")); | paint.Typeface = SKTypeface.FromFile(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "ipag.ttf")); | ||
paint.TextSize = 64; | paint.TextSize = 64; | ||
| 28行目: | 29行目: | ||
// 文字列を描画 | // 文字列を描画 | ||
// | // 描画位置の指定は「ベースライン」な点に注意すること | ||
var text = "日本語"; | var text = "日本語"; | ||
var location = new SKPoint(100, 500); | var location = new SKPoint(100, 500); | ||
| 49行目: | 50行目: | ||
canvas.Flush(); | canvas.Flush(); | ||
// | // おまけ:キャンバスをファイルに保存 | ||
using (var image = SKImage.FromBitmap(bitmap)) | using (var image = SKImage.FromBitmap(bitmap)) | ||
using (var file = File.Create("/tmp/test.png")) | using (var file = File.Create("/tmp/test.png")) | ||