using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace vcsFONT { public partial class Form1 : Form { PictureBox thePicBox; string str = "WANtaro 0123456789 日本語"; public Form1() { InitializeComponent(); } //--------------------------------------------------------------------------------- private void Form1_Load(object sender, EventArgs e) { this.Width = 400; pictureBox1.Visible = true; pictureBox2.Visible = false; pictureBox3.Visible = false; foreach (FontFamily ff in FontFamily.Families) { if (ff.IsStyleAvailable(FontStyle.Regular)) { if(ff.Name.IndexOf("@")==-1)listBox1.Items.Add(ff.Name); } } listBox1.SelectedIndex = 0; } //--------------------------------------------------------------------------------- private void button1_Click_1(object sender, EventArgs e) { Bitmap bmp; Graphics g; FontFamily ff; Font f; SizeF TextSize; thePicBox = pictureBox1; bmp = new Bitmap(thePicBox.Width, thePicBox.Height); thePicBox.Image = bmp; g = Graphics.FromImage(thePicBox.Image); ff = new FontFamily(listBox1.SelectedItem.ToString()); f = new Font(ff, 14); g.FillRectangle(Brushes.White, 0, 0, thePicBox.Width, thePicBox.Height); g.DrawString(str, f, Brushes.Black, 3, 3); TextSize = g.MeasureString(str, f); if (35 < TextSize.Height) { MessageBox.Show("文字列寸法が大きすぎです\n"+TextSize.Width.ToString("0")+"×"+TextSize.Height.ToString("0")); } f.Dispose(); ff.Dispose(); g.Dispose(); } //--------------------------------------------------------------------------------- private void toolStripButton1_Click(object sender, EventArgs e) { int kpt = 1; int HSIZE = 400; int VSIZE = 200; Bitmap bmp; Graphics g; FontFamily ff; Font f; int i; string dir = ""; string fnameW; SizeF TextSize; int[] Vsave = new int[listBox1.Items.Count]; int[] Hsave = new int[listBox1.Items.Count]; Bitmap bmpsave; Rectangle rectsave; Graphics gsave; PictureBox saveBox = pictureBox3; int dd = 6; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { dir = folderBrowserDialog1.SelectedPath; } thePicBox = pictureBox2; thePicBox.Size = new Size(kpt * HSIZE, kpt * VSIZE); bmp = new Bitmap(kpt * thePicBox.Width, kpt * thePicBox.Height); thePicBox.Image = bmp; g = Graphics.FromImage(thePicBox.Image); for (i = 0; i <= listBox1.Items.Count - 1; i++) { ff = new FontFamily(listBox1.Items[i].ToString()); f = new Font(ff, 14); g.FillRectangle(Brushes.White, new Rectangle(0, 0, kpt * HSIZE, kpt * VSIZE)); g.DrawString(str, f, Brushes.Black, 3, 3); TextSize = g.MeasureString(str, f); Vsave[i] = (int)TextSize.Height+dd; Hsave[i] = (int)TextSize.Width+dd; rectsave = new Rectangle(0, 0, Hsave[i], Vsave[i]); saveBox.Size = new Size(Hsave[i], Vsave[i]); bmpsave = new Bitmap(rectsave.Width, rectsave.Height); saveBox.Image = bmpsave; gsave = Graphics.FromImage(saveBox.Image); gsave.DrawImage(thePicBox.Image,0,0,rectsave,GraphicsUnit.Pixel); Console.WriteLine(listBox1.Items[i].ToString() + " " + TextSize.Width.ToString("0") + " " + TextSize.Height.ToString("0")); fnameW = dir + "\\png_fnt_" + i.ToString() + ".png"; saveBox.Image.Save(fnameW, System.Drawing.Imaging.ImageFormat.Png); gsave.Dispose(); f.Dispose(); ff.Dispose(); } g.Dispose(); //htmlファイル出力 System.IO.StreamWriter sw; string[] fntname = new string[listBox1.Items.Count]; string[] pngname = new string[listBox1.Items.Count]; fnameW = dir + "\\subFONT.html"; sw = new System.IO.StreamWriter(fnameW, false, System.Text.Encoding.GetEncoding("shift-jis")); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("

VB & C# Font

"); //******************************************************************* sw.WriteLine("

フォント描画サンプル(14ポイントで描画)

"); for (i = 0; i <= listBox1.Items.Count - 1; i++) { pngname[i] = "png_fnt_" + i.ToString() + ".png"; fntname[i] = listBox1.Items[i].ToString(); } sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); for (i = 0; i <= listBox1.Items.Count - 1; i++) { sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine(""); } sw.WriteLine("
"); sw.WriteLine("フォント"); sw.WriteLine(""); sw.WriteLine("png 画像(文字列寸法+ 6 pixel)"); sw.WriteLine(""); sw.WriteLine("文字列寸法(Pixel)"); sw.WriteLine("
"); sw.WriteLine(fntname[i]); sw.WriteLine(""); sw.WriteLine("\"""); sw.WriteLine(""); sw.WriteLine((Hsave[i]-dd).ToString("0") + "×" + (Vsave[i]-dd).ToString("0") ); sw.WriteLine("
"); //******************************************************************* sw.WriteLine(""); sw.WriteLine(""); sw.Close(); //******************************************************************* System.Threading.Thread.Sleep(1000); //1秒待つ System.Diagnostics.Process.Start(fnameW); } //--------------------------------------------------------------------------------- } }