Skip to content

Instantly share code, notes, and snippets.

@azechi
Created February 21, 2013 07:10
Show Gist options
  • Save azechi/5155a9ead72e5c1bc350 to your computer and use it in GitHub Desktop.
Save azechi/5155a9ead72e5c1bc350 to your computer and use it in GitHub Desktop.
指定した文字を入れた画像ファイルを作る
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace TextImage
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 5 || args.Any(x => x == "-?")) {
Console.Error.Write(@"
TextImage.exe width height text filename fileformat
TextImage.exe 600 600 ""sample image"" ""d:\desktop\sample.png"" png
fileformat : jpg | png | gif | bmp
");
return;
}
var width = int.Parse(args[0]);
var height = int.Parse(args[1]);
var text = args[2];
var filename = args[3];
var f = args[4];
ImageFormat format = f == "jpg" ? ImageFormat.Jpeg
: f == "png" ? ImageFormat.Png
: f == "gif" ? ImageFormat.Gif
: f == "bmp" ? ImageFormat.Bmp
: ImageFormat.Jpeg
;
using (var bitmap = new Bitmap(width, height))
using (var g = Graphics.FromImage(bitmap)) {
var font = new Font("Consolas", 18);
g.FillRectangle(Brushes.White, g.VisibleClipBounds);
g.DrawString(text, font, Brushes.Black, 10, 10);
bitmap.Save(filename, format);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment