diff --git a/Box.cs b/Box.cs new file mode 100644 index 0000000..800e167 --- /dev/null +++ b/Box.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; + +namespace CardEditor +{ + /// + /// 框对象 + /// + public class Box + { + public string Name { get; set; } + public decimal RectX1 { get; set; } + public decimal RectX2 { get; set; } + public decimal RectY1 { get; set; } + public decimal RectY2 { get; set; } + public string Font { get; set; } + public decimal FontSize { get; set; } + public Color Color { get; set; } + public TextAlign Flag { get; set; } + public string Pic { get; set; } + public bool TextOnly => "仅文字无图".Equals(Pic); + + /// + /// 将框的保存格式转换为 Box 对象 + /// + /// Box 对象 + public static Box Parse(string line) + { + var split = line.Split(','); + + if (split.Length == 10) + { + return new Box + { + Name = split[0], + RectX1 = int.Parse(split[1]), + RectY1 = int.Parse(split[2]), + RectX2 = int.Parse(split[3]), + RectY2 = int.Parse(split[4]), + Font = split[5], + FontSize = decimal.Parse(split[6]), + Color = ColorTranslator.FromHtml("#" + split[7]), + Flag = (TextAlign) int.Parse(split[8]), + Pic = split[9] + }; + } + else + { + return null; + } + } + + /// + /// 将 Box 对象转换为框的保存格式 + /// + /// 框的保存格式 + public string Serialize() => + $"{Name},{RectX1},{RectY1},{RectX2},{RectY2},{Font},{FontSize},{Color.ToArgb():X8},{(int) Flag},{Pic}"; + + /// + /// 在指定的 Image 上绘制对应的内容 + /// + /// 要绘制的内容:文本框的文字,图像的路径 + /// 要在其上绘制的 Image + /// 是否显示框区域 + /// + public void Draw(string content, Image target, bool drawBack, bool isPreview = true) + { + var font = new Font(Font ?? "黑体", (float) FontSize); + var sf = new StringFormat((StringFormatFlags) Flag); + var fontBrush = new SolidBrush(Color); + var backBrush = new SolidBrush(Color.FromArgb(100, 255 - Color.R, 255 - Color.G, 255 - Color.B)); + var g = Graphics.FromImage(target); + var rect = RectangleF.FromLTRB((float) RectX1, (float) RectY1, (float) RectX2, (float) RectY2); + + if (drawBack) + { + g.FillRectangle(backBrush, rect); + } + + if (!TextOnly) + { + var path = "从绝对路径".Equals(Pic) ? content : Path.Combine(Properties.Settings.Default.srcpicdir, content); + var bitmap = isPreview ? Properties.Resources.defaultimg1 : Image.FromFile(path); + g.DrawImage(bitmap, rect); + } + + if (TextOnly || isPreview) + { + g.DrawString(content, font, fontBrush, rect, sf); + } + + g.Dispose(); + } + } +} \ No newline at end of file diff --git a/CardEditor.csproj b/CardEditor.csproj index 930091b..c232f7c 100644 --- a/CardEditor.csproj +++ b/CardEditor.csproj @@ -85,14 +85,18 @@ + + Form Form1.cs + + Form1.cs diff --git a/EnumHelper.cs b/EnumHelper.cs new file mode 100644 index 0000000..951a11e --- /dev/null +++ b/EnumHelper.cs @@ -0,0 +1,29 @@ +using System; +using System.ComponentModel; +using System.Linq; + +namespace CardEditor +{ + public static class EnumHelper + { + public class ValueWrapper + { + public T Value { get; set; } + public string Display { get; set; } + } + + public static ValueWrapper WrapEnum(this T enumValue) where T : Enum + { + return new ValueWrapper {Value = enumValue, Display = enumValue.GetEnumDescription()}; + } + + public static string GetEnumDescription(this Enum enumValue) + { + var name = enumValue.ToString(); + var type = enumValue.GetType(); + return type.GetMember(name).FirstOrDefault() != null + ? $"{(Attribute.GetCustomAttribute(type.GetField(name), typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description}" + : "string.Empty"; + } + } +} \ No newline at end of file diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 40d1152..3068507 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -1,4 +1,6 @@  +using System.Drawing; + namespace CardEditor { partial class Form1 @@ -32,16 +34,6 @@ private void InitializeComponent() System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.dgv_boxesdata = new System.Windows.Forms.DataGridView(); - this.boxname = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.rectx1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.recty1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.rectx2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.recty2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.font = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.fontsize = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.color = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.flag = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.pic = new System.Windows.Forms.DataGridViewComboBoxColumn(); this.btn_loadbase = new System.Windows.Forms.Button(); this.tb_boxname = new System.Windows.Forms.TextBox(); this.l_boxname = new System.Windows.Forms.Label(); @@ -100,6 +92,17 @@ private void InitializeComponent() this.btn_choosepicfolder = new System.Windows.Forms.Button(); this.btn_opensavefolder = new System.Windows.Forms.Button(); this.label17 = new System.Windows.Forms.Label(); + this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.boxname = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.rectx1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.recty1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.rectx2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.recty2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.font = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.fontsize = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.color = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.flag = new System.Windows.Forms.DataGridViewComboBoxColumn(); + this.pic = new System.Windows.Forms.DataGridViewComboBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dgv_boxesdata)).BeginInit(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pBmainview)).BeginInit(); @@ -155,89 +158,7 @@ private void InitializeComponent() this.dgv_boxesdata.TabIndex = 1; this.dgv_boxesdata.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_boxesdata_CellClick); this.dgv_boxesdata.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_boxesdata_CellEndEdit); - // - // boxname - // - this.boxname.HeaderText = "名称"; - this.boxname.MinimumWidth = 8; - this.boxname.Name = "boxname"; - this.boxname.Width = 70; - // - // rectx1 - // - this.rectx1.HeaderText = "x1"; - this.rectx1.MinimumWidth = 8; - this.rectx1.Name = "rectx1"; - this.rectx1.Width = 50; - // - // recty1 - // - this.recty1.HeaderText = "y1"; - this.recty1.MinimumWidth = 8; - this.recty1.Name = "recty1"; - this.recty1.Width = 50; - // - // rectx2 - // - this.rectx2.HeaderText = "x2"; - this.rectx2.MinimumWidth = 8; - this.rectx2.Name = "rectx2"; - this.rectx2.Width = 50; - // - // recty2 - // - this.recty2.HeaderText = "y2"; - this.recty2.MinimumWidth = 8; - this.recty2.Name = "recty2"; - this.recty2.Width = 50; - // - // font - // - this.font.HeaderText = "字体"; - this.font.MinimumWidth = 8; - this.font.Name = "font"; - this.font.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.font.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.font.Width = 50; - // - // fontsize - // - this.fontsize.HeaderText = "字体大小"; - this.fontsize.MinimumWidth = 8; - this.fontsize.Name = "fontsize"; - this.fontsize.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.fontsize.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.fontsize.Width = 70; - // - // color - // - this.color.HeaderText = "颜色"; - this.color.MinimumWidth = 8; - this.color.Name = "color"; - this.color.ReadOnly = true; - this.color.Resizable = System.Windows.Forms.DataGridViewTriState.False; - this.color.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.color.Width = 50; - // - // flag - // - this.flag.HeaderText = "对齐"; - this.flag.MinimumWidth = 8; - this.flag.Name = "flag"; - this.flag.Width = 40; - // - // pic - // - this.pic.HeaderText = "图片引用方式"; - this.pic.Items.AddRange(new object[] { - "仅文字无图", - "从绝对路径", - "从相对路径"}); - this.pic.MinimumWidth = 6; - this.pic.Name = "pic"; - this.pic.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.pic.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; - this.pic.Width = 110; + this.dgv_boxesdata.RowPrePaint += new System.Windows.Forms.DataGridViewRowPrePaintEventHandler(this.dgv_boxesdata_RowPrePaint); // // btn_loadbase // @@ -877,20 +798,16 @@ private void InitializeComponent() // // cb_flag // + this.cb_flag.DisplayMember = "Display"; this.cb_flag.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cb_flag.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.cb_flag.FormattingEnabled = true; - this.cb_flag.Items.AddRange(new object[] { - "居左0", - "居右1", - "竖左2", - "竖右3", - "整行4096"}); this.cb_flag.Location = new System.Drawing.Point(57, 90); this.cb_flag.Margin = new System.Windows.Forms.Padding(4); this.cb_flag.Name = "cb_flag"; this.cb_flag.Size = new System.Drawing.Size(189, 23); this.cb_flag.TabIndex = 6; + this.cb_flag.ValueMember = "Value"; // // label10 // @@ -994,6 +911,113 @@ private void InitializeComponent() "1+原高)"; this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // + // dataGridViewTextBoxColumn1 + // + this.dataGridViewTextBoxColumn1.DataPropertyName = "Flag"; + this.dataGridViewTextBoxColumn1.HeaderText = "对齐"; + this.dataGridViewTextBoxColumn1.MinimumWidth = 8; + this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1"; + this.dataGridViewTextBoxColumn1.Width = 40; + // + // boxname + // + this.boxname.DataPropertyName = "Name"; + this.boxname.HeaderText = "名称"; + this.boxname.MinimumWidth = 8; + this.boxname.Name = "boxname"; + this.boxname.Width = 70; + // + // rectx1 + // + this.rectx1.DataPropertyName = "RectX1"; + this.rectx1.HeaderText = "x1"; + this.rectx1.MinimumWidth = 8; + this.rectx1.Name = "rectx1"; + this.rectx1.Width = 50; + // + // recty1 + // + this.recty1.DataPropertyName = "RectY1"; + this.recty1.HeaderText = "y1"; + this.recty1.MinimumWidth = 8; + this.recty1.Name = "recty1"; + this.recty1.Width = 50; + // + // rectx2 + // + this.rectx2.DataPropertyName = "RectX2"; + this.rectx2.HeaderText = "x2"; + this.rectx2.MinimumWidth = 8; + this.rectx2.Name = "rectx2"; + this.rectx2.Width = 50; + // + // recty2 + // + this.recty2.DataPropertyName = "RectY2"; + this.recty2.HeaderText = "y2"; + this.recty2.MinimumWidth = 8; + this.recty2.Name = "recty2"; + this.recty2.Width = 50; + // + // font + // + this.font.DataPropertyName = "Font"; + this.font.HeaderText = "字体"; + this.font.MinimumWidth = 8; + this.font.Name = "font"; + this.font.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.font.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.font.Width = 50; + // + // fontsize + // + this.fontsize.DataPropertyName = "FontSize"; + this.fontsize.HeaderText = "字体大小"; + this.fontsize.MinimumWidth = 8; + this.fontsize.Name = "fontsize"; + this.fontsize.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.fontsize.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.fontsize.Width = 70; + // + // color + // + this.color.DataPropertyName = "Color"; + this.color.DefaultCellStyle.ForeColor = Color.Transparent; + this.color.DefaultCellStyle.SelectionForeColor = Color.Transparent; + this.color.HeaderText = "颜色"; + this.color.MinimumWidth = 8; + this.color.Name = "color"; + this.color.ReadOnly = true; + this.color.Resizable = System.Windows.Forms.DataGridViewTriState.False; + this.color.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.color.Width = 50; + // + // flag + // + this.flag.DataPropertyName = "Flag"; + this.flag.HeaderText = "对齐"; + this.flag.MinimumWidth = 8; + this.flag.Name = "flag"; + this.flag.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.flag.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; + this.flag.Width = 60; + this.flag.DisplayMember = "Display"; + this.flag.ValueMember = "Value"; + // + // pic + // + this.pic.DataPropertyName = "Pic"; + this.pic.HeaderText = "图片引用方式"; + this.pic.Items.AddRange(new object[] { + "仅文字无图", + "从绝对路径", + "从相对路径"}); + this.pic.MinimumWidth = 6; + this.pic.Name = "pic"; + this.pic.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.pic.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; + this.pic.Width = 110; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); @@ -1105,6 +1129,9 @@ private void InitializeComponent() private System.Windows.Forms.NumericUpDown nUD_recty2; private System.Windows.Forms.NumericUpDown nUD_rectx2; private System.Windows.Forms.NumericUpDown nUD_rectx1; + private System.Windows.Forms.Button btn_opensavefolder; + private System.Windows.Forms.Label label17; + private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1; private System.Windows.Forms.DataGridViewTextBoxColumn boxname; private System.Windows.Forms.DataGridViewTextBoxColumn rectx1; private System.Windows.Forms.DataGridViewTextBoxColumn recty1; @@ -1113,10 +1140,8 @@ private void InitializeComponent() private System.Windows.Forms.DataGridViewTextBoxColumn font; private System.Windows.Forms.DataGridViewTextBoxColumn fontsize; private System.Windows.Forms.DataGridViewTextBoxColumn color; - private System.Windows.Forms.DataGridViewTextBoxColumn flag; + private System.Windows.Forms.DataGridViewComboBoxColumn flag; private System.Windows.Forms.DataGridViewComboBoxColumn pic; - private System.Windows.Forms.Button btn_opensavefolder; - private System.Windows.Forms.Label label17; } } diff --git a/Form1.cs b/Form1.cs index cf7aba4..3ec8303 100644 --- a/Form1.cs +++ b/Form1.cs @@ -1,748 +1,553 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Diagnostics; -using System.Drawing; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading; -using System.Windows.Forms; - - -namespace CardEditor -{ - public partial class Form1 : Form - { - //Stopwatch Stopwatch= new Stopwatch();//花里胡哨的的选择框alpha周期缓动(bushi - public bool Isdrawing; - public Image SourceImage; - //public bool IsFirst;//换拖动选取了 - public bool IsShowBack; - private System.Drawing.Text.InstalledFontCollection ObjFont = new System.Drawing.Text.InstalledFontCollection(); - int Last_X , Last_Y = 0;//用于存储光标在预览中映射的最终坐标 - int Down_X, Down_Y, Up_X, Up_Y = 0;//存储鼠标在预览中点击和抬起时映射的坐标 - bool IsMouseDown = false; - bool IsResizing = false;//修复启动时窗体还未完全加载时调用l_size报错的问题 - - public Form1() - { - //初始化设置 - InitializeComponent(); - SourceImage = Properties.Resources._1; - l_size.Text = "预览比例(" + (pBmainview.Height * 1.0 / pBmainview.Width).ToString("N2") + ")可能与原图(" + (SourceImage.Height * 1.0 / SourceImage.Width).ToString("N2") + ")不一致\n但不影响输出情况(如何调整比例?拉伸窗口!)"; - if (Directory.Exists(Properties.Settings.Default.savefolder)) tb_outdir.Text = Properties.Settings.Default.savefolder; - if (Directory.Exists(Properties.Settings.Default.srcpicdir)) tb_srcpicfolder.Text = Properties.Settings.Default.srcpicdir; - if (File.Exists(Properties.Settings.Default.inputtxtdir)) tb_textdir.Text = Properties.Settings.Default.inputtxtdir; - //允许跨线程传参 - CheckForIllegalCrossThreadCalls = false; - - //IsFirst = true; - - //读取系统字库初始化字体选择下拉框 - foreach(System.Drawing.FontFamily i in ObjFont.Families) - { - cb_font.Items.Add(i.Name.ToString()); - } - cb_font.SelectedIndex = 0; - cb_picsrcfrom.SelectedIndex = 0; - cb_flag.SelectedIndex = 0; - //dgv_boxesdata.Columns["font"].DataPropertyName - } - - private void btn_loadbase_Click(object sender, EventArgs e) - { - //加载底图函数 - OpenFileDialog dialog = new OpenFileDialog(); - dialog.Multiselect = false; - dialog.Title = "请选择文件"; - dialog.Filter = "Png(*.png)|*.png"; - if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) - { - FileStream fileStream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read); - int byteLength = (int)fileStream.Length; - byte[] fileBytes = new byte[byteLength]; - fileStream.Read(fileBytes, 0, byteLength); - fileStream.Close();//关闭文件流,解除锁定 - - SourceImage = Image.FromStream(new MemoryStream(fileBytes));//流转Image类 - //SourceImage = Image.FromFile(dialog.FileName); - RefreshView(); - //标点底图右下角坐标 - l_xyplus.Text = SourceImage.Width + "," + SourceImage.Height; - } - } - - //刷新底图预览(&一行样例)函数 - private void RefreshView() - { - int rx1,rx2,ry1,ry2; - Bitmap TempImg = new Bitmap(SourceImage); - tb_shouldlike.Text = " "; - foreach (DataGridViewRow row in dgv_boxesdata.Rows) - { - tb_shouldlike.Text += "'" +row.Cells["boxname"].Value.ToString() + "',"; - rx1 = int.Parse(row.Cells["rectx1"].Value.ToString()); - rx2 = int.Parse(row.Cells["rectx2"].Value.ToString()); - ry1 = int.Parse(row.Cells["recty1"].Value.ToString()); - ry2 = int.Parse(row.Cells["recty2"].Value.ToString()); - if (row.Cells["pic"].Value.ToString() == "仅文字无图") - { - - string tfontname = "黑体";//默认字体 - if (row.Cells["font"].Value != null)//判断防止引用空报错 - tfontname = row.Cells["font"].Value.ToString(); - DrawPic("(" + row.Cells["boxname"].Value + ")the quick brown fox jumps over a lazy dog.the quick brown fox jumps over a lazy dog.the quick brown fox jumps over a lazy dog.", new Rectangle(Math.Min(rx1, rx2), Math.Min(ry1, ry2), Math.Abs(rx2-rx1), Math.Abs(ry2-ry1)), tfontname, float.Parse(row.Cells["fontsize"].Value.ToString()), TempImg, row.Cells["color"].Style.BackColor, true, int.Parse(row.Cells["flag"].Value.ToString()), null); - - } - else - { - //针对图片框显示预览图(品红/绿) - if(IsShowBack) DrawPic(string.Empty, new Rectangle(Math.Min(rx1, rx2), Math.Min(ry1, ry2), Math.Abs(rx2 - rx1), Math.Abs(ry2 - ry1)), row.Cells["font"].Value.ToString(), float.Parse(row.Cells["fontsize"].Value.ToString()), TempImg, row.Cells["color"].Style.BackColor, false, int.Parse(row.Cells["flag"].Value.ToString()), Properties.Resources.defaultimg1); - else DrawPic(string.Empty, new Rectangle(Math.Min(rx1, rx2), Math.Min(ry1, ry2), Math.Abs(rx2 - rx1), Math.Abs(ry2 - ry1)), row.Cells["font"].Value.ToString(), float.Parse(row.Cells["fontsize"].Value.ToString()), TempImg, row.Cells["color"].Style.BackColor, false, int.Parse(row.Cells["flag"].Value.ToString()),null); - - } - - } - tb_shouldlike.Text = tb_shouldlike.Text.Substring(0, tb_shouldlike.Text.Length - 1);//刷新一行预览 - pBmainview.Image = TempImg; - //TempImg.Dispose(); - } - - - //添加框函数 - private void btn_addbox_Click(object sender, EventArgs e) - { - //原点坐标+长宽确定矩形,已改为两点坐标确定矩形 - int index = dgv_boxesdata.Rows.Count; - int Flagint;//用于判断对齐 - dgv_boxesdata.Rows.Add(); - dgv_boxesdata.Rows[index].HeaderCell.Value = index + 1; - dgv_boxesdata.Rows[index].Cells["boxname"].Value = tb_boxname.Text; - - dgv_boxesdata.Rows[index].Cells["rectx1"].Value = nUD_rectx1.Value; - dgv_boxesdata.Rows[index].Cells["recty1"].Value = nUD_recty1.Value; - dgv_boxesdata.Rows[index].Cells["rectx2"].Value = nUD_rectx2.Value; - dgv_boxesdata.Rows[index].Cells["recty2"].Value = nUD_recty2.Value; - - dgv_boxesdata.Rows[index].Cells["font"].Value = cb_font.Text; - dgv_boxesdata.Rows[index].Cells["fontsize"].Value = nUD_fontsize.Value; - dgv_boxesdata.Rows[index].Cells["color"].Style.BackColor = colorshow.BackColor; - - dgv_boxesdata.Rows[index].Cells["pic"].Value = cb_picsrcfrom.SelectedItem; - if (cb_picsrcfrom.Text != "仅文字无图") - { - dgv_boxesdata.Rows[index].Cells["font"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[index].Cells["fontsize"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[index].Cells["flag"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[index].Cells["font"].Value = string.Empty; - dgv_boxesdata.Rows[index].Cells["fontsize"].Value = 1; - dgv_boxesdata.Rows[index].Cells["color"].Style.BackColor = Color.Black; - - } - if (cb_flag.Text == "居左0") - Flagint = 0; - else if (cb_flag.Text == "居右1") - Flagint = 1; - else if (cb_flag.Text == "竖左2") - Flagint = 2; - else if (cb_flag.Text == "竖右3") - Flagint = 3; - else - Flagint = 4096; - dgv_boxesdata.Rows[index].Cells["flag"].Value = Flagint; - RefreshView(); - } - - private void DrawPic(string text,Rectangle rectangle,string fontname,float fontsize,Image drawimage,Color color,bool drawback,int sfflag,Image img) - { - if (fontname == string.Empty) fontname = "黑体"; - Font font = new Font(fontname,fontsize); - StringFormat sf = new StringFormat((StringFormatFlags)sfflag); - Brush fontbrush = new SolidBrush(color); - Brush backbrush = new SolidBrush(Color.FromArgb(100,255-color.R,255-color.G,255-color.B)); - Graphics g = Graphics.FromImage(drawimage); - if (drawback&&IsShowBack) - g.FillRectangle(backbrush,rectangle); - if(img != null) - g.DrawImage(img, rectangle); - g.DrawString(text, font, fontbrush, rectangle, sf); - g.Dispose(); - - } - - private void dgv_boxesdata_CellEndEdit(object sender, DataGridViewCellEventArgs e) - { - - if (dgv_boxesdata.Rows[e.RowIndex].Cells["pic"].Value.ToString() == "仅文字无图") - { - dgv_boxesdata.Rows[e.RowIndex].Cells["font"].Style.BackColor = Color.White; - dgv_boxesdata.Rows[e.RowIndex].Cells["fontsize"].Style.BackColor = Color.White; - dgv_boxesdata.Rows[e.RowIndex].Cells["flag"].Style.BackColor = Color.White; - } - else - { - dgv_boxesdata.Rows[e.RowIndex].Cells["font"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[e.RowIndex].Cells["fontsize"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[e.RowIndex].Cells["flag"].Style.BackColor = Color.Gray; - - } - RefreshView(); - } - - //选择输出文件夹函数 - private void btn_choosefolder_Click(object sender, EventArgs e) - { - FolderBrowserDialog dialog = new FolderBrowserDialog(); - dialog.Description = "请选择文件路径"; - dialog.SelectedPath = Properties.Settings.Default.savefolder; - //dialog.RootFolder = Environment.SpecialFolder.Programs; - if (dialog.ShowDialog() == DialogResult.OK) - { - string foldPath = dialog.SelectedPath + "\\"; - Properties.Settings.Default.savefolder = foldPath; - Properties.Settings.Default.Save(); - tb_outdir.Text = foldPath; - } - } - - //选择输入参数函数 - private void btn_choosefile_Click(object sender, EventArgs e) - { - OpenFileDialog dialog = new OpenFileDialog(); - dialog.Multiselect = false;//该值确定是否可以选择多个文件 - dialog.Title = "请选择文件"; - dialog.Filter = "有逗号分隔的文本文档(*.txt)|*.txt|逗号分隔文件(*.csv)|*.csv"; - if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) - { - string filePath = dialog.FileName; - Properties.Settings.Default.inputtxtdir = filePath; - Properties.Settings.Default.Save(); - tb_textdir.Text = filePath; - - - } - } - - //开始绘制线程函数 - private void btn_startdraw_Click(object sender, EventArgs e) - { - Thread t1 = new Thread(new ThreadStart(DrawProcess)); - btn_startdraw.Enabled = false; - t1.IsBackground=true; - t1.Start(); - } - - //绘制独立线程,防止程序假死 - public void DrawProcess() - { - - int LineId = 0; - int BoxId = 0; - string Mode = string.Empty; - string PicSrc = string.Empty; - List drawlist = null; - string text = string.Empty; - string FontName = string.Empty; - float FontSize = 0; - Color DrawColor= Color.White; - int flag = 0; - Rectangle myRectangle= new Rectangle(0,0,0,0); - Image image; - string e = Properties.Settings.Default.srcpicdir; - if (!File.Exists(Properties.Settings.Default.inputtxtdir)) - { - MessageBox.Show("没有文件输入!"); - btn_startdraw.Enabled = true; - return; - } - try - { - foreach (string str in File.ReadAllLines(Properties.Settings.Default.inputtxtdir, Encoding.UTF8)) - { - int rx1, rx2, ry1, ry2; - LineId++; - Bitmap TempImg = new Bitmap(SourceImage); - drawlist = new List(str.Split(',')); - //if (drawlist.Count == dgv_boxesdata.Rows.Count) - //{ - BoxId = 0; - foreach (DataGridViewRow row in dgv_boxesdata.Rows) - { - //原点坐标+长宽确定矩形,已改为两点坐标确定矩形 - rx1 = int.Parse(row.Cells["rectx1"].Value.ToString()); - rx2 = int.Parse(row.Cells["rectx2"].Value.ToString()); - ry1 = int.Parse(row.Cells["recty1"].Value.ToString()); - ry2 = int.Parse(row.Cells["recty2"].Value.ToString()); - - BoxId++; - myRectangle = new Rectangle(Math.Min(rx1, rx2), Math.Min(ry1, ry2), Math.Abs(rx2 - rx1), Math.Abs(ry2 - ry1)); - FontName = row.Cells["font"].Value.ToString(); - FontSize = float.Parse(row.Cells["fontsize"].Value.ToString()); - DrawColor = row.Cells["color"].Style.BackColor; - flag = int.Parse(row.Cells["flag"].Value.ToString()); - text = string.Empty; - if (row.Cells["pic"].Value.ToString() == "仅文字无图") - { - Mode = row.Cells["pic"].Value.ToString(); - PicSrc = "no source"; - text = drawlist[row.Index]; - image = null; - } - else if (row.Cells["pic"].Value.ToString() == "从绝对路径") - { - Mode = row.Cells["pic"].Value.ToString(); - PicSrc = drawlist[row.Index]; - image = Image.FromFile(PicSrc); - } - else - { - if (!Directory.Exists(e)) - e += "---"; - - Mode = row.Cells["pic"].Value.ToString(); - PicSrc = Properties.Settings.Default.srcpicdir + drawlist[row.Index]; - image = Image.FromFile(PicSrc); - } - - DrawPic(text, myRectangle, FontName, FontSize, TempImg, DrawColor, false, flag, image); - - - } - TempImg.Save(Properties.Settings.Default.savefolder + drawlist[0] + ".png"); - btn_startdraw.Enabled = true; - - - - } - - - } - - catch (Exception ex) - { - string a = dgv_boxesdata.Rows.Count.ToString(); - string b = drawlist.Count.ToString(); - string c = Properties.Settings.Default.inputtxtdir; - string d = PicSrc; - if (dgv_boxesdata.Rows.Count != drawlist.Count) - { - a += "---"; - b += "---"; - } - if (!File.Exists(c)) - c += "---"; - if(!File.Exists(d)) - d += "---"; - - string errormessage = "出现绘制错误,已退出绘制!\n!!!!!!!(错误内容以---显示)!!!!!!!\n======**======\n尝试绘制行:" + LineId + "\n尝试绘制文本框:" + BoxId + "\n文本框数:" + a + "\n行分隔字段数:" + b + "\n输出:" + Properties.Settings.Default.savefolder + "\n输入文件:" + c + "\n图像来源:\n模式:" + Mode + "\n相对路径:" + e + "\n完整路径:" + d + "\n======**======\n常见解决方案:\n检查输入文件和图像来源是否存在\n检查输入文件的尝试绘制行中分隔字段数是否与当前文本框数相同\n======**======\n高级错误信息,汇报请上传截图:" + ex; - MessageBox.Show(errormessage); - - } - btn_startdraw.Enabled = true; - - - } - - private void colorshow_Click(object sender, EventArgs e) - { - ColorDialog colorDia = new ColorDialog(); - - if (colorDia.ShowDialog() == DialogResult.OK) - { - //获取所选择的颜色 - Color colorChoosed = colorDia.Color; - //改变panel的背景色 - colorshow.BackColor = colorChoosed; - - } - - } - - public void pBmainview_DrawArea_get(object sender, MouseEventArgs e) - { - //Stopwatch.Start(); - IsMouseDown = true; - Down_X = Last_X; - Down_Y = Last_Y; - } - private void pBmainview_MouseUp(object sender, MouseEventArgs e) - { - //Stopwatch.Stop(); - //Stopwatch.Reset(); - IsMouseDown = false; - Up_X = Last_X; - Up_Y = Last_Y; - RefreshView(); - - nUD_rectx1.Value = Math.Min(Down_X, Up_X); - nUD_recty1.Value = Math.Min(Down_Y, Up_Y); - nUD_rectx2.Value = Math.Max(Down_X, Up_X); - nUD_recty2.Value = Math.Max(Down_Y, Up_Y); - - } - - private void pBmainview_MouseMove(object sender, MouseEventArgs e) - { - - double wd = pBmainview.Width; - double ht = pBmainview.Height; - Last_X = (int)(SourceImage.Width * (e.X / wd)); - Last_Y = (int)(SourceImage.Height * (e.Y / ht)); - l_pos.Text = "truepixel(" + Last_X + "," + Last_Y + ")"; - - if (IsMouseDown) - { - Bitmap TempImg = new Bitmap(SourceImage); - Graphics g = Graphics.FromImage(TempImg); - //LDebugger.Text = ((int)Math.Abs((Math.Sin(Stopwatch.ElapsedMilliseconds * 0.001) * 100))).ToString(); - //Brush br = new SolidBrush(Color.FromArgb((int)(Math.Sin(Stopwatch.ElapsedMilliseconds * 0.001) * 25)+100, 255, 200, 0)); - Brush br = new SolidBrush(Color.FromArgb(100, 255, 200, 0)); - g.FillRectangle(br, new Rectangle(Math.Min(Down_X,Last_X), Math.Min(Down_Y,Last_Y),Math.Abs(Last_X-Down_X), Math.Abs(Last_Y -Down_Y))); - pBmainview.Image = TempImg; - - } - - } - /*private void pBmainview_MouseDown(object sender, MouseEventArgs e) - { - double wd = pBmainview.Width; - double ht = pBmainview.Height; - int x = (int)(SourceImage.Width * (e.X / wd)); - int y = (int)(SourceImage.Height * (e.Y / ht)); - if (IsFirst) - { - nUD_rectx1.Value = x; - nUD_recty1.Value = y; - //tb_rectx1.Text = x.ToString(); - //tb_recty1.Text = y.ToString(); - - IsFirst = false; - } - else - { - int x1 = (int)nUD_rectx1.Value; - int y1 = (int)nUD_recty1.Value; - //int x1 = int.Parse(tb_rectx1.Text); - //int y1 = int.Parse(tb_recty1.Text); - if (x1 != null && x >= x1 && y1 != null && y >= y1) - { - nUD_rectx2.Value = x - x1; - nUD_recty2.Value = y - y1; - //tb_width.Text = (x - x1).ToString(); - //tb_height.Text = (y - y1).ToString(); - IsFirst = true; - } - - - } - }*/ - - private void dgv_boxesdata_CellClick(object sender, DataGridViewCellEventArgs e) - { - if (dgv_boxesdata.RowCount == 0) - return; - if(dgv_boxesdata.CurrentCell.ColumnIndex == 7) - { - ColorDialog colorDia = new ColorDialog(); - - if (colorDia.ShowDialog() == DialogResult.OK) - { - //获取所选择的颜色 - Color colorChoosed = colorDia.Color; - //改变panel的背景色 - dgv_boxesdata.CurrentCell.Style.BackColor = colorChoosed; - RefreshView(); - } - - } - - } - - private void label8_Click(object sender, EventArgs e) - { - Process.Start("https://github.com/POPCORNBOOM"); - } - - private void checkBox1_CheckedChanged(object sender, EventArgs e) - { - if(IsShowBack) - { - IsShowBack = false; - RefreshView(); - } - else - { - IsShowBack = true; - RefreshView(); - } - } - - - - private void pBmainview_MouseLeave(object sender, EventArgs e) - { - l_pos.Text = "truepixel(0,0)"; - } - - - - public bool islegal(string validateString) - { - try - { - int.Parse(validateString); - return true; - } - catch - { - return false; - } - } - - private void btn_savecfg_Click(object sender, EventArgs e) - { - SaveFileDialog sfd = new SaveFileDialog(); - sfd.Filter = "框集合BoxSet(*.bs)|*.bs"; - sfd.FilterIndex = 1; - sfd.RestoreDirectory = true; - if (dgv_boxesdata.RowCount == 0) - { - MessageBox.Show("集合为空!"); - return; - } - if (sfd.ShowDialog() == DialogResult.OK) - { - string filename = sfd.FileName.ToString(); - StreamWriter sw = new StreamWriter(filename); - foreach (DataGridViewRow row in dgv_boxesdata.Rows) - { - sw.WriteLine(row.Cells["boxname"].Value + ","+ row.Cells["rectx1"].Value + "," + row.Cells["recty1"].Value + "," + row.Cells["rectx2"].Value + "," + row.Cells["recty2"].Value + "," + row.Cells["font"].Value + "," + row.Cells["fontsize"].Value + "," + row.Cells["color"].Style.BackColor.ToArgb().ToString("X8") + "," + row.Cells["flag"].Value +","+row.Cells["pic"].Value); - } - sw.Close();//关闭 - MessageBox.Show("保存成功"); - } - } - - private void btn_loadcfg_Click(object sender, EventArgs e) - { - OpenFileDialog dialog = new OpenFileDialog(); - dialog.Multiselect = false; - dialog.Title = "请选择文件"; - dialog.Filter = "框集合BoxSet(*.bs,*.tbs)|*.bs;*.tbs"; - if (dialog.ShowDialog() == DialogResult.OK) - { - dgv_boxesdata.Rows.Clear(); - dgv_boxesdata.Refresh(); - int readingline = 0; - string filePath = dialog.FileName; - foreach (string str in System.IO.File.ReadAllLines(filePath)) - { - readingline++; - List textboxrow = new List(str.Split(',')); - - if (textboxrow.Count == 10) - { - dgv_boxesdata.Rows.Add(); - dgv_boxesdata.Rows[readingline - 1].Cells["boxname"].Value = textboxrow[0]; - dgv_boxesdata.Rows[readingline - 1].Cells["rectx1"].Value = textboxrow[1]; - dgv_boxesdata.Rows[readingline - 1].Cells["recty1"].Value = textboxrow[2]; - dgv_boxesdata.Rows[readingline - 1].Cells["rectx2"].Value = textboxrow[3]; - dgv_boxesdata.Rows[readingline - 1].Cells["recty2"].Value = textboxrow[4]; - dgv_boxesdata.Rows[readingline - 1].Cells["font"].Value = textboxrow[5]; - dgv_boxesdata.Rows[readingline - 1].Cells["fontsize"].Value = textboxrow[6]; - dgv_boxesdata.Rows[readingline - 1].Cells["color"].Style.BackColor = ColorTranslator.FromHtml("#" + textboxrow[7]); - dgv_boxesdata.Rows[readingline - 1].Cells["flag"].Value = textboxrow[8]; - dgv_boxesdata.Rows[readingline - 1].Cells["pic"].Value = textboxrow[9]; - if (textboxrow[9] != "仅文字无图") - { - dgv_boxesdata.Rows[readingline - 1].Cells["font"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[readingline - 1].Cells["fontsize"].Style.BackColor = Color.Gray; - dgv_boxesdata.Rows[readingline - 1].Cells["flag"].Style.BackColor = Color.Gray; - - } - - } - else - { - - MessageBox.Show("载入错误:行" + readingline); - break; - } - - - - } - dgv_boxesdata.Refresh(); - RefreshView(); - - - } - - } - private void Form1_SizeChanged(object sender, EventArgs e) - { - - } - private void Form1_ResizeBegin(object sender, EventArgs e) - { - IsResizing = true; - int side; - if (SourceImage.Width > SourceImage.Height) - { - side = SourceImage.Height; - } - else - side = SourceImage.Width; - Bitmap TempImg = new Bitmap(SourceImage); - Graphics g = Graphics.FromImage(TempImg); - Brush br = new SolidBrush(Color.FromArgb(100,255,200,0)); - Brush fontbrush = new SolidBrush(Color.Red); - Font font = new Font("黑体", 50); - StringFormat sf = new StringFormat((StringFormatFlags)0); - g.FillRectangle(br, new Rectangle(0, 0, side, side)); - g.FillRectangle(br, new Rectangle(5, 5, side-5, side-5)); - g.DrawString("我是个正方形,如果看起来不像,调整窗口大小直到我看上去像正方形,或者你也可以对照下面的预览比例和实际比例进行调整", font, fontbrush, new Rectangle(5, 5, side - 5, side - 5), sf); - pBmainview.Image=TempImg; - - - } - - private void Form1_ResizeEnd(object sender, EventArgs e) - { - IsResizing = false; - RefreshView(); - } - - private void btn_report_Click(object sender, EventArgs e) - { - Process.Start("https://popcornboom.github.io/contact/"); - } - - private void btn_del_Click(object sender, EventArgs e) - { - if (dgv_boxesdata.CurrentRow == null) return; - dgv_boxesdata.Rows.Remove(dgv_boxesdata.CurrentRow); - RefreshView(); - } - - private void btn_moveup_Click(object sender, EventArgs e) - { - if (dgv_boxesdata.CurrentRow == null|| dgv_boxesdata.CurrentRow.Index <= 0) return; - int index = dgv_boxesdata.CurrentRow.Index; - DataGridViewRow dgvs = dgv_boxesdata.CurrentRow; - - //dgv_boxesdata.Rows[dgv_boxesdata.CurrentRow.Index-1].Cells - try - { - - DataGridViewRow dgvr = dgv_boxesdata.Rows[index - 1];//获取选中行的上一行 - dgv_boxesdata.Rows.RemoveAt(index - 1);//删除原选中行的上一行 - dgv_boxesdata.Rows.Insert((index), dgvr);//将选中行的上一行插入到选中行的后面 - - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString()); - } - RefreshView(); - - } - - private void pBmainview_Resize(object sender, EventArgs e) - { - if(IsResizing) - l_size.Text = "预览比例(" + (pBmainview.Height * 1.0 / pBmainview.Width).ToString("N2") + ")可能与原图(" + (SourceImage.Height * 1.0 / SourceImage.Width).ToString("N2") + ")不一致\n但不影响输出情况(如何调整比例?拉伸窗口!)"; - - } - - private void btn_opensavefolder_Click(object sender, EventArgs e) - { - Process.Start(Properties.Settings.Default.savefolder); - } - - private void btn_movedown_Click(object sender, EventArgs e) - { - if (dgv_boxesdata.CurrentRow == null || dgv_boxesdata.CurrentRow.Index == dgv_boxesdata.RowCount-1) return; - int index = dgv_boxesdata.CurrentRow.Index; - DataGridViewRow dgvs = dgv_boxesdata.CurrentRow; - - //dgv_boxesdata.Rows[dgv_boxesdata.CurrentRow.Index-1].Cells - try - { - - DataGridViewRow dgvr = dgv_boxesdata.Rows[index+1];//获取选中行的下一行 - dgv_boxesdata.Rows.RemoveAt(index + 1);//删除原选中行的下一行 - dgv_boxesdata.Rows.Insert((index), dgvr);//将选中行的上一行插入到选中行的后面 - - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString()); - } - RefreshView(); - - } - - private void btn_choosepicfolder_Click(object sender, EventArgs e) - { - FolderBrowserDialog dialog = new FolderBrowserDialog(); - dialog.Description = "请选择文件路径"; - dialog.SelectedPath = Properties.Settings.Default.srcpicdir; - //dialog.RootFolder = Environment.SpecialFolder.Programs; - if (dialog.ShowDialog() == DialogResult.OK) - { - string foldPath = dialog.SelectedPath + "\\"; - Properties.Settings.Default.srcpicdir = foldPath; - Properties.Settings.Default.Save(); - tb_srcpicfolder.Text = foldPath; - } - } - - private void cb_picsrcfrom_SelectedIndexChanged(object sender, EventArgs e) - { - if(cb_picsrcfrom.Text == "仅文字无图") - { - gb_text.Enabled = true; - l_boxname.Text = "文本框名称"; - btn_addbox.Text = "新建文本框"; - } - else - { - gb_text.Enabled = false; - l_boxname.Text = "图片框名称"; - btn_addbox.Text = "新建图片框"; - - } - } - - private void btn_duplicate_Click(object sender, EventArgs e) - { - if (dgv_boxesdata.CurrentRow == null) return; - dgv_boxesdata.Rows.Add(); - int id=0; - int index = dgv_boxesdata.CurrentRow.Index; - foreach (DataGridViewCell cell in dgv_boxesdata.CurrentRow.Cells) - { - if(id==7) - dgv_boxesdata.Rows[dgv_boxesdata.RowCount - 1].Cells[id].Style.BackColor = cell.Style.BackColor; - dgv_boxesdata.Rows[dgv_boxesdata.RowCount - 1].Cells[id].Value = cell.Value; - id++; - } - DataGridViewRow dgvs = dgv_boxesdata.CurrentRow; - try - { - DataGridViewRow dgvr = dgv_boxesdata.Rows[dgv_boxesdata.RowCount - 1]; - dgv_boxesdata.Rows.RemoveAt(dgv_boxesdata.RowCount - 1); - dgv_boxesdata.Rows.Insert((index), dgvr); - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString()); - } - RefreshView(); - - } - } -} +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Text; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Windows.Forms; +using CardEditor.Properties; + +// ReSharper disable PossibleNullReferenceException +// ReSharper disable LocalizableElement + +namespace CardEditor +{ + public partial class Form1 : Form + { + private readonly BindingList boxes = new BindingList(); + private int Down_X, Down_Y, Up_X, Up_Y; //存储鼠标在预览中点击和抬起时映射的坐标 + + //Stopwatch Stopwatch= new Stopwatch();//花里胡哨的的选择框alpha周期缓动(bushi + public bool Isdrawing; + private bool IsMouseDown; + + private bool IsResizing; //修复启动时窗体还未完全加载时调用l_size报错的问题 + + //public bool IsFirst;//换拖动选取了 + public bool IsShowBack; + private int Last_X, Last_Y; //用于存储光标在预览中映射的最终坐标 + private readonly InstalledFontCollection ObjFont = new InstalledFontCollection(); + public Image SourceImage; + + public Form1() + { + //初始化设置 + InitializeComponent(); + SourceImage = Resources._1; + l_size.Text = "预览比例(" + (pBmainview.Height * 1.0 / pBmainview.Width).ToString("N2") + ")可能与原图(" + + (SourceImage.Height * 1.0 / SourceImage.Width).ToString("N2") + + ")不一致\n但不影响输出情况(如何调整比例?拉伸窗口!)"; + if (Directory.Exists(Settings.Default.savefolder)) tb_outdir.Text = Settings.Default.savefolder; + if (Directory.Exists(Settings.Default.srcpicdir)) tb_srcpicfolder.Text = Settings.Default.srcpicdir; + if (File.Exists(Settings.Default.inputtxtdir)) tb_textdir.Text = Settings.Default.inputtxtdir; + //允许跨线程传参 + CheckForIllegalCrossThreadCalls = false; + + //IsFirst = true; + + //读取系统字库初始化字体选择下拉框 + foreach (var i in ObjFont.Families) cb_font.Items.Add(i.Name); + cb_font.SelectedIndex = 0; + cb_picsrcfrom.SelectedIndex = 0; + + dgv_boxesdata.DataSource = boxes; + dgv_boxesdata.Columns[nameof(Box.TextOnly)].Visible = false; + + var wrappedFlags = Enum.GetValues(typeof(TextAlign)).Cast() + .Select(ta => ta.WrapEnum()).Cast().ToArray(); + var flagCol = (DataGridViewComboBoxColumn) dgv_boxesdata.Columns["flag"]; + flagCol.Items.AddRange(wrappedFlags); + cb_flag.Items.AddRange(wrappedFlags); + cb_flag.SelectedIndex = 0; + } + + private void btn_loadbase_Click(object sender, EventArgs e) + { + //加载底图函数 + var dialog = new OpenFileDialog(); + dialog.Multiselect = false; + dialog.Title = "请选择文件"; + dialog.Filter = "Png(*.png)|*.png"; + if (dialog.ShowDialog() == DialogResult.OK) + { + using (var fileStream = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read)) + { + SourceImage = Image.FromStream(fileStream); + } + + //SourceImage = Image.FromFile(dialog.FileName); + RefreshView(); + //标点底图右下角坐标 + l_xyplus.Text = SourceImage.Width + "," + SourceImage.Height; + } + } + + //刷新底图预览(&一行样例)函数 + private void RefreshView() + { + var tempImg = new Bitmap(SourceImage); + + tb_shouldlike.Text = string.Join(",", boxes.Select(it => $"'{it.Name}'").ToArray()); //刷新一行预览 + + foreach (var box in boxes) + box.Draw(PlaceHolderText(box.Name), tempImg, IsShowBack); + pBmainview.Image = tempImg; + } + + private static string PlaceHolderText(string name) + { + return + $"({name})the quick brown fox jumps over a lazy dog.the quick brown fox jumps over a lazy dog.the quick brown fox jumps over a lazy dog."; + } + + + //添加框函数 + private void btn_addbox_Click(object sender, EventArgs e) + { + boxes.Add(new Box + { + Name = tb_boxname.Text, + RectX1 = nUD_rectx1.Value, + RectY1 = nUD_recty1.Value, + RectX2 = nUD_rectx2.Value, + RectY2 = nUD_recty2.Value, + Font = cb_font.Text, + FontSize = nUD_fontsize.Value, + Color = colorshow.BackColor, + Pic = cb_picsrcfrom.SelectedItem.ToString(), + Flag = ((EnumHelper.ValueWrapper) cb_flag.SelectedItem).Value + }); + RefreshView(); + } + + private void dgv_boxesdata_CellEndEdit(object sender, DataGridViewCellEventArgs e) + { + if (dgv_boxesdata.Rows[e.RowIndex].Cells["pic"].Value.ToString() == "仅文字无图") + { + dgv_boxesdata.Rows[e.RowIndex].Cells["font"].Style.BackColor = Color.White; + dgv_boxesdata.Rows[e.RowIndex].Cells["fontsize"].Style.BackColor = Color.White; + dgv_boxesdata.Rows[e.RowIndex].Cells["flag"].Style.BackColor = Color.White; + } + else + { + dgv_boxesdata.Rows[e.RowIndex].Cells["font"].Style.BackColor = Color.Gray; + dgv_boxesdata.Rows[e.RowIndex].Cells["fontsize"].Style.BackColor = Color.Gray; + dgv_boxesdata.Rows[e.RowIndex].Cells["flag"].Style.BackColor = Color.Gray; + } + + RefreshView(); + } + + //选择输出文件夹函数 + private void btn_choosefolder_Click(object sender, EventArgs e) + { + var dialog = new FolderBrowserDialog(); + dialog.Description = "请选择文件路径"; + dialog.SelectedPath = Settings.Default.savefolder; + //dialog.RootFolder = Environment.SpecialFolder.Programs; + if (dialog.ShowDialog() == DialogResult.OK) + { + var foldPath = dialog.SelectedPath + "\\"; + Settings.Default.savefolder = foldPath; + Settings.Default.Save(); + tb_outdir.Text = foldPath; + } + } + + //选择输入参数函数 + private void btn_choosefile_Click(object sender, EventArgs e) + { + var dialog = new OpenFileDialog(); + dialog.Multiselect = false; //该值确定是否可以选择多个文件 + dialog.Title = "请选择文件"; + dialog.Filter = "有逗号分隔的文本文档(*.txt)|*.txt|逗号分隔文件(*.csv)|*.csv"; + if (dialog.ShowDialog() == DialogResult.OK) + { + var filePath = dialog.FileName; + Settings.Default.inputtxtdir = filePath; + Settings.Default.Save(); + tb_textdir.Text = filePath; + } + } + + //开始绘制线程函数 + private void btn_startdraw_Click(object sender, EventArgs e) + { + var t1 = new Thread(DrawProcess); + btn_startdraw.Enabled = false; + t1.IsBackground = true; + t1.Start(); + } + + //绘制独立线程,防止程序假死 + public void DrawProcess() + { + var currentLine = 0; + var currentBox = 0; + var mode = string.Empty; + var picSrc = string.Empty; + string[] contents = null; + var e = Settings.Default.srcpicdir; + if (!File.Exists(Settings.Default.inputtxtdir)) + { + MessageBox.Show("没有文件输入!"); + btn_startdraw.Enabled = true; + return; + } + + try + { + foreach (var str in File.ReadAllLines(Settings.Default.inputtxtdir, Encoding.UTF8)) + { + currentLine++; + currentBox = 0; + + var tempImg = new Bitmap(SourceImage); + contents = str.Split(','); + + if (contents.Length < boxes.Count) + { + // TODO: 抛出自定义异常 + MessageBox.Show($"框数和第{currentLine}行分隔字段数不同,跳过"); + break; + } + + for (var i = 0; i < boxes.Count; i++) + { + currentBox = i; + boxes[i].Draw(contents[i], tempImg, false, false); + } + + tempImg.Save(Settings.Default.savefolder + contents[0] + ".png"); + btn_startdraw.Enabled = true; + } + } + + catch (Exception ex) + { + var a = dgv_boxesdata.Rows.Count.ToString(); + var b = contents.Length.ToString(); + var c = Settings.Default.inputtxtdir; + var d = picSrc; + if (dgv_boxesdata.Rows.Count != contents.Length) + { + a += "---"; + b += "---"; + } + + if (!File.Exists(c)) + c += "---"; + if (!File.Exists(d)) + d += "---"; + + var errormessage = "出现绘制错误,已退出绘制!\n!!!!!!!(错误内容以---显示)!!!!!!!\n======**======\n尝试绘制行:" + currentLine + + "\n尝试绘制文本框:" + currentBox + "\n文本框数:" + a + "\n行分隔字段数:" + b + "\n输出:" + + Settings.Default.savefolder + "\n输入文件:" + c + "\n图像来源:\n模式:" + mode + "\n相对路径:" + e + + "\n完整路径:" + d + + "\n======**======\n常见解决方案:\n检查输入文件和图像来源是否存在\n检查输入文件的尝试绘制行中分隔字段数是否与当前文本框数相同\n======**======\n高级错误信息,汇报请上传截图:" + + ex; + MessageBox.Show(errormessage); + } + + btn_startdraw.Enabled = true; + } + + private void colorshow_Click(object sender, EventArgs e) + { + var colorDia = new ColorDialog(); + + if (colorDia.ShowDialog() == DialogResult.OK) + { + //获取所选择的颜色 + var colorChoosed = colorDia.Color; + //改变panel的背景色 + colorshow.BackColor = colorChoosed; + } + } + + public void pBmainview_DrawArea_get(object sender, MouseEventArgs e) + { + //Stopwatch.Start(); + IsMouseDown = true; + Down_X = Last_X; + Down_Y = Last_Y; + } + + private void pBmainview_MouseUp(object sender, MouseEventArgs e) + { + //Stopwatch.Stop(); + //Stopwatch.Reset(); + IsMouseDown = false; + Up_X = Last_X; + Up_Y = Last_Y; + RefreshView(); + + nUD_rectx1.Value = Math.Min(Down_X, Up_X); + nUD_recty1.Value = Math.Min(Down_Y, Up_Y); + nUD_rectx2.Value = Math.Max(Down_X, Up_X); + nUD_recty2.Value = Math.Max(Down_Y, Up_Y); + } + + private void pBmainview_MouseMove(object sender, MouseEventArgs e) + { + double wd = pBmainview.Width; + double ht = pBmainview.Height; + Last_X = (int) (SourceImage.Width * (e.X / wd)); + Last_Y = (int) (SourceImage.Height * (e.Y / ht)); + l_pos.Text = "truepixel(" + Last_X + "," + Last_Y + ")"; + + if (IsMouseDown) + { + var tempImg = new Bitmap(SourceImage); + var g = Graphics.FromImage(tempImg); + //LDebugger.Text = ((int)Math.Abs((Math.Sin(Stopwatch.ElapsedMilliseconds * 0.001) * 100))).ToString(); + //Brush br = new SolidBrush(Color.FromArgb((int)(Math.Sin(Stopwatch.ElapsedMilliseconds * 0.001) * 25)+100, 255, 200, 0)); + Brush br = new SolidBrush(Color.FromArgb(100, 255, 200, 0)); + g.FillRectangle(br, + new Rectangle(Math.Min(Down_X, Last_X), Math.Min(Down_Y, Last_Y), Math.Abs(Last_X - Down_X), + Math.Abs(Last_Y - Down_Y))); + pBmainview.Image = tempImg; + } + } + + private void dgv_boxesdata_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (dgv_boxesdata.RowCount == 0) + return; + if (dgv_boxesdata.CurrentCell.OwningColumn.Name == color.Name) + { + var colorDia = new ColorDialog(); + + if (colorDia.ShowDialog() == DialogResult.OK) + { + //获取所选择的颜色 + var colorChoosed = colorDia.Color; + //改变 Box.Color + (dgv_boxesdata.CurrentRow.DataBoundItem as Box).Color = colorChoosed; + RefreshView(); + } + } + } + + private void label8_Click(object sender, EventArgs e) + { + Process.Start("https://github.com/POPCORNBOOM"); + } + + private void checkBox1_CheckedChanged(object sender, EventArgs e) + { + if (IsShowBack) + { + IsShowBack = false; + RefreshView(); + } + else + { + IsShowBack = true; + RefreshView(); + } + } + + + private void pBmainview_MouseLeave(object sender, EventArgs e) + { + l_pos.Text = "truepixel(0,0)"; + } + + private void btn_savecfg_Click(object sender, EventArgs e) + { + var sfd = new SaveFileDialog(); + sfd.Filter = "框集合BoxSet(*.bs)|*.bs"; + sfd.FilterIndex = 1; + sfd.RestoreDirectory = true; + if (dgv_boxesdata.RowCount == 0) + { + MessageBox.Show("集合为空!"); + return; + } + + if (sfd.ShowDialog() == DialogResult.OK) + { + var filename = sfd.FileName; + using (var sw = new StreamWriter(filename)) + { + foreach (var box in boxes) + sw.WriteLine(box.Serialize()); + } + + MessageBox.Show("保存成功"); + } + } + + private void btn_loadcfg_Click(object sender, EventArgs e) + { + var dialog = new OpenFileDialog(); + dialog.Multiselect = false; + dialog.Title = "请选择文件"; + dialog.Filter = "框集合BoxSet(*.bs,*.tbs)|*.bs;*.tbs"; + if (dialog.ShowDialog() == DialogResult.OK) + { + dgv_boxesdata.Rows.Clear(); + dgv_boxesdata.Refresh(); + var currentLine = 0; + var filePath = dialog.FileName; + boxes.Clear(); + foreach (var box in File.ReadAllLines(filePath).Select(Box.Parse)) + { + currentLine++; + if (box is null) + MessageBox.Show("载入错误:行" + currentLine); + else + boxes.Add(box); + } + + dgv_boxesdata.Refresh(); + RefreshView(); + } + } + + private void Form1_ResizeBegin(object sender, EventArgs e) + { + IsResizing = true; + var side = SourceImage.Width > SourceImage.Height ? SourceImage.Height : SourceImage.Width; + var tempImg = new Bitmap(SourceImage); + + using (var g = Graphics.FromImage(tempImg)) + { + Brush br = new SolidBrush(Color.FromArgb(100, 255, 200, 0)); + var outerRect = new Rectangle(0, 0, side, side); + var innerRect = new Rectangle(5, 5, side - 5, side - 5); + g.FillRectangle(br, outerRect); + g.FillRectangle(br, innerRect); + g.DrawString("我是个正方形,如果看起来不像,调整窗口大小直到我看上去像正方形,或者你也可以对照下面的预览比例和实际比例进行调整", + new Font("黑体", 50), Brushes.Red, innerRect, new StringFormat(0)); + } + + pBmainview.Image = tempImg; + } + + private void Form1_ResizeEnd(object sender, EventArgs e) + { + IsResizing = false; + RefreshView(); + } + + private void btn_report_Click(object sender, EventArgs e) + { + Process.Start("https://popcornboom.github.io/contact/"); + } + + private void btn_del_Click(object sender, EventArgs e) + { + if (dgv_boxesdata.CurrentRow == null) return; + dgv_boxesdata.Rows.Remove(dgv_boxesdata.CurrentRow); + RefreshView(); + } + + private void btn_moveup_Click(object sender, EventArgs e) + { + if (dgv_boxesdata.CurrentRow == null || dgv_boxesdata.CurrentRow.Index <= 0) return; + var index = dgv_boxesdata.CurrentRow.Index; + + try + { + Helper.Swap(boxes, index - 1, index); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + + RefreshView(); + } + + private void pBmainview_Resize(object sender, EventArgs e) + { + if (IsResizing) + l_size.Text = "预览比例(" + (pBmainview.Height * 1.0 / pBmainview.Width).ToString("N2") + ")可能与原图(" + + (SourceImage.Height * 1.0 / SourceImage.Width).ToString("N2") + + ")不一致\n但不影响输出情况(如何调整比例?拉伸窗口!)"; + } + + private void btn_opensavefolder_Click(object sender, EventArgs e) + { + Process.Start(Settings.Default.savefolder); + } + + private void dgv_boxesdata_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) + { + // 自动把 Box.Color 应用到单元格 + var index = e.RowIndex; + var targetStyle = dgv_boxesdata.Rows[index].Cells[color.Name].Style; + targetStyle.BackColor = boxes[index].Color; + targetStyle.SelectionBackColor = boxes[index].Color; + } + + private void btn_movedown_Click(object sender, EventArgs e) + { + if (dgv_boxesdata.CurrentRow == null || + dgv_boxesdata.CurrentRow.Index == dgv_boxesdata.RowCount - 1) return; + var index = dgv_boxesdata.CurrentRow.Index; + + try + { + Helper.Swap(boxes, index + 1, index); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + + RefreshView(); + } + + private void btn_choosepicfolder_Click(object sender, EventArgs e) + { + var dialog = new FolderBrowserDialog(); + dialog.Description = "请选择文件路径"; + dialog.SelectedPath = Settings.Default.srcpicdir; + //dialog.RootFolder = Environment.SpecialFolder.Programs; + if (dialog.ShowDialog() == DialogResult.OK) + { + var foldPath = dialog.SelectedPath + "\\"; + Settings.Default.srcpicdir = foldPath; + Settings.Default.Save(); + tb_srcpicfolder.Text = foldPath; + } + } + + private void cb_picsrcfrom_SelectedIndexChanged(object sender, EventArgs e) + { + if (cb_picsrcfrom.Text == "仅文字无图") + { + gb_text.Enabled = true; + l_boxname.Text = "文本框名称"; + btn_addbox.Text = "新建文本框"; + } + else + { + gb_text.Enabled = false; + l_boxname.Text = "图片框名称"; + btn_addbox.Text = "新建图片框"; + } + } + + private void btn_duplicate_Click(object sender, EventArgs e) + { + try + { + var y = dgv_boxesdata.CurrentCellAddress.Y; + boxes.Insert(y, Helper.DeepCopy(boxes[y])); + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString()); + } + + RefreshView(); + } + } +} \ No newline at end of file diff --git a/Helper.cs b/Helper.cs new file mode 100644 index 0000000..5e193a0 --- /dev/null +++ b/Helper.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace CardEditor +{ + public static class Helper + { + public static IList Swap(IList list, int index1, int index2) + { + var temp = list[index1]; + list[index1] = list[index2]; + list[index2] = temp; + return list; + } + + public static T DeepCopy(T obj) + { + //如果是字符串或值类型则直接返回 + if (obj is string || obj.GetType().IsValueType) return obj; + + var result = Activator.CreateInstance(obj.GetType()); + var fields = obj.GetType().GetFields( + BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); + foreach (var field in fields) + field.SetValue(result, DeepCopy(field.GetValue(obj))); + + return (T) result; + } + } +} \ No newline at end of file diff --git a/TextAlign.cs b/TextAlign.cs new file mode 100644 index 0000000..0210760 --- /dev/null +++ b/TextAlign.cs @@ -0,0 +1,19 @@ +using System.ComponentModel; +using System.Drawing; + +namespace CardEditor +{ + /// + /// 对齐方式 + /// + public enum TextAlign + { + [Description("居左")] Left = 0, + [Description("居右")] Right = StringFormatFlags.DirectionRightToLeft, + [Description("竖左")] VerticalLeft = StringFormatFlags.DirectionVertical, + + [Description("竖右")] VerticalRight = StringFormatFlags.DirectionRightToLeft + | StringFormatFlags.DirectionVertical, + [Description("整行")] Flat = StringFormatFlags.NoWrap + } +} \ No newline at end of file