using System;using System.Diagnostics;using System.Drawing;using System.Threading;using System.Windows.Forms;using GenerateImage.Utilities;namespace GenerateImage{ public partial class Form1 : Form { private AutoGenImageRequire autoGenImageRequire = new AutoGenImageRequire(); private BatchModImageRequire batchModImageRequire = new BatchModImageRequire(); private ImageRequire imageRequire = null; private const string LabelAutoGenSettings = "Auto Generate Image Settings:"; private const string LabelBatchModSettings = "Batch Modify Image Settings:"; private Thread workThread = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.lblPropertyGrid.Text = LabelAutoGenSettings; this.propertyGrid1.SelectedObject = autoGenImageRequire; this.propertyGrid1.PropertyValueChanged = propertyGrid1_PropertyValueChanged; this.rtnAutoGen.CheckedChanged = radioButton_CheckedChanged; } private void radioButton_CheckedChanged(object sender, EventArgs e) { if (!rtnAutoGen.Enabled || !rtnBatchModify.Enabled) return; if (rtnAutoGen.Checked) { this.propertyGrid1.SelectedObject = autoGenImageRequire; this.lblPropertyGrid.Text = LabelAutoGenSettings; this.pictureBox1.BackgroundImage = null; } else { this.propertyGrid1.SelectedObject = batchModImageRequire; this.lblPropertyGrid.Text = LabelBatchModSettings; this.pictureBox1.BackgroundImage = null; } } private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { if (!this.propertyGrid1.Enabled) return; switch(e.ChangedItem.Label) { case "SourceImageFileName": string filename = e.ChangedItem.Value.ToString(); if (string.IsNullOrEmpty(filename)) this.pictureBox1.BackgroundImage = null; else { if (System.IO.File.Exists(filename)) { this.pictureBox1.BackgroundImage = Image.FromFile(filename); this.autoGenImageRequire.ImageFormat = this.pictureBox1.BackgroundImage.RawFormat; } else this.pictureBox1.BackgroundImage = null; } break; default: break; } } private void btnPreview_Click(object sender, EventArgs e) { try { ValidateAutoGenImageRequire(autoGenImageRequire); this.pictureBox1.BackgroundImage = ImageUtils.GenerateImageForPreview(autoGenImageRequire); } catch(InputError err) { MessageBox.Show(err.Message); SetPropertyFocus(err.Label); } } private void btnGenerate_Click(object sender, EventArgs e) { try { if (rtnAutoGen.Checked) { ValidateAutoGenImageRequire(autoGenImageRequire); this.pictureBox1.BackgroundImage = ImageUtils.GenerateImageForPreview(autoGenImageRequire); } else { ValidateBatchModImageRequire(batchModImageRequire); } workThread = new Thread(GenerateImages); workThread.Start(); SetControlEnabled(true); } catch (InputError err) { MessageBox.Show(err.Message); SetPropertyFocus(err.Label); } } private void btnOpenFolder_Click(object sender, EventArgs e) { string folder = null; if (rtnAutoGen.Checked) folder = autoGenImageRequire.SaveAsFolderName; else folder = batchModImageRequire.SaveAsFolderName; if (string.IsNullOrEmpty(folder)) return; Process.Start("Explorer.exe", "\"" folder "\""); } private void timer1_Tick(object sender, EventArgs e) { if (workThread == null) SetControlEnabled(false); if (!workThread.Join(1)) { this.toolStripProgressBar1.Value = ImageUtils.CurrentNumber * 100 / ImageUtils.TotalNumber; this.tslImageName.Text = ImageUtils.CurrentFileName; } else { this.toolStripProgressBar1.Value = 100; this.tslImageName.Text = ImageUtils.CurrentFileName; SetControlEnabled(false); } } private void GenerateImages() { if (rtnAutoGen.Checked) ImageUtils.GenerateImages(this.autoGenImageRequire); else ImageUtils.GenerateImages(this.batchModImageRequire); } private void SetControlEnabled(bool busy) { if (busy) { timer1.Enabled = true; tslStatus.Text = "Generating"; btnGenerate.Enabled = false; btnPreview.Enabled = false; btnOpenFolder.Enabled = false; rtnAutoGen.Enabled = false; rtnBatchModify.Enabled = false; propertyGrid1.Enabled = false; toolStripProgressBar1.Value = 0; } else { timer1.Enabled = false; tslStatus.Text = "Completed"; btnGenerate.Enabled = true; btnPreview.Enabled = true; btnOpenFolder.Enabled = true; rtnAutoGen.Enabled = true; rtnBatchModify.Enabled = true; propertyGrid1.Enabled = true; //toolStripProgressBar1.Value = 0; } } private void SetPropertyFocus(string label) { GridItem item = this.propertyGrid1.SelectedGridItem.Parent; foreach (GridItem p in item.GridItems) { if (p.Label == label) { this.propertyGrid1.Focus(); p.Select(); return; } } } private void ValidateCommonImageRequire(ImageRequire imageReq) { if (imageReq.AddTextFont == null) throw new InputError("AddTextFont is required.", "AddTextFont"); if (!System.IO.Directory.Exists(imageReq.SaveAsFolderName)) throw new InputError("SaveAsFolder is not exist.", "SaveAsFolderName"); } private void ValidateAutoGenImageRequire(AutoGenImageRequire imageReq) { if (imageReq.GenerateNumber > 3000 || imageReq.GenerateNumber < 1) throw new InputError("GenerateNumber must be in the range(0, 3000).", "GenerateNumber"); if (string.IsNullOrEmpty(imageReq.SourceImageFileName)) throw new InputError("SourceImageFileName is required.", "SourceImageFileName"); if (!System.IO.File.Exists(imageReq.SourceImageFileName)) throw new InputError("SourceImageFileName is not exist.", "SourceImageFileName"); ValidateCommonImageRequire(imageReq); } private void ValidateBatchModImageRequire(BatchModImageRequire imageReq) { if (string.IsNullOrEmpty(imageReq.SourceFolderName)) throw new InputError("SourceFolderName is required.", "SourceFolderName"); ValidateCommonImageRequire(imageReq); } }}
下载c# 给图片添加文字水印 实例源码下载用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论