我手头有一个别人的代码,是winform程序,其中包含了两个类,分别是FormSnake.cs和SnakeSharp.cs在FormSnake类中的鼠标事件里,用了一个动态数组presetContour来记录鼠标每次每次停留位置的坐标,在SnakeSarp中也定义一个数组snakePoints,我的问题是:presetContour里面是肯定有数据的,而snakePoints是怎么得到这些数据的。我把两个类的代码都放上来,请明白的前辈指点一下。
3条回答 默认 最新
- 林大虾1 2016-05-17 02:50关注
+++++++++++FrmSnake.cs++++++++++++++
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace SnakeSharp
{
public partial class FormSnake : Form
{
public FormSnake()
{
InitializeComponent();
}
Graphics graphics = null;
Image curImage = null;
Bitmap oriImage = null;
Point imageOrignal = new Point(0, 0);
List presetContour = null;
SnakeSharp snakeSharp = null;private void panelBMP_Paint(object sender, PaintEventArgs e) { try { SplitterPanel pnl = sender as SplitterPanel; if (pnl == null) { return; } if (curImage == null) { return; } e.Graphics.DrawImage(curImage, imageOrignal); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void FormSnake_Load(object sender, EventArgs e) { buttonClear.Enabled = false; buttonSetContour.Enabled = false; buttonStart.Enabled = false; //buttonStop.Enabled = false; //buttonStep.Enabled = false; } private void buttonClear_Click(object sender, EventArgs e) { curImage = null; buttonClear.Enabled = true; buttonSetContour.Enabled = true; buttonStart.Enabled = true; //buttonStop.Enabled = false; //buttonStep.Enabled = false; // buttonLoadImage.Enabled = true; imageOrignal = new Point(0, 0); presetContour = null; mainContainer.Panel1.Refresh(); } bool isDrawingContour = false; bool isMouseDown = false; private void buttonSetContour_Click(object sender, EventArgs e) { isDrawingContour = true; buttonSetContour.Enabled = true; buttonStart.Enabled = true; presetContour = new List<Point>(); snakeSharp = SnakeSharp.FromImage(oriImage); curImage = new Bitmap(snakeSharp.CachedImage); mainContainer.Panel1.Refresh(); } private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e) { if (!isDrawingContour) { return; } else { isMouseDown = true; } } private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e) { if (isMouseDown) { isDrawingContour = false; isMouseDown = false; buttonSetContour.Enabled = true; buttonStart.Enabled = true; //buttonStep.Enabled = true; graphics = Graphics.FromImage(curImage); Pen p = new Pen(Color.Green); if (presetContour.Count > 1) { graphics.DrawLine(p, presetContour[0], presetContour[presetContour.Count - 1]); } graphics.Save(); mainContainer.Panel1.Refresh(); textBoxInfo.AppendText("\r\n边界设定完毕,共设定" + presetContour.Count + "个点。"); if (snakeSharp != null) { snakeSharp.SetCurcePoints(presetContour); } } } long mouseTick = 0; private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown && isDrawingContour) { long ticks = DateTime.Now.Ticks; if ((ticks - mouseTick) < 100 * 10000) { return; } mouseTick = ticks; Rectangle imageRec = new Rectangle(imageOrignal, curImage.Size); if (imageRec.Contains(e.Location)) { Point pt = new Point(e.Location.X - imageOrignal.X, e.Location.Y - imageRec.Y); presetContour.Add(pt); graphics = Graphics.FromImage(curImage); Pen p = new Pen(Color.Green); graphics.DrawRectangle(p, pt.X-1, pt.Y-1, 3, 3); if (presetContour.Count > 1) { graphics.DrawLine(p, presetContour[presetContour.Count - 2], presetContour[presetContour.Count - 1]); } graphics.Save(); mainContainer.Panel1.Refresh(); } } } private void buttonStart_Click(object sender, EventArgs e) { buttonStart.Enabled = false; buttonSetContour.Enabled = false; buttonClear.Enabled = false; //buttonStop.Enabled = true; //buttonStep.Enabled = false; isRunning = true; try { // snakeSharp.CurvatureThreshold = double.Parse(textBoxCurvatureThreshold.Text); // snakeSharp.GradientThreshold = byte.Parse(textBoxGradientThreshold.Text); // snakeSharp.PointsMovedThreshold = Int32.Parse(textBoxPtsmovedThreshold.Text); // snakeSharp.WillRemoveDead = checkBoxRemoveDeadpoint.Checked; backgroundWorker.RunWorkerAsync();//当执行BackgroundWorker.RunWorkerAsync方法时会触发DoWork该事件,并且传递DoWorkEventArgs参数 } catch (Exception ex) { MessageBox.Show(ex.Message); } } //private void buttonStop_Click(object sender, EventArgs e) //{ // backgroundWorker.CancelAsync(); // } // private void buttonStep_Click(object sender, EventArgs e)//单步运行 //{ // try //{ // int steps = (int)numericSteps.Value; //snakeSharp.CurvatureThreshold = double.Parse(textBoxCurvatureThreshold.Text); //snakeSharp.GradientThreshold = byte.Parse(textBoxGradientThreshold.Text); //snakeSharp.PointsMovedThreshold = Int32.Parse(textBoxPtsmovedThreshold.Text); //snakeSharp.WillRemoveDead = checkBoxRemoveDeadpoint.Checked; //int ptsMoved = 0; //while (steps > 0) //{ // ptsMoved = snakeSharp.RunSnake(); //textBoxInfo.AppendText("\r\n移动了" + ptsMoved + "个点。"); //steps--; //} //curImage = new Bitmap(snakeSharp.CachedImage); //graphics = Graphics.FromImage(curImage); //Pen p = new Pen(Color.Green); //if (snakeSharp.SnakePoints.Length > 0) //{ // CurvePoint[] snakePoints = snakeSharp.SnakePoints; // foreach (CurvePoint pt in snakePoints) //{ // graphics.DrawRectangle(p, pt.X-1, pt.Y-1, 3, 3); //} // for (int i = 0; i < snakePoints.Length; i++) //{ // graphics.DrawLine(p, snakePoints[i].Pos, snakePoints[(i + 1)%snakePoints.Length].Pos); //} //} //graphics.Save(); //mainContainer.Panel1.Refresh(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} //} public bool isRunning { get; set; } private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bgWorker = sender as BackgroundWorker; if (bgWorker == null) { return; } int ptsMoved = 0; while (!bgWorker.CancellationPending) { long ticks = DateTime.Now.Ticks; long currentTicks = DateTime.Now.Ticks; long interval = 500 * 10000; do { ptsMoved = snakeSharp.RunSnake(); currentTicks = DateTime.Now.Ticks; } while (currentTicks - ticks < interval && ptsMoved > snakeSharp.PointsMovedThreshold); WorkState state = new WorkState(); state.Points = new List<CurvePoint>(); foreach (CurvePoint pt in snakeSharp.SnakePoints) { state.Points.Add(pt); } bgWorker.ReportProgress(10, state);//触发ProgressChanged事件 if (ptsMoved <= snakeSharp.PointsMovedThreshold) { return; } } } private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { BackgroundWorker bgWorker = sender as BackgroundWorker; if (bgWorker == null) { return; } WorkState state = e.UserState as WorkState; if (state == null) { return; } curImage = new Bitmap(snakeSharp.CachedImage); graphics = Graphics.FromImage(curImage); Pen p = new Pen(Color.Green); if (state.Points.Count > 0) { foreach (CurvePoint pt in state.Points) { graphics.DrawRectangle(p, pt.X-1, pt.Y-1, 3, 3); } for (int i = 0; i < state.Points.Count ; i++) { graphics.DrawLine(p, state.Points[i].Pos, state.Points[(i + 1)%state.Points.Count].Pos); } } graphics.Save(); mainContainer.Panel1.Refresh(); } private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //buttonStop.Enabled = false; buttonStart.Enabled = true; buttonSetContour.Enabled = true; buttonClear.Enabled = true; //buttonStep.Enabled = true; isRunning = false; textBoxInfo.AppendText("\r\n异步处理完毕"); } private void textBoxPtsmovedThreshold_KeyPress(object sender, KeyPressEventArgs e) { int key = e.KeyChar; if ((key < 0x30 || key > 0x39) && key != 0x08 /*&& key != 46*/) { e.Handled = true; } } private void textBoxCurvatureThreshold_KeyPress(object sender, KeyPressEventArgs e) { int key = e.KeyChar; e.Handled = false; if ((key < 0x30 || key > 0x39) && key != 0x08 && key != 46) { e.Handled = true; return; } if (key != 46) { return; } // if (textBoxCurvatureThreshold.Text.Length <= 0) // { // e.Handled = true; // return; // } // else // { // float f = 0.0f; // String newStr = textBoxCurvatureThreshold.Text + e.KeyChar; // bool b = float.TryParse(newStr, out f); // e.Handled = !b; // } } private void textBoxCurvatureThreshold_KeyUp(object sender, KeyEventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//复选框 { //获取被选项目 MyComboItem item = comboBox1.SelectedItem as MyComboItem; //执行操作 if (comboBox1.SelectedItem.ToString() == "导入图片") { OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = ".bmp"; ofd.Filter = "Image Files(*.bmp,*.jpg,*.png)|*.bmp;*.jpg;*.png||"; if (DialogResult.OK != ofd.ShowDialog(this)) { return; } Image tmpImage = Image.FromFile(ofd.FileName); textBoxInfo.AppendText("\r\n图片加载成功(" + ofd.FileName + ")"); textBoxInfo.AppendText("\r\n图片大小:" + tmpImage.Width.ToString() + "X" + tmpImage.Height.ToString()); Size clientSize = mainContainer.Panel1.ClientSize; Size newImageSize = new Size(tmpImage.Width, tmpImage.Height); if (tmpImage.Width > clientSize.Width || tmpImage.Height > clientSize.Height) { double rImage = tmpImage.Width * 1.0 / tmpImage.Height; double rWnd = clientSize.Width * 1.0 / clientSize.Height; if (rImage < rWnd) // image more high { newImageSize.Height = clientSize.Height; newImageSize.Width = (int)(newImageSize.Height * rImage); } else //image is more wide { newImageSize.Width = clientSize.Width; newImageSize.Height = (int)(newImageSize.Width / rImage); } } textBoxInfo.AppendText("\r\n调整图片大小为:" + newImageSize.Width + "X" + newImageSize.Height); oriImage = new Bitmap(tmpImage, newImageSize); snakeSharp = SnakeSharp.FromImage(oriImage); Bitmap bmpTmp = new Bitmap(newImageSize.Width, newImageSize.Height); for (int i = 0; i < newImageSize.Height; i++) { for (int j = 0; j < newImageSize.Width; j++) { int val = (int)snakeSharp.ImageData[j, i]; if (val > 255) val = 255; if (val < 0) val = 0; bmpTmp.SetPixel(j, i, Color.FromArgb(val, val, val)); } } curImage = new Bitmap(bmpTmp, newImageSize); imageOrignal = new Point((clientSize.Width - curImage.Width) / 2, (clientSize.Height - curImage.Height) / 2); mainContainer.Panel1.Refresh(); buttonClear.Enabled = true; //buttonLoadImage.Enabled = true; buttonSetContour.Enabled = true; } else { ChildForm1 cf = new ChildForm1(); cf.Show(); } } } public class WorkState { List<CurvePoint> points; public List<CurvePoint> Points { get { return points; } set { points = value; } } }
}
解决 无用评论 打赏 举报
悬赏问题
- ¥15 vhdl+MODELSIM
- ¥20 simulink中怎么使用solve函数?
- ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
- ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
- ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
- ¥15 知识蒸馏实战博客问题
- ¥15 用PLC设计纸袋糊底机送料系统
- ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
- ¥15 用C语言输入方程怎么
- ¥15 网站显示不安全连接问题