using Metis.ParameterSet;
using PaintDotNet.Adjust;
using PaintDotNet.Base;
using PaintDotNet.Base.CommTool;
using PaintDotNet.CustomControl;
using PaintDotNet.Data.Action;
using PaintDotNet.Data.Param;
using PaintDotNet.DbOpreate.DbModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using static Metis.ParameterSet.ImageMenu;
namespace PaintDotNet.Processing
{
///
/// 图像处理窗口
///
internal class BshTransferAdjustDialog : Form
{
///
/// 步长 初始值1,依次增加
///
string stepLength = "00" + Startup.instance.step_length;
///
/// 主控件
///
private AppWorkspace appWorkspace;
///
/// 当前处理的程序
///
private ParamObject action;
///
/// 如果是脚本执行,将参数带入
///
private Boolean initScriptValues = false;
//
/// 如果有参数保存,将参数带入
///
private Boolean initPamasValues = false;
///
/// 当前活动的图像
///
private OpenCvSharp.Mat imageMat;
///
/// 调用调色板的控件
///
private Panel panel2;
///
/// 调用原图取色 显示色板的控件
///
private Panel panel3;
///
/// 是否使用原图取色
///
private bool item3Checked;
///
/// 调色板
///
private ColorsForm colorsFormGrid;
///
/// 像素格式
///
private PixelFormat pixelFormat;
///
/// 图像面板
///
private DocumentWorkspaceWindow documentWorkspace;
///
/// 公共按钮
///
private CommonControlButtons commonControlButtons;
///
/// 是否点击过应用按钮
/// 如果没有点过,直接点确定还需要走一遍方法,
/// 如果已经点过,直接拿当前图片
///
private bool applyButtonClick = false;
private PdnMenuItem MenuItem;
private double ruler = 0;
private decimal gainMultiple = 0;
private int width = 0;
private int height = 0;
///
/// 选中参数的索引
///
private string index = "1";
[Browsable(false)]
public AppWorkspace AppWorkspace
{
get
{
return this.appWorkspace;
}
set
{
this.appWorkspace = value;
}
}
ActionType menuIdOverall;
///
/// dialog
///
///
/// 对应菜单的id
public BshTransferAdjustDialog(AppWorkspace appWorkspace, ActionType menuId, string dialogText,PdnMenuItem menuItem)
{
menuIdOverall = menuId;
this.MenuItem = menuItem;
InitializeComponent();
InitializeLanguageText();
//
// 设置Dialog属性
//
this.AppWorkspace = appWorkspace;
this.StartPosition = FormStartPosition.CenterScreen;
this.Icon = PdnInfo.AppIcon;
this.imageMat = this.AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat();
this.pixelFormat = this.AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.PixelFormat;
this.Text = dialogText;
//
//初始化色板
//
this.colorsFormGrid = new ColorsForm();
this.colorsFormGrid.StartPosition = FormStartPosition.CenterScreen;
this.colorsFormGrid.setSaveBtn_Click(new System.EventHandler(this.gridColorChanged));
//
//初始化图像控件
//
this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
this.documentWorkspace.Dock = DockStyle.Fill;
this.documentWorkspace.HookMouseEvents();
this.documentWorkspace.AuxiliaryLineEnabled = false;
this.documentWorkspace.Visible = false;
this.groupBox1.Controls.Add(documentWorkspace);
//
//初始化操作按钮
//
this.commonControlButtons = new CommonControlButtons();
this.commonControlButtons.Dock = DockStyle.Top;
this.commonControlButtons.Height = 30;
this.commonControlButtons.HideZoomToWindowAndActualSize();
this.groupBox1.Controls.Add(commonControlButtons);
switch (menuId)
{
//图像偏移
case ActionType.ActionImageOffset:
action = new Data.Action.Action507();
((Action507)action).one.Min = -imageMat.Height;
((Action507)action).one.Max = imageMat.Height;
((Action507)action).two.Min = -imageMat.Width;
((Action507)action).two.Max = imageMat.Width;
break;
//调节
case ActionType.ActionRevertColor:
action = new Data.Action.Action5010();
break;
case ActionType.ActionBgr2Gray:
action = new Data.Action.Action5011();
break;
case ActionType.ActionWhiteBalance:
action = new Data.Action.Action5012();
//白平衡原图取点
this.documentWorkspace.panel.MouseClick += PicturePreviewControl1_item3MouseDown;
//picturePreviewControl1.item3MouseDown += PicturePreviewControl1_item3MouseDown;
break;
case ActionType.ActionColorBalance:
action = new Data.Action.Action5013();
break;
case ActionType.ActionShadingCorrection:
action = new Data.Action.Action5014();
break;
case ActionType.ActionHSB:
action = new Data.Action.Action5015();
break;
case ActionType.ActionBCG:
action = new Data.Action.Action5016();
break;
case ActionType.ActionImageRotate:
action = new Data.Action.Action5017();
break;
case ActionType.ImageChangeResolution:
action = new Data.Action.Action6001();
break;
//锐化
case ActionType.ActionPortray:
action = new Data.Action.Action5020();
break;
case ActionType.ActionEnhanceContour:
action = new Data.Action.Action5021();
break;
case ActionType.ActionBlurMask:
action = new Data.Action.Action5022();
break;
//平滑
case ActionType.Sigma:
action = new Data.Action.Action5030();
break;
case ActionType.GaussianFiltering:
action = new Data.Action.Action5031();
break;
case ActionType.LowPassFiltering:
action = new Data.Action.Action5032();
break;
case ActionType.MedianFiltering:
action = new Data.Action.Action5033();
break;
case ActionType.GaussianBlur:
action = new Data.Action.Action5034();
break;
case ActionType.BilateralFiltering:
action = new Data.Action.Action5035();
break;
case ActionType.WaveletDenoising:
action = new Data.Action.Action5036();
break;
//边缘检测
case ActionType.Log:
action = new Data.Action.Action5040();
break;
case ActionType.Canny:
action = new Data.Action.Action5041();
break;
case ActionType.Sobel:
action = new Data.Action.Action5042();
break;
case ActionType.Kirsch:
action = new Data.Action.Action5043();
break;
case ActionType.Prewitt:
action = new Data.Action.Action5044();
break;
case ActionType.Roberts:
action = new Data.Action.Action5045();
break;
case ActionType.Laplace:
action = new Data.Action.Action5046();
break;
//图像间操作
case ActionType.ActionImageAddition:
action = new Data.Action.Action5050();
break;
default:
action = new Data.Action.Action5010();
break;
}
if (action.Lists == null || action.Lists.Count < 1)
{
Label label = new Label();
label.Left = 7;
label.Top = 31;
label.Size = new Size(60, 12);
label.TextAlign = ContentAlignment.TopLeft;
label.Text = PdnResources.GetString("Menu.Noparams.text");
groupBoxParam.Controls.Add(label);
this.button5.Visible = false;
GetParameters(Startup.instance.imageMenuModel);
}
else
{
//如果是脚本执行,将参数带入(AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId);
if (AppWorkspace.ScriptRunning && AppWorkspace.ScriptCurrentParam != null && AppWorkspace.ScriptCurrentParam.MenuId == action.MenuId)
{
this.initScriptValues = true;
foreach (Args arg in action.Lists)
{
Args param1 = AppWorkspace.ScriptCurrentParam.Lists.Find(m => m.Key.Equals(arg.Key));
if (param1.value != null)
arg.Value = param1.value;
}
AppWorkspace.ScriptCurrentParam = null;//阻止第二次进入仍然被赋值参数
}
else
{
GetParameters(Startup.instance.imageMenuModel);
this.initScriptValues = false;
}
int controlsTop = 0;
//初始化手动控件
for (int i = 0; i < action.Lists.Count; i++)
{
Args args = action.Lists[i];
index = args.value.ToString();
if (args is IntegerNumber || args is DecimalNumber || args is OddNumber)
{
CustomControl.NumberParamControl numberParam = new CustomControl.NumberParamControl();
numberParam.Left = 78;
numberParam.Top = 35 + controlsTop;
numberParam.Tag = args.key;
//设置参数范围
numberParam.paramIndex = 100 + i;
if (args is DecimalNumber)
{
numberParam.DecimalPlaces = ((DecimalNumber)args).DecimalPlaces;
numberParam.Minimum = (double)((DecimalNumber)args).Min;
numberParam.Maximum = (double)((DecimalNumber)args).Max;
}
else if (args is OddNumber)
{
numberParam.Increment = 2;
numberParam.Minimum = ((OddNumber)args).Min;
numberParam.Maximum = ((OddNumber)args).Max;
numberParam.IsOddNumber = true;
}
else
{
numberParam.DecimalPlaces = 0;
numberParam.Minimum = ((IntegerNumber)args).Min;
numberParam.Maximum = ((IntegerNumber)args).Max;
}
double fnum = 0;
if (double.TryParse((initScriptValues || initPamasValues ? args.value : args.initialValue).ToString(), out fnum))
{
numberParam.Value = fnum;
//args.Value = fnum;
}
groupBoxParam.Controls.Add(numberParam);
groupBoxParam.Controls.SetChildIndex(numberParam, 100 + i);
if (args is DecimalNumber)
{
numberParam.ValueChanged += new EventHandler(((DecimalNumber)args).numberParam_ValueChanged);
}
else if (args is OddNumber)
{
numberParam.ValueChanged += new EventHandler(((OddNumber)args).numberParam_ValueChanged);
}
else
{
numberParam.ValueChanged += new EventHandler(((IntegerNumber)args).numberParam_ValueChanged);
}
numberParam.ValueChanged += this.ParameterChangeEvent;
Label label = new Label();
label.Left = 19;
label.Top = 40 + controlsTop;
label.Size = new Size(65, 12);//55->65 调试值,提交备注:调试3-双边滤波-邻域直径。
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = args.name + ":";
groupBoxParam.Controls.Add(label);
controlsTop += 45;
}
else if (args is BooleanObject)
{
Label label = new Label();
label.Left = 10;
label.Top = 40 + controlsTop;
label.Size = new Size(75, 12);
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = args.name + ":";
//colorPanel.BackColorChanged += ((ColorNumber)arg).numberParam_ValueChanged;
////radio.CheckedChanged += ((ChoiseArray)args).numberParam_ValueChanged;
groupBoxParam.Controls.Add(label);
CheckBox check = new CheckBox();
check.Left = 88;
check.Top = 40 + controlsTop;
check.AutoSize = true;
check.CheckedChanged += ((BooleanObject)args).Boolean_ValueChanged;
check.CheckedChanged += this.CheckBoxChangeEvent;
groupBoxParam.Controls.Add(check);
controlsTop += 45;
}
else if (args is ChoiseArray)
{
Label label = new Label();
label.Left = 19;
label.Top = 40 + controlsTop;
label.Size = new Size(55, 12);
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = args.name + ":";
groupBoxParam.Controls.Add(label);
//初始化选项列表
for (int j = 0; j < args.choiseList.Count; j++)
{
BooleanObject argsChoise = args.choiseList[j] as BooleanObject;
RadioButton radio = new RadioButton();
if (argsChoise.key == args.value)
{
radio.Checked = true;
}
radio.AutoSize = true;
radio.Location = new Point(88, 40 + controlsTop/*topMargin*/);
radio.Text = argsChoise.name + "";
radio.Tag = argsChoise.Key;
radio.CheckedChanged += ((ChoiseArray)args).numberParam_ValueChanged;
radio.CheckedChanged += Radio_CheckedChanged;
groupBoxParam.Controls.Add(radio);
if (argsChoise.Lists.Count > 0)
{
ColorNumber phaseColor = argsChoise.Lists[0] as ColorNumber;
if (phaseColor.Key.Equals("phaseColor"))
{
Panel panel = new Panel();
panel.BackColor = Color.FromArgb(int.Parse(phaseColor.Value.ToString()));
panel.Size = new Size(53, 20);
panel.Top = 37 + controlsTop;
panel.Left = 178;
groupBoxParam.Controls.Add(panel);
panel.Click += new System.EventHandler(this.panel2_Click);
panel.BackColorChanged += ((ColorNumber)phaseColor).numberParam_ValueChanged;
panel.BackColorChanged += this.ParameterChangeEvent;
this.panel2 = panel;
}
else if (phaseColor.Key.Equals("imageColor"))
{
Panel panel = new Panel();
panel.BackColor = Color.FromArgb(int.Parse(phaseColor.Value.ToString()));
panel.Size = new Size(53, 20);
panel.Top = 37 + controlsTop;
panel.Left = 178;
groupBoxParam.Controls.Add(panel);
panel.BackColorChanged += ((ColorNumber)phaseColor).numberParam_ValueChanged;
panel.BackColorChanged += this.ParameterChangeEvent;
this.panel3 = panel;
}
}
controlsTop += 45;
}
}
else if (args is ColorNumber)
{
Label label = new Label();
label.Left = 19;
label.Top = 40 + controlsTop;
label.Size = new Size(65, 12);
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = args.name + ":";
//colorPanel.BackColorChanged += ((ColorNumber)arg).numberParam_ValueChanged;
////radio.CheckedChanged += ((ChoiseArray)args).numberParam_ValueChanged;
groupBoxParam.Controls.Add(label);
Panel panel = new Panel();
panel.BackColor = Color.FromArgb(int.Parse(args.Value.ToString()));
panel.Size = new Size(53, 20);
panel.Top = 35 + controlsTop;
panel.Left = 85;
groupBoxParam.Controls.Add(panel);
panel.Click += new System.EventHandler(this.panel2_Click);
panel.BackColorChanged += ((ColorNumber)args).numberParam_ValueChanged;
panel.BackColorChanged += this.ParameterChangeEvent;
this.panel2 = panel;
controlsTop += 45;
}
else if (args is StringArray)
{
Label label = new Label();
label.Left = 29;
label.Top = 40 + controlsTop;
label.Size = new Size(55, 12);
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = args.name + ":";
groupBoxParam.Controls.Add(label);
Panel panel = new Panel();
panel.AutoSize = true;
panel.Top = 40 + controlsTop;
panel.Left = 88;
int position = 1;
int topMargin = 0;
//循环字典,铺多个radio
foreach (var item in (Dictionary)args.initialValue/*((initScriptValues || initPamasValues ? args.value : args.initialValue))*/)
{
int xvalue = 0;
if (position % 1 == 0) xvalue = 0;
if (position % 2 == 0) xvalue = 1;
if (position % 3 == 0) xvalue = 2;
RadioButton radio = new RadioButton();
if (position == 1)
{
radio.Checked = true;
}
radio.AutoSize = true;
radio.Location = new Point(xvalue * 90, topMargin);
radio.Text = item.Value.ToString();
radio.Tag = item.Key;
radio.Checked = item.Key.ToString().Equals(args.value.ToString());
radio.CheckedChanged += ((StringArray)args).numberParam_ValueChanged;
radio.CheckedChanged += this.ParameterChangeEvent;
panel.Controls.Add(radio);
if (position % 3 == 0) topMargin += 25;
position++;
}
groupBoxParam.Controls.Add(panel);
controlsTop += 45;
}
}
}
InitData();
InitCommonButtonEvent();
}
private void startScriptAutomaticAction()
{
applyButtonImpl(null, null);
sureButtonImpl(null, null);
}
private void ParameterChangeEvent(object sender, EventArgs e)
{
this.applyButtonClick = false;
if (action is Action6001)
{
int width = 0;
bool isCheck = false;
foreach (Control control in groupBoxParam.Controls)//遍历参数面板上的所有控件
{
if (control is CustomControl.NumberParamControl)
{
CustomControl.NumberParamControl numberParam = (CustomControl.NumberParamControl)control;
if (numberParam.Tag.Equals("width"))
{
width = (int)numberParam.Value;
}
}
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
if (checkBox.Checked)
{
isCheck = true;
}
}
}
if (isCheck)
{
double matProportion = Convert.ToDouble(this.imageMat.Width) / Convert.ToDouble(this.imageMat.Height);
foreach (Control control in groupBoxParam.Controls)//遍历参数面板上的所有控件
{
if (control is CustomControl.NumberParamControl)
{
CustomControl.NumberParamControl numberParam = (CustomControl.NumberParamControl)control;
if (numberParam.Tag.Equals("height"))
{
numberParam.Value = width / matProportion;
}
}
}
}
}
}
private void CheckBoxChangeEvent(object sender, EventArgs e)
{
this.applyButtonClick = false;
CheckBox checkBox = (CheckBox)sender;
if (!checkBox.Checked)
{
return;
}
if (action is Action6001)
{
int width = 0;
foreach (Control control in groupBoxParam.Controls)//遍历参数面板上的所有控件
{
if (control is CustomControl.NumberParamControl)
{
CustomControl.NumberParamControl numberParam = (CustomControl.NumberParamControl)control;
if (numberParam.Tag.Equals("width"))
{
width = (int)numberParam.Value;
}
}
}
double matProportion = Convert.ToDouble(this.imageMat.Width) / Convert.ToDouble(this.imageMat.Height);
foreach (Control control in groupBoxParam.Controls)//遍历参数面板上的所有控件
{
if (control is CustomControl.NumberParamControl)
{
CustomControl.NumberParamControl numberParam = (CustomControl.NumberParamControl)control;
if (numberParam.Tag.Equals("height"))
{
numberParam.Value = width / matProportion;
}
}
}
}
}
private void InitCommonButtonEvent()
{
this.commonControlButtons.zoomInButton.Click += new EventHandler(zoomInButton_Click);
this.commonControlButtons.zoomOutButton.Click += new EventHandler(zoomOutButton_Click);
this.commonControlButtons.zoomToWindowButton.Click += new EventHandler(zoomToWindowButton_Click);
this.commonControlButtons.actualSizeButton.Click += new EventHandler(actualSizeButton_Click);
this.commonControlButtons.pointerButton.Click += new EventHandler(pointerButton_Click);
this.commonControlButtons.mobileModeButton.Click += new EventHandler(mobileModeButton_Click);
}
private void zoomInButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ZoomIn();
}
private void zoomOutButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ZoomOut();
}
private void zoomToWindowButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
}
private void actualSizeButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
}
private void pointerButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.Pointer;
}
private void mobileModeButton_Click(object sender, EventArgs e)
{
this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
}
private void Radio_CheckedChanged(object sender, EventArgs e)
{
if (((RadioButton)sender).Tag.Equals("3"))//原图取色
{
this.item3Checked = ((RadioButton)sender).Checked;
//picturePreviewControl1.selectColorMode(this.item3Checked);
}
OpenCvSharp.Mat mat = action.PerformProcess(this.imageMat.Clone());
Document document = Document.FromMat(mat);
this.documentWorkspace.Document = document;
this.documentWorkspace.Refresh();
//picturePreviewControl1.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
}
///
/// 相颜色点击事件
///
///
///
private void panel2_Click(object sender, EventArgs e)
{
this.panel2 = ((Panel)sender);
this.colorsFormGrid.UserPrimaryColor = ColorBgra.FromColor(panel2.BackColor);
this.colorsFormGrid.ShowDialog();
}
///
/// Panel2的调色板颜色改变
///
///
///
private void gridColorChanged(object sender, EventArgs e)
{
Color color = this.colorsFormGrid.UserPrimaryColor.ToColor();
//更改背景色,触发事件
this.panel2.BackColor = color;
////改变配置文件里面的相颜色
//this.binaryExtractionModel.PhaseColor = color.ToArgb();
OpenCvSharp.Mat mat = action.PerformProcess(this.imageMat.Clone());
//if (menuIdOverall == ActionType.ActionImageRotate)
//{
// OpenCvSharp.Mat src = action.PerformProcess(PaintDotNet.Camera.Tools.ToMat(this.bitmap));
// //GeometryIntent.ImageRotate(src, mat, float.Parse(action.lists[0].value.ToString()), OpenCvSharp.InterpolationFlags.Linear);
// mat = GeometryIntent.MatRotate(src, float.Parse(action.lists[0].value.ToString()), new OpenCvSharp.Scalar(panel2.BackColor.B, panel2.BackColor.G, panel2.BackColor.R, 255));
//}
//else
//{
// mat = action.PerformProcess(PaintDotNet.Camera.Tools.ToMat(this.bitmap));
//}
//OpenCvSharp.Mat mat = action.PerformProcess(PaintDotNet.Camera.Tools.ToMat(this.bitmap));
Document document = Document.FromMat(mat);
this.documentWorkspace.Document = document;
this.documentWorkspace.Refresh();
//picturePreviewControl1.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
//关闭色板
this.colorsFormGrid.Close();
}
///
/// 初始化数据
///
public void InitData()
{
Document document = Document.FromImageMat(this.imageMat.Clone());
this.documentWorkspace.Document = document;
this.documentWorkspace.Visible = true;
this.documentWorkspace.Refresh();
//给picturebox赋值
//picturePreviewControl1.pictureBox1.Image = this.bitmap;
//初始化/重置参数
resetNumber();
//this.initScriptValues = false;//阻止第二次进入仍然被赋值参数
}
private void resetNumber()
{
if (action is Action5012)//白平衡
{
//GetParameters(Startup.instance.imageMenuModel);
this.item3Checked = false;
//重新初始化选项列表
Args args = action.Lists[0];
for (int j = 0; j < args.choiseList.Count; j++)
{
BooleanObject argsChoise = args.choiseList[j] as BooleanObject;
if (argsChoise.Lists.Count > 0)
{
ColorNumber phaseColor = argsChoise.Lists[0] as ColorNumber;
if (phaseColor.Key.Equals("phaseColor"))//色板取色
{
this.panel2.BackColor = Color.FromArgb(int.Parse((initScriptValues || initPamasValues ? phaseColor.value : phaseColor.initialValue).ToString()));
}
else if (phaseColor.Key.Equals("imageColor"))//原图取色
{
this.panel3.BackColor = Color.FromArgb(int.Parse((initScriptValues || initPamasValues ? phaseColor.value : phaseColor.initialValue).ToString()));
}
}
}
}
if (action is Action507 || action is Action5017)//图像偏移
{
//重新初始化选色面板
for (int j = 0; j < action.Lists.Count; j++)
{
ColorNumber phaseColor = action.Lists[j] as ColorNumber;
if (phaseColor is ColorNumber)
{
this.panel2.BackColor = Color.FromArgb(int.Parse((initScriptValues || initPamasValues ? phaseColor.value : phaseColor.initialValue).ToString()));
break;
}
}
}
foreach (Control control in groupBoxParam.Controls)//遍历参数面板上的所有控件
{
if (control is CustomControl.NumberParamControl)
{
CustomControl.NumberParamControl numberParam = (CustomControl.NumberParamControl)control;
if (numberParam.paramIndex >= 100)
{
//List list1s = action.Lists[0];
Args args = /*list1s*/action.Lists[numberParam.paramIndex - 100];
double fnum = 0;
if (double.TryParse((initScriptValues || initPamasValues ? args.value : args.initialValue).ToString(), out fnum))
{
numberParam.Value = fnum;
}
//numberParam.Value = (int)args.initialValue;
}
}
else if (control is RadioButton)
{
if (control.Tag.Equals(index))
{
((RadioButton)control).Checked = true;
}
else
{
((RadioButton)control).Checked = false;
}
}
}
}
///
/// 原图取点的响应方法
///
///
///
private void PicturePreviewControl1_item3MouseDown(object sender, MouseEventArgs e)
{
if (this.item3Checked)
{
System.Drawing.Point point = this.documentWorkspace.GetScalePoint(new System.Drawing.Point(e.X, e.Y));
int x = point.X;
int y = point.Y;
if (x >= 0 && y >= 0 && x < this.documentWorkspace.CompositionSurface.Width && y < this.documentWorkspace.CompositionSurface.Height)
{
OpenCvSharp.Mat srt = this.imageMat.Clone();
OpenCvSharp.Vec3b bgr = srt.At(y, x);
Color color = Color.FromArgb(bgr[2], bgr[1], bgr[0]);
//更改背景色,触发事件
this.panel3.BackColor = color;
OpenCvSharp.Mat mat = action.PerformProcess(srt);
//mat.Set(e.Location.Y, e.Location.X, new OpenCvSharp.Vec3b(0, 0, 255));//测试取点的位置
//picturePreviewControl1.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
Document document = Document.FromMat(mat);
this.documentWorkspace.Document = document;
this.documentWorkspace.Refresh();
}
}
}
private void BshTransferAdjustDialog_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
//图像名称输入框
informationControl.fileNameTextBox1.Text = this.Text + "-" + stepLength;
//picturePreviewControl1.isShown = true;
parametersSettingControl.applyButtonClick += new EventHandler(applyButtonImpl);//注册事件,在窗体中事件处理代码中预览图像处理效果
parametersSettingControl.sureButtonClick += new EventHandler(sureButtonImpl);//注册事件,在窗体中事件处理代码中预览图像处理效果
if (this.initScriptValues && this.appWorkspace.ScriptAutomatic)
{
this.startScriptAutomaticAction();
}
}
//应用
private void applyButtonImpl(object sender, EventArgs e)
{
ProgressThreadProcClass procClass = new ProgressThreadProcClass();
int itemCount = 100;
ProgressThreadProcClass.IFileTransferProgressEvents progressEvents = new ProgressThreadProcClass.IFileTransferProgressEvents();
System.Threading.ThreadStart copyThreadProc =
delegate ()
{
try
{
OpenCvSharp.Mat mat = new OpenCvSharp.Mat();
if (menuIdOverall == ActionType.ImageChangeResolution)
{
double ruleData = 1;
decimal gainMultiple = 0;
if (this.documentWorkspace.xmlSaveModel == null)
{
this.appWorkspace.getMeasureInfo().TryGetValue(Startup.instance.ruleDB == null ? MeasurementUnit.Micron : (MeasurementUnit)(Startup.instance.ruleDB.ruler_units), out ruleData);
gainMultiple = Startup.instance.ruleDB == null ? 1 : Startup.instance.ruleDB.gain_multiple;
}
else
{
ruleData = this.documentWorkspace.GetRuler((MeasurementUnit)this.documentWorkspace.xmlSaveModel.ruler_units);
gainMultiple = this.documentWorkspace.xmlSaveModel.gain_multiple;
}
this.width = Convert.ToInt32(action.lists[0].value);
this.height = Convert.ToInt32(action.lists[1].value);
if (!action.lists[3].value.ToString().Equals("Pixel"))
{
int width = this.imageMat.Width;
int height = this.imageMat.Height;
int realWidth = Convert.ToInt32(Convert.ToDouble(this.width) * ruleData);
int realHeight = Convert.ToInt32(Convert.ToDouble(this.height) * ruleData);
double ruler = Convert.ToDouble(this.width) / realWidth;
this.gainMultiple *= (width / realWidth);
action.lists[0].value = realWidth;
action.lists[1].value = realHeight;
this.ruler = ruler;
}
else
{
if (this.documentWorkspace.xmlSaveModel == null)
{
this.appWorkspace.getMeasureInfo().TryGetValue((MeasurementUnit)Startup.instance.ruleDB.ruler_units, out ruleData);
}
else
{
ruleData = this.documentWorkspace.GetRuler((MeasurementUnit)Startup.instance.ruleDB.ruler_units);
}
int width = this.imageMat.Width;
this.gainMultiple *= (width / this.width);
double ruler = Convert.ToDouble((width * ruleData) / Convert.ToDouble(action.lists[0].value));
this.ruler = ruler;
}
mat = action.PerformProcess(this.imageMat.Clone());
}
else
{
mat = action.PerformProcess(this.imageMat.Clone());
}
Document document = Document.FromMat(mat);
this.documentWorkspace.Document = document;
this.documentWorkspace.Refresh();
this.applyButtonClick = true;
}
catch (Exception ex)
{
}
finally
{
progressEvents.EndOperation(OperationResult.Finished);
}
};
procClass.StartProgressAction(/*currentForm*/this, itemCount, copyThreadProc, progressEvents, null);
}
//确定
internal void sureButtonImpl(object sender, EventArgs e)
{
ProgressThreadProcClass procClass = new ProgressThreadProcClass();
int itemCount = 100;
ProgressThreadProcClass.IFileTransferProgressEvents progressEvents = new ProgressThreadProcClass.IFileTransferProgressEvents();
System.Threading.ThreadStart copyThreadProc =
delegate ()
{
try
{
OpenCvSharp.Mat mat = new OpenCvSharp.Mat();
if (menuIdOverall == ActionType.ImageChangeResolution)
{
if (!this.applyButtonClick)
{
if (this.pixelFormat == PixelFormat.Format8bppIndexed && mat.Type() != OpenCvSharp.MatType.CV_8UC1)
{
mat = mat.CvtColor(OpenCvSharp.ColorConversionCodes.BGR2GRAY);
}
double ruleData = 1;
if (this.documentWorkspace.xmlSaveModel == null)
{
this.appWorkspace.getMeasureInfo().TryGetValue(Startup.instance.ruleDB == null ? MeasurementUnit.Micron : (MeasurementUnit)(Startup.instance.ruleDB.ruler_units), out ruleData);
gainMultiple = Startup.instance.ruleDB == null ? 1 : Startup.instance.ruleDB.gain_multiple;
}
else
{
ruleData = this.documentWorkspace.GetRuler((MeasurementUnit)this.documentWorkspace.xmlSaveModel.ruler_units);
gainMultiple = this.documentWorkspace.xmlSaveModel.gain_multiple;
}
if (!action.lists[3].value.ToString().Equals("Pixel"))
{
int width = this.imageMat.Width;
int height = this.imageMat.Height;
int realWidth = Convert.ToInt32(Convert.ToDouble(this.width) * ruleData);
int realHeight = Convert.ToInt32(Convert.ToDouble(this.height) * ruleData);
this.gainMultiple *= (width / realWidth);
double ruler = Convert.ToDouble(this.width) / realWidth;
action.lists[0].value = realWidth;
action.lists[1].value = realHeight;
this.ruler = ruler;
}
else
{
int width = this.imageMat.Width;
this.gainMultiple *= (width / this.width);
double ruler = Convert.ToDouble((width * ruleData) / Convert.ToDouble(this.width));
this.ruler = ruler;
}
}
else {
double ruleData = 1;
if (this.documentWorkspace.xmlSaveModel == null)
{
this.appWorkspace.getMeasureInfo().TryGetValue(Startup.instance.ruleDB == null ? MeasurementUnit.Micron : (MeasurementUnit)(Startup.instance.ruleDB.ruler_units), out ruleData);
gainMultiple = Startup.instance.ruleDB == null ? 1 : Startup.instance.ruleDB.gain_multiple;
}
else
{
ruleData = this.documentWorkspace.GetRuler((MeasurementUnit)this.documentWorkspace.xmlSaveModel.ruler_units);
gainMultiple = this.documentWorkspace.xmlSaveModel.gain_multiple;
}
if (!action.lists[3].value.ToString().Equals("Pixel"))
{
int width = this.imageMat.Width;
int height = this.imageMat.Height;
int realWidth = Convert.ToInt32(Convert.ToDouble(this.width) * ruleData);
int realHeight = Convert.ToInt32(Convert.ToDouble(this.height) * ruleData);
double ruler = Convert.ToDouble(this.width) / realWidth;
this.gainMultiple *= (width / realWidth);
action.lists[0].value = realWidth;
action.lists[1].value = realHeight;
}
else {
int width = this.imageMat.Width;
this.gainMultiple *= (width / this.width);
double ruler = Convert.ToDouble((width * ruleData) / Convert.ToDouble(this.width));
this.ruler = ruler;
}
}
mat = action.PerformProcess(this.imageMat.Clone());
}
else
{
mat = action.PerformProcess(this.imageMat.Clone());
if (!this.applyButtonClick)
{
if (this.pixelFormat == PixelFormat.Format8bppIndexed && mat.Type() != OpenCvSharp.MatType.CV_8UC1)
{
mat = mat.CvtColor(OpenCvSharp.ColorConversionCodes.BGR2GRAY);
}
}
else
{
mat = this.documentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
}
}
this.Invoke(new EventHandler(delegate
{
//生成新图-是
if (informationControl.radioButton1.Checked)
{
this.OpenFileImpl(mat);
}
else
{
Document document = Document.FromImageMat(mat);
AppWorkspace.ActiveDocumentWorkspace.Document = document;
}
this.Close();
}));
}
catch (Exception ex)
{
}
finally
{
progressEvents.EndOperation(OperationResult.Finished);
}
};
procClass.StartProgressAction(/*currentForm*/this, itemCount, copyThreadProc, progressEvents, null);
}
//生成新图
private void OpenFileImpl(OpenCvSharp.Mat mat)
{
DocumentWorkspace dw = AppWorkspace.AddNewDocumentWorkspace();
Document document = Document.FromImageMat(mat);
dw.Document = document;
dw.fileText = informationControl.fileNameTextBox1.Text;
if (this.ruler != 0)
{
dw.RuleAttribute("分辨率调整", ruler, MeasurementUnit.Micron, this.gainMultiple);
}
else
{
dw.xmlSaveModel = this.appWorkspace.ActiveDocumentWorkspace.xmlSaveModel;
dw.rules = this.appWorkspace.ActiveDocumentWorkspace.rules;
}
AppWorkspace.ActiveDocumentWorkspace = dw;
Startup.instance.step_length += 1;
}
//参数-重设
private void button5_Click(object sender, EventArgs e)
{
GetParameters(Startup.instance.imageMenuModel);
//初始化/重置参数
resetNumber();
Document document = Document.FromImageMat(this.imageMat.Clone());
this.documentWorkspace.Document = document;
this.documentWorkspace.Refresh();
//picturePreviewControl1.pictureBox1.Image = this.bitmap;
}
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeLanguageText()
{
this.groupBoxParam.Text = PdnResources.GetString("Menu.params.text");
this.button5.Text = PdnResources.GetString("Menu.rebuild.Text");
this.groupBox1.Text = PdnResources.GetString("Menu.Preview.text");
this.Text = PdnResources.GetString("Menu.geprocessiconditioningsaturationbrigh.Text");
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.parametersSettingControl = new PaintDotNet.CustomControl.ParametersSettingAdjustControl();
this.informationControl = new PaintDotNet.CustomControl.InformationAdjustControl();
this.groupBoxParam = new System.Windows.Forms.GroupBox();
this.button5 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBoxParam.SuspendLayout();
this.SuspendLayout();
//
// parametersSettingControl
//
this.parametersSettingControl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.parametersSettingControl.Location = new System.Drawing.Point(12, 12);
this.parametersSettingControl.Name = "parametersSettingControl";
this.parametersSettingControl.Size = new System.Drawing.Size(918, 58);
this.parametersSettingControl.TabIndex = 9;
//
// informationControl
//
this.informationControl.Location = new System.Drawing.Point(12, 76);
this.informationControl.Name = "informationControl";
this.informationControl.Size = new System.Drawing.Size(373, 82);
this.informationControl.TabIndex = 11;
//
// groupBoxParam
//
this.groupBoxParam.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.groupBoxParam.Controls.Add(this.button5);
this.groupBoxParam.Location = new System.Drawing.Point(12, 164);
this.groupBoxParam.Name = "groupBoxParam";
this.groupBoxParam.Size = new System.Drawing.Size(373, 353);
this.groupBoxParam.TabIndex = 12;
this.groupBoxParam.TabStop = false;
//
// button5
//
this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button5.Location = new System.Drawing.Point(292, 324);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 1;
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Location = new System.Drawing.Point(392, 77);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(538, 440);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
//
// BshTransferAdjustDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(942, 529);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBoxParam);
this.Controls.Add(this.informationControl);
this.Controls.Add(this.parametersSettingControl);
this.Name = "BshTransferAdjustDialog";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.BshTransferAdjustDialog_FormClosing);
this.Load += new System.EventHandler(this.BshTransferAdjustDialog_Load);
this.groupBoxParam.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private CustomControl.ParametersSettingAdjustControl parametersSettingControl;
private CustomControl.InformationAdjustControl informationControl;
private System.Windows.Forms.GroupBox groupBoxParam;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.GroupBox groupBox1;
private void BshTransferAdjustDialog_FormClosing(object sender, FormClosingEventArgs e)
{
#region [开启脚本录制]
if (appWorkspace.startScriptRecording)
{
getScriptRecording();
}
#endregion
string newpic = 1 + "";
//获取参数实体
ImageMenu imageMenu;
if (Startup.instance.imageMenuModel != null)
{
imageMenu = Startup.instance.imageMenuModel;
}
else
{
imageMenu = new ImageMenu();
imageMenu.ListParam = new List();
}
//数字控制是否生成新图
if (informationControl.radioButton1.Checked)
newpic = 1 + "";
else
newpic = 0 + "";
List list = imageMenu.ListParam.Where(x => x.menuId == (int)action.MenuId).ToList();
if (list.Count == 0)
{
ImageParam param = new ImageParam();
param.menuId = (int)action.MenuId;
param.param_key = "param_newpic";
param.param_type = -1;
param.param_value = newpic;
imageMenu.ListParam.Add(param);
}
//判断页面是否有参数
if (action.lists.Count > 0)
{
for (int i = 0; i < action.lists.Count; i++)
{
if (list.Count > 0)//是否存过参数
{
for (int j = 0; j < imageMenu.ListParam.Count; j++)//给参数实体赋值
{
if (imageMenu.ListParam[j].menuId == (int)action.MenuId && imageMenu.ListParam[j].param_key == action.lists[i].key)
{
imageMenu.ListParam[j].param_type = (int)action.lists[i].Type;
imageMenu.ListParam[j].param_value = action.lists[i].value.ToString();
//imageMenu.ListParam[j].param_newpic = informationControl.radioButton1.Checked;
}
else if (imageMenu.ListParam[j].menuId == (int)action.MenuId && imageMenu.ListParam[j].param_key == "param_newpic")
{
//imageMenu.ListParam[j].param_type = (int)action.lists[i].Type;
imageMenu.ListParam[j].param_value = newpic;
//imageMenu.ListParam[j].param_newpic = informationControl.radioButton1.Checked;
}
}
//判断是否是白平衡
if (action.MenuId == ActionType.ActionWhiteBalance)
{
for (int j = 0; j < imageMenu.ListParam.Count; j++)//给参数实体赋值
{
if (int.Parse(action.Lists[0].value.ToString()) > 1)
{
if (imageMenu.ListParam[j].param_key == "phaseColor" || imageMenu.ListParam[j].param_key == "imageColor")
{
imageMenu.ListParam[j].menuId = (int)action.MenuId;
imageMenu.ListParam[j].param_key = action.Lists[0].choiseList[int.Parse(action.Lists[0].value.ToString()) - 1].Lists[0].key;
imageMenu.ListParam[j].param_value = action.Lists[0].choiseList[int.Parse(action.Lists[0].value.ToString()) - 1].Lists[0].value.ToString();
}
}
}
}
}
else //如果没保存过新增
{
ImageParam param = new ImageParam();
param.menuId = (int)action.MenuId;
param.param_key = action.lists[i].key;
param.param_type = (int)action.lists[i].Type;
param.param_value = action.lists[i].value.ToString();
//判断是否是白平衡
if (action.MenuId == ActionType.ActionWhiteBalance)
{
ImageParam param5012 = new ImageParam();
try
{
param5012.menuId = (int)action.MenuId;
param5012.param_key = action.Lists[0].choiseList[int.Parse(action.Lists[0].value.ToString()) - 1].Lists[0].key;
param5012.param_value = action.Lists[0].choiseList[int.Parse(action.Lists[0].value.ToString()) - 1].Lists[0].value.ToString();
}
catch (Exception)
{
//ImageParam param5012 = new ImageParam();
param5012.menuId = (int)action.MenuId;
param5012.param_key = "objectSelect";
param5012.param_value = "1";
}
imageMenu.ListParam.Add(param5012);
}
imageMenu.ListParam.Add(param);
}
}
}
else //没有参数,给是否生成新图赋值
{
if (list.Count > 0)
{
for (int j = 0; j < imageMenu.ListParam.Count; j++)
{
if (imageMenu.ListParam[j].menuId == (int)action.MenuId && imageMenu.ListParam[j].param_key == "param_newpic")
{
imageMenu.ListParam[j].param_value = newpic;
}
}
}
}
Startup.instance.imageMenuModel = imageMenu;
//按路径和名称保存xml文件
string userInfoXml = XmlSerializeHelper.XmlSerialize(imageMenu);
//xml保存路径
string filePath = Application.StartupPath + "\\Config\\" + "Default\\ParameterSaving\\ParameterSaving.xml";
//保存xml
FileOperationHelper.WriteStringToFile(userInfoXml, filePath, FileMode.Create);
}
///
/// 初始化时获取参数
///
/// 系统读取的参数配置
public void GetParameters(ImageMenu imageMenu)
{
List list = new List();
if (imageMenu.ListParam.Count > 0)
list = imageMenu.ListParam.Where(x => x.menuId == (int)action.MenuId).ToList();
if (list.Count > 0)
{
initPamasValues = true;
for (int i = 0; i < action.Lists.Count; i++)
{
for (int j = 0; j < list.Count; j++)
{
if (list[j].param_key.Equals(action.Lists[i].key))
{
action.Lists[i].Type = (Dtryt)list[j].param_type;
action.Lists[i].value = list[j].param_value;
break;
}
}
//判断是否是白平衡
if (action.MenuId == ActionType.ActionWhiteBalance)
{
for (int j = 0; j < list.Count; j++)//给参数实体赋值
{
if (list[j].param_key == "phaseColor")
{
action.Lists[0].choiseList[1].Lists[0].key = "phaseColor";
action.Lists[0].choiseList[1].Lists[0].value = list[j].param_value;
}
if (list[j].param_key == "imageColor")
{
action.Lists[0].choiseList[2].Lists[0].key = "imageColor";
action.Lists[0].choiseList[2].Lists[0].value = list[j].param_value;
}
}
}
}
for (int j = 0; j < list.Count; j++)
{
if ("param_newpic".Equals(list[j].param_key))
{
if (list[j].param_value == "1")
{
informationControl.radioButton1.Checked = true;
informationControl.radioButton2.Checked = false;
}
else
{
informationControl.radioButton1.Checked = false;
informationControl.radioButton2.Checked = true;
}
}
}
}
}
#region [脚本录制]
private void getScriptRecording()
{
List args = action.Lists;
if (action is Action5012) {
args[0].Type = Dtryt.Choise;
//if (Convert.ToInt32(args[0].value) == 2)
//{
// ColorNumber phase0Color = new ColorNumber();
// phase0Color.key = "imageColor";
// phase0Color.name = PdnResources.GetString("Menu.ImageProcessing.adjust.Theoriginalcolor.text");
// phase0Color.Value = this.panel2.BackColor.ToArgb();
// phase0Color.initialValue = this.panel2.BackColor.ToArgb();
// args.Add(phase0Color);
//} else if (Convert.ToInt32(args[0].value) == 3) {
// ColorNumber phaseColor = new ColorNumber();
// phaseColor.key = "phaseColor";
// phaseColor.name = PdnResources.GetString("Menu.ImageProcessing.adjust.Colorpicking.text");
// phaseColor.Value = this.panel3.BackColor.ToArgb();
// phaseColor.initialValue = this.panel3.BackColor.ToArgb();
// args.Add(phaseColor);
//}
}
appWorkspace.SetScriptStartRecording(MenuItem.MenuId, MenuItem.Text, args);
}
#endregion
}
}