コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
UIAppFonts Fonts/ERASBD.TTF
using System.Drawing;
using Foundation;
using UIKit;
using Xamarin.Forms;
public class ImageService : IImageService
{
public Stream GetDrawStream(string text, int width, int height)
{
UIImage result = UIImage.FromFile("icon.png");
result = this.GetDrawTextBitmap(result, text);
var stream = result.AsPNG().AsStream();
stream.Position = 0;
return stream;
}
private UIImage GetDrawTextBitmap(UIImage image, string text) { //開始処理 UIGraphics.BeginImageContext(new CGSize(image.Size.Width, image.Size.Height)); CGContext context = UIGraphics.GetCurrentContext(); //元画像の表示 image.Draw(new CGPoint(0, 0)); //表示する文字の設定 NSString contentText = new NSString(text); //フォントと描画サイズの認識 nfloat fontSize = 64f; var font = UIFont.FromName("Eras Bold ITC", fontSize); //ファイル名ではなくフォント名で指定します。 UIStringAttributes attribs = new UIStringAttributes() { Font = font }; //var contentSize = contentText.StringSize(font); var contentSize = contentText.GetSizeUsingAttributes(attribs); //描画エリアを取得する float left = (float)((image.Size.Width - contentSize.Width) / 2); float top = (float)((image.Size.Height - contentSize.Height) / 2); RectangleF rectF = new RectangleF(left, top, (float)contentSize.Width, (float)contentSize.Height); //背景を表示 context.SetFillColor(UIColor.White.CGColor); context.FillRect(rectF); //テキストを表示 context.SetFillColor(UIColor.Blue.CGColor); //文字の色 contentText.DrawString(rectF, font, UILineBreakMode.WordWrap, UITextAlignment.Center); //終了処理 UIImage retImg = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return retImg;
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。