代码拉取完成,页面将自动刷新
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;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
namespace PictureText
{
public partial class FormDrawBoard : Form
{
#region 变量
Bitmap originImg;
Image finishImg;
Graphics g;
Point StartPoint, EndPoint, picPoint;
Color drawColor;
bool IsDraw;
Font font;
string drawText;
string backgroundPicturePath;
string backgroundPicturePathNext;
RadioButton preRadioButton;
DrawType drawtype;
SolidBrush myBrush;//画刷
string folderExportOnekey;
#endregion
#region 枚举
enum DrawType
{
Rect,
RectWhite,
Text
}
#endregion
#region 初始化
//构造函数
public FormDrawBoard()
{
InitializeComponent();
drawColor = Config.getColor();
bt_color.BackColor = drawColor;
font = Config.getFont();
folderExportOnekey = Config.getExportPath();
if (folderExportOnekey != null)
btnExportOnekey.Enabled = true;
initRadioButton();
}
//初始化背景
private void bt_init_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image file(*.jpg;*.png;*.bmp)|*.jpg;*.png;*.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
backgroundPicturePath = ofd.FileName;
init_Background();
picDraw.Image = originImg;
}
}
//设置背景
void init_Background()
{
Image img = Image.FromFile(backgroundPicturePath);
picDraw.Width = img.Width;
picDraw.Height = img.Height;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.UpdateStyles();
originImg = new Bitmap(img.Width, img.Height);
g = Graphics.FromImage(originImg);
g.DrawImage(img, 0, 0, img.Width, img.Height);
finishImg = (Image)originImg.Clone();
this.Text = "文件: " + backgroundPicturePath.Substring(backgroundPicturePath.LastIndexOf('\\') + 1);
findNext();
initChapter();
}
void findNext()
{
string dir = backgroundPicturePath.Substring(0, backgroundPicturePath.LastIndexOf('\\') + 1);
string path = backgroundPicturePath.Substring(backgroundPicturePath.LastIndexOf('\\') + 1);
string exe;
int num;
exe = path.Substring(path.LastIndexOf('.'));
path = path.Replace(exe, "");
try
{
num = Int32.Parse(path);
}
catch
{
bt_next.Enabled = false;
return;
}
num++;
if (!File.Exists(dir + num.ToString("D6") + exe))
{
bt_next.Enabled = false;
return;
}
backgroundPicturePathNext = dir + num.ToString("D6") + exe;
bt_next.Enabled = true;
}
private void bt_next_Click(object sender, EventArgs e)
{
backgroundPicturePath = backgroundPicturePathNext;
init_Background();
picDraw.Image = originImg;
}
void initChapter()
{
string file = backgroundPicturePath.Substring(backgroundPicturePath.LastIndexOf('\\') + 1);
file = file.Substring(0, file.LastIndexOf('.'));
int id = Int32.Parse(file);
tv_chapter.Text = Database.query(id);
}
//初始化单选按钮
void initRadioButton()
{
iterateRadioButton(this.Controls);
preRadioButton = rb_2;
rb_1.Checked = true;
//RadioButton_Click(rb_name, null);
setDrawText(rb_1);
}
// 遍历RadioButton
public virtual void iterateRadioButton(Control.ControlCollection ctls)
{
foreach (Control control in ctls)
{
if (control is RadioButton)
{
((RadioButton)control).Click += new System.EventHandler(RadioButton_Click);
}
if (control.Controls.Count > 0)
{
iterateRadioButton(control.Controls);
}
}
}
//单选按钮点击事件
private void RadioButton_Click(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Text.Contains(" "))
setDrawText(rb);
else
setRectangle(rb);
}
//复位
private void bt_reset_Click(object sender, EventArgs e)
{
if (backgroundPicturePath == null || backgroundPicturePath == "")
return;
init_Background();
picDraw.Image = originImg;
}
#endregion
#region 绘图
private void picDraw_MouseDown(object sender, MouseEventArgs e)
{
if (originImg == null)
return;
if (e.Button == MouseButtons.Left)
{
IsDraw = true;
StartPoint = e.Location;
}
}
private void picDraw_MouseMove(object sender, MouseEventArgs e)
{
if (IsDraw)
{
EndPoint = e.Location;
finishImg = (Image)originImg.Clone();
g = Graphics.FromImage(finishImg);
g.SmoothingMode = SmoothingMode.AntiAlias; //抗锯齿
if (drawtype == DrawType.Text)
{
if (!cb_isTransparent.Checked)
{
SizeF s = g.MeasureString(drawText, font);
myBrush = new SolidBrush(Color.White);
g.FillRectangle(myBrush, EndPoint.X, EndPoint.Y, (int)s.Width, (int)s.Height);
}
g.DrawString(drawText, font, new SolidBrush(drawColor), EndPoint);
}
else
{
Point leftTop = new Point(StartPoint.X, StartPoint.Y);
int Ewidth = Math.Abs(StartPoint.X - e.X), Eheight = Math.Abs(StartPoint.Y - e.Y);
if (e.X < StartPoint.X)
leftTop.X = e.X;
if (e.Y < StartPoint.Y)
leftTop.Y = e.Y;
Rectangle rect = new Rectangle(leftTop, new Size(Ewidth, Eheight));
if (drawtype == DrawType.Rect)
{
myBrush = new SolidBrush(drawColor);
g.FillRectangle(myBrush, rect);
}
else
{
myBrush = new SolidBrush(Color.White);
g.FillRectangle(myBrush, rect);
}
}
picPoint = EndPoint;
reDraw();
}
}
private void picDraw_MouseUp(object sender, MouseEventArgs e)
{
IsDraw = false;
originImg = (Bitmap)finishImg;
//此句的作用是避免窗体最小化后还原窗体时,画布内容“丢失”
//其实没有丢失,只是没刷新而已,读者可以在画布任意处作画,便可还原画布内容
picDraw.Image = originImg;
}
//重绘绘图区(二次缓冲技术)
private void reDraw()
{
Graphics graphics = picDraw.CreateGraphics();
graphics.DrawImage(finishImg, new Point(0, 0));
graphics.Dispose();
}
#endregion
#region 设置
//设置颜色
private void bt_color_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.FullOpen = true;
cd.Color = drawColor;
if (cd.ShowDialog() == DialogResult.OK)
{
setColor(cd.Color);
}
}
//设置字体
private void bt_font_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.Font = font;
if (fd.ShowDialog() == DialogResult.OK)
{
font = fd.Font;
}
}
//设置文字
void setDrawText(RadioButton rb)
{
int index = rb.Text.LastIndexOf(" ");
setColor(rb.Text.Substring(index));
drawText = rb.Text.Substring(index + 1);
drawtype = DrawType.Text;
setRadioButtonColor(rb);
}
//设置矩形
void setRectangle(RadioButton rb)
{
if (rb.Text == "矩形")
drawtype = DrawType.Rect;
else if (rb.Text == "白色矩形")
drawtype = DrawType.RectWhite;
setRadioButtonColor(rb);
}
//设置单选按钮颜色
void setRadioButtonColor(RadioButton rb)
{
preRadioButton.ForeColor = Color.Black;
rb.ForeColor = Color.Red;
preRadioButton = rb;
}
//设置导出目录
private void setDir_Click(object sender, EventArgs e)
{
FormOnekey f = new FormOnekey(folderExportOnekey);
f.StartPosition = FormStartPosition.CenterScreen;
f.Owner = this;
f.Show();
}
//设置导出目录,外部调用
public void setFolderExportOnekey(string path)
{
if (path == "")
return;
folderExportOnekey = path;
btnExportOnekey.Enabled = true;
Config.saveExportPath(path);
}
#endregion
#region 保存
//保存单个图片
private void btnExport_Click(object sender, EventArgs e)
{
if (originImg == null)
{
MessageBox.Show("不存在背景图片");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "jpg|*.jpg|png|*.png|bmp|*.bmp";
if (sfd.ShowDialog() == DialogResult.OK)
{
finishImg.Save(sfd.FileName);
}
MessageBox.Show("保存成功");
}
//保存配置
private void bt_saveconfig_Click(object sender, EventArgs e)
{
Config.saveFont(font);
Config.saveColor(drawColor);
MessageBox.Show("保存成功");
}
// 一键保存图片
private void btnExportOnekey_Click(object sender, EventArgs e)
{
if (originImg == null)
{
MessageBox.Show("不存在背景图片");
return;
}
string path = folderExportOnekey;
path += backgroundPicturePath.Substring(backgroundPicturePath.LastIndexOf('\\'));
finishImg.Save(path);
MessageBox.Show("保存成功");
}
#endregion
#region 颜色
void setColor(Color c)
{
drawColor = c;
bt_color.BackColor = c;
}
void setColor(string text)
{
setColor(getTextColor(text));
}
private void Color_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
setColor(pb.BackColor);
}
Color getTextColor(string text)
{
text = text.Trim();
if (isTextEquals(text, "形容词", "定语"))
return ColorTranslator.FromHtml("#912BD6");
if (isTextEquals(text, "专有名词"))
return ColorTranslator.FromHtml("#329CBF");
if (isTextEquals(text, "名词", "偏次", "同位语", "主语", "代主语", "名词句"))
return ColorTranslator.FromHtml("#6F9FDB");
if (isTextEquals(text, "介词", "介词短语", "受词"))
return ColorTranslator.FromHtml("#D73434");
if (isTextEquals(text, "牵挂","否定虚词", "字母"))
return ColorTranslator.FromHtml("#F08229");
if (isTextEquals(text, "动词", "动词句", "状语"))
return ColorTranslator.FromHtml("#45D040");
if (isTextEquals(text, "连词", "另起虚词"))
return ColorTranslator.FromHtml("#1903E9");
if (isTextEquals(text, "人称代词", "谓语", "宾语"))
return ColorTranslator.FromHtml("#637B93");
if (isTextEquals(text, "并列语", "强调"))
return ColorTranslator.FromHtml("#D8019C");
if (isTextEquals(text, "关系代词", "关系子句"))
return ColorTranslator.FromHtml("#8B7B03");
if (isTextEquals(text, "指示代词"))
return ColorTranslator.FromHtml("#C9AD58");
//
return drawColor;
}
//字符串对比
public bool isTextEquals(string data, params string[] texts)
{
foreach (string t in texts)
{
if (data == t)
return true;
}
return false;
}
#endregion
#region 拖拽操作
private void Form_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
if (!PICTURE.IsImage(path))
{
MessageBox.Show("不是图片");
return;
}
backgroundPicturePath = path;
init_Background();
picDraw.Image = originImg;
}
private void Form_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
}
#endregion
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。