using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Text; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } FontFamily[] ffs; TextBox tb; PlayCard pc; //保存場所はデスクトップ string cardPass = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\card\\"; private void Form1_Load(object sender, EventArgs e) { tb = new TextBox(); tb.Multiline = true; tb.Dock = DockStyle.Fill; tb.ScrollBars = ScrollBars.Both; this.Controls.Add(tb); InstalledFontCollection ifc = new InstalledFontCollection(); ffs = ifc.Families; if (!Directory.Exists(cardPass)) Directory.CreateDirectory(cardPass); } private void Form1_Shown(object sender, EventArgs e) { pc = new PlayCard(); pc.ScaleX = 0.5f; pc.ScaleY = 0.5f; //pc.Outline = false; //Bitmap allbmp = new Bitmap(pc.Size.Width * 14, pc.Size.Height * 4, PixelFormat.Format32bppArgb); for (int k = 0; k < 4; k++) { for (int j = 0; j < 14; j++) { Bitmap bmp = pc.GetPlayCard(k, j); //using (Graphics g = Graphics.FromImage(allbmp)) //{ // g.DrawImage(bmp, new Point(j * pc.Size.Width, k * pc.Size.Height)); //} string viewtext = cardPass + pc.CardInitial[k] + (j + 1).ToString("00") + ".png"; bmp.Save(viewtext, ImageFormat.Png); tb.AppendText(viewtext + "\r\n"); Application.DoEvents(); } } //allbmp.Save(cardPass + "allcard.png", ImageFormat.Png); tb.AppendText("end"); } } class PlayCard : PlayCardMark { public Color BackgroundColor { get; set; } public Color FillColor { get; set; } public PixelFormat PixelFormat { get; set; } public bool Outline { get; set; } public int OutlineWidth { get; set; } public Color OutlineColor { get; set; } public string[] CardInitial = new string[] { "S","D","C","H" }; public PlayCard() { this.PixelFormat = PixelFormat.Format32bppArgb; this.Outline = true; this.OutlineWidth = 1; this.OutlineColor = Color.Black; this.FontName = "MS Pゴシック"; this.FillColor = Color.White; } public Bitmap GetPlayCard(int Type, int CardNumber) { Bitmap bmp = new Bitmap((int)((float)this.Size.Width * this.ScaleX), (int)((float)(this.Size.Height * this.ScaleY)), this.PixelFormat); using (Graphics g = Graphics.FromImage(bmp)) { g.SmoothingMode = SmoothingMode.HighQuality; //念のため透明色で塗る g.FillRectangle(new SolidBrush(BackgroundColor), 0, 0, bmp.Width, bmp.Height); //カード作成 ModelManager mm = new ModelManager(); mm.AddRectangleArc(new RectangleF(0.0f, 0.0f, 199.0f, 299.0f), 10.0f); Matrix m = new Matrix(); m.Scale(this.ScaleX, this.ScaleY); GraphicsPath gp = mm.GetModelPath(m); g.FillPath(new SolidBrush(this.FillColor), gp); //カードの輪郭 if (this.Outline == true) g.DrawPath(new Pen(this.OutlineColor, this.OutlineWidth), gp); //カードの中味 gp = this.GetMarkPath(Type, CardNumber); g.FillPath(this.MarkColor[Type], gp); } return bmp; } } public class PlayCardMark { const int hosei_x = -11; const int hosei_y = -11; public string FontName { get; set; } public float ScaleX { get; set; } public float ScaleY { get; set; } public Size Size { get; set; } public Brush[] MarkColor = new Brush[] { Brushes.Black, Brushes.Crimson, Brushes.Black, Brushes.Crimson, }; public string[] Mark = new string[] { "♠","♦","♣","♥" }; public Point[] Point = new Point[] { new Point(hosei_x + 25,hosei_y + 25), new Point(hosei_x + 25,hosei_y + 87), new Point(hosei_x + 25,hosei_y + 125), new Point(hosei_x + 125,hosei_y + 25), new Point(hosei_x + 125,hosei_y + 87), new Point(hosei_x + 125,hosei_y + 125),//5 new Point(hosei_x + 75,hosei_y + 75), new Point(hosei_x + 75,hosei_y + 125), new Point(hosei_x + 75,hosei_y + 25), new Point(hosei_x + 75,hosei_y + 67), }; public Size MarkSize; string[] cardnumber = new string[] { "A","2","3","4","5","6","7","8","9","10","J","Q","K","JOKER" }; int[] cardnumberw = new int[] { 1,1,1,1,1, 1,1,1,1,-5, 1,1,1,1 }; public PlayCardMark() { this.Size = new Size(200, 300); } private void GetLeftUpMark(GraphicsPath gp, int Type, int CardNumber) { //カード左上のマークと数字を描画 //右下は回転+平行移動で対応 gp.AddString(this.cardnumber[CardNumber], new FontFamily(this.FontName), 0, 30, new Point(this.cardnumberw[CardNumber], 0), StringFormat.GenericDefault); if (CardNumber < 13) gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 30, new Point(2, 30), StringFormat.GenericDefault); Matrix m = new Matrix(); m.Scale(0.8f, 1.0f); gp.Transform(m); } private void GetUpperMark(GraphicsPath gp, int Type, int CardNumber) { //A~10までの中のマークを描画 //上半分共通部分だけ処理 //下半分は上を回転+平行移動で対応 switch ((CardNumber + 1)) { case 2: case 3: gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, this.Point[8], StringFormat.GenericDefault); break; case 4: case 5: case 6: case 7: case 8: gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[0], StringFormat.GenericDefault); gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[3], StringFormat.GenericDefault); if ((CardNumber + 1) == 8) { gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[6], StringFormat.GenericDefault); } break; case 9: case 10: gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[0], StringFormat.GenericDefault); gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[1], StringFormat.GenericDefault); gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[3], StringFormat.GenericDefault); gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[4], StringFormat.GenericDefault); if ((CardNumber + 1) == 10) { gp.AddString(this.Mark[Type], new FontFamily(this.FontName), 0, 70, Point[9], StringFormat.GenericDefault); } break; default: break; }; } public GraphicsPath GetMarkPath(int Type, int CardNumber) { ModelManager mm = new ModelManager(); //回転で対応可能なマークの処理 //上半分 GraphicsPath gp1 = new GraphicsPath(); this.GetLeftUpMark(gp1, Type, CardNumber); this.GetUpperMark(gp1, Type, CardNumber); mm.AddPath(gp1); //下半分 GraphicsPath gp2 = new GraphicsPath(); this.GetLeftUpMark(gp2, Type, CardNumber); this.GetUpperMark(gp2, Type, CardNumber); Matrix m = new Matrix(); m.Rotate(180f); gp2.Transform(m); m.Reset(); m.Translate((float)(this.Size.Width - 1), (float)(this.Size.Height - 1)); gp2.Transform(m); mm.AddPath(gp2); //回転で対応できないマークの処理 switch ((CardNumber + 1)) { case 1: case 3: case 5: case 9: mm.AddString(this.Mark[Type], this.FontName, 0, 70, Point[7], StringFormat.GenericDefault); break; case 6: case 7: case 8: mm.AddString(this.Mark[Type], this.FontName, 0, 70, Point[2], StringFormat.GenericDefault); mm.AddString(this.Mark[Type], this.FontName, 0, 70, Point[5], StringFormat.GenericDefault); if ((CardNumber + 1) == 7) { mm.AddString(this.Mark[Type], this.FontName, 0, 70, Point[6], StringFormat.GenericDefault); } break; default: break; }; m.Reset(); m.Scale(this.ScaleX, this.ScaleY); return mm.GetModelPath(m); } } public class ModelManager { public Matrix Matrix { get; set; } public List Model { get; set; } public ModelManager() { this.Model = new List(); this.Matrix = new Matrix(); } public void AddVectorModel(VectorModel vm) { this.Model.Add(vm); } public void AddLine(int StartX, int StartY, int EndX, int EndY) { this.AddLine(new Point(StartX, StartY), new Point(EndX, EndY)); } public void AddLine(Point[] Point) { VectorModel vm = new VectorModel(); vm.Type = 0; vm.Point = Point; this.AddVectorModel(vm); } public void AddLine(Point Start, Point End) { Point[] Point = new Point[2]; Point[0] = Start; Point[1] = End; this.AddLine(Point); } public void AddLines(Point[] Point) { VectorModel vm = new VectorModel(); vm.Type = 1; vm.Point = Point; this.AddVectorModel(vm); } public void AddArc(RectangleF RectangleF, float StartAngle, float SweepAngle) { VectorModel vm = new VectorModel(); vm.Type = 2; vm.RectangleF = RectangleF; vm.StartAngle = StartAngle; vm.SweepAngle = SweepAngle; this.AddVectorModel(vm); } public void AddArc(int StartX, int StartY, int Width, int Height, int StartAngle, int SweepAngle) { this.AddArc(new RectangleF((float)StartX, (float)StartY, (float)Width, (float)Height), (float)StartAngle, (float)SweepAngle); } public void AddArc(float StartX, float StartY, float Width, float Height, float StartAngle, float SweepAngle) { this.AddArc(new RectangleF(StartX, StartY, Width, Height), StartAngle, SweepAngle); } public void AddString(String Text, string FontName, int FontStyle, float FontemSize, Point Point, StringFormat StringFormat) { VectorModel vm = new VectorModel(); vm.Type = 3; vm.Text = Text; vm.FontName = FontName; vm.FontStyle = FontStyle; vm.FontemSize = FontemSize; vm.Point = new Point[] { Point }; vm.StringFormat = StringFormat; this.AddVectorModel(vm); } public void AddRectangle(RectangleF RectangleF) { VectorModel vm = new VectorModel(); vm.Type = 4; vm.RectangleF = RectangleF; this.AddVectorModel(vm); } public void AddRectangle(int StartX, int StartY, int Width, int Height) { this.AddRectangle(new RectangleF((float)StartX, (float)StartY, (float)Width, (float)Height)); } public void AddRectangle(float StartX, float StartY, float Width, float Height) { this.AddRectangle(new RectangleF(StartX, StartY, Width, Height)); } public void AddRectangleArc(RectangleF RectangleF, float Radius) { VectorModel vm = new VectorModel(); if (Radius < 1.0f) { //半径が1以下の場合は丸み無しに切り替え this.AddRectangle(RectangleF); } else { vm.Type = 5; vm.RectangleF = RectangleF; vm.Radius = Radius; this.AddVectorModel(vm); } } public void AddRectangleArc(int StartX, int StartY, int Width, int Height, int Radius) { this.AddRectangleArc(new RectangleF((float)StartX, (float)StartY, (float)Width, (float)Height), (float)Radius); } public void AddRectangleArc(float StartX, float StartY, float Width, float Height, float Radius) { this.AddRectangleArc(new RectangleF(StartX, StartY, Width, Height), Radius); } public void AddPath(GraphicsPath Path) { //if (Path.PointCount == 0) return; VectorModel vm = new VectorModel(); vm.Type = 6; vm.Path = Path; this.AddVectorModel(vm); } public void Clear() { this.Model.Clear(); } public GraphicsPath GetModelPath(Matrix m) { GraphicsPath gp = new GraphicsPath(); foreach (VectorModel vm in this.Model) { vm.GetPath(gp); } gp.Transform(m); return gp; } public GraphicsPath GetModelPath() { //GraphicsPath gp = this.GetModelPath(this.Matrix); //return gp; return this.GetModelPath(this.Matrix); } } public class VectorModel { const float SwAngle = 90.0f; const float S000Angel = 0.0f; const float S090Angel = 90.0f; const float S180Angel = 180.0f; const float S270Angel = 270.0f; //public VectorModel Parent { get; set; } //public VectorModel Child { get; set; } //public VectorModel Next { get; set; } public int Type { get; set; } public Point[] Point { get; set; } public RectangleF RectangleF { get; set; } public float StartAngle { get; set; } public float SweepAngle { get; set; } public string Text { get; set; } public string FontName { get; set; } public int FontStyle { get; set; } public float FontemSize { get; set; } public StringFormat StringFormat { get; set; } public GraphicsPath Path { get; set; } public float Radius { get; set; } public float[] StAngel = new float[] { S180Angel, S270Angel, S000Angel, S090Angel, }; public void GetPath(GraphicsPath gp) { //if (this.Child != null) Child.GetPath(gp); switch (this.Type) { case 0: gp.AddLine(this.Point[0], this.Point[1]); break; case 1: gp.AddLines(this.Point); break; case 2: gp.AddArc(this.RectangleF, this.StartAngle, this.SweepAngle); break; case 3: gp.AddString(this.Text, new FontFamily(this.FontName), this.FontStyle, this.FontemSize, Point[0], this.StringFormat); break; case 4: gp.AddRectangle(this.RectangleF); break; case 5: int xs = (int)this.RectangleF.Location.X; int ys = (int)this.RectangleF.Location.Y; int xe = (int)this.RectangleF.Width; int ye = (int)this.RectangleF.Height; int rad = (int)(this.Radius); int diameter = rad * 2; Point[] sp = new Point[]{ new Point(xs + rad, ys), new Point(xe, ys + rad), new Point(xe - rad, ye), new Point(xs, ye - rad), }; Point[] ep = new Point[]{ new Point(xe - rad, ys), new Point(xe, ye - rad), new Point(xs + rad, ye), new Point(xs, ys + rad), }; Rectangle[] r = new Rectangle[]{ new Rectangle(xs, ys, diameter, diameter), new Rectangle(xe - diameter, ys, diameter, diameter), new Rectangle(xe - diameter, ye - diameter, diameter, diameter), new Rectangle(xs, ye - diameter, diameter, diameter), }; for (int i = 0; i < 4; i++) { gp.AddArc(r[i], this.StAngel[i], SwAngle); gp.AddLine(sp[i], ep[i]); } break; case 6: gp.AddPath(this.Path, false); break; default: break; } //if (this.Next != null) Next.GetPath(gp); return; } } }