问题描述
我当前正在使用Cyotek图像框在图像上方绘制图形,并且想要变换图形(在平移事件上变换图形,在缩放事件上变换图形)。我的印象是我必须重写图像框类的方法,但是由于类非常广泛,我感到很迷惑。如果有人对此有经验,可以提供替代方法,或者可以提供有关此问题的任何见解,将不胜感激。下面是我在图像框中绘制的代码,以及绘制后图形外观的图像。
预先感谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Diagnostics;
namespace SPEL_Mi
{
public partial class TracerForm : Form
{
private List<Point> polygon;
private double scale;
private int counter = 0;
private readonly Graphics g;
private Point lastMousePos;
private bool _imageInserted = false;
private Matrix transform = new Matrix();
private float m_dZoomscale = 1.0f;
public const float s_dScrollValue = 0.1f;
private Stopwatch s = new Stopwatch();
private List<int> times = new List<int>();
public bool imageInserted
{
get { return _imageInserted; }
set { _imageInserted = value; }
}
public TracerForm()
{
InitializeComponent();
polygon = new List<Point>();
imageInserted = false;
scale = 1.0f;
g = imgCatchment.CreateGraphics();
lastMousePos = Point.Empty;
}
private void TracerForm_Load(object sender,EventArgs e)
{
}
public void setImage(Image image)
{
this.imgCatchment.Image = image;
}
private double distance(Point p1,Point p2)
{
return Math.Sqrt(Math.Pow(p2.X - p1.X,2) + Math.Pow(p2.Y - p1.Y,2));
}
// This function uses the shoelace formula to calculate the area of a polygon given it's vertices
private double calcArea(List<Point> vertices)
{
double area = 0.0f;
int j = vertices.Count - 1;
for (int i = 0; i < vertices.Count; i++)
{
area += (vertices[j].X + vertices[i].X) * (vertices[j].Y - vertices[i].Y);
j = i;
}
return Math.Abs(area / (2.0 * Math.Pow(scale,2)));
}
private void TracerForm_FormClosing(object sender,FormClosingEventArgs e)
{
if(Application.OpenForms.Count == 2)
{
Application.Exit();
}
}
public void DrawPoint(Graphics G,PointF pt,Pen pen)
{
using (SolidBrush brush = new SolidBrush(pen.Color))
{
float pw = pen.Width;
float pr = pw / 2f;
G.FillEllipse(brush,new RectangleF(pt.X - pr,pt.Y - pr,pw,pw));
}
}
private void imgCatchment_MouseDoubleClick(object sender,MouseEventArgs e)
{
if (polygon.Count > 2)
{
string area = Math.Round(calcArea(polygon),3).ToString();
//lblArea.Text = area + "sqm";
//lstSoureNodes.Items.Add(new ListViewItem(new String[] { area }));
}
else
{
imgCatchment.Invalidate();
}
polygon.Clear();
counter = 0;
}
private void imgCatchment_MouseMove(object sender,MouseEventArgs e)
{
lastMousePos = e.Location;
if (e.Button == MouseButtons.None)
{
if (polygon.Count >= 1)
{
imgCatchment.Invalidate();
g.DrawLine(new Pen(Color.Black,3),polygon[0],lastMousePos);
if (polygon.Count > 2)
{
g.DrawLine(new Pen(Color.Black,polygon[polygon.Count - 1],lastMousePos);
}
}
}
}
private void imgCatchment_MouseClick(object sender,MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (!imageInserted)
{
Interaction.MsgBox("Please insert an image first",MsgBoxStyle.Critical,"Image required");
}
else
{
if (counter < 2)
{
if (counter == 0)
{
//lblArea.Text = string.Empty;
imgCatchment.Invalidate();
polygon.Add(e.Location);
}
else
{
double pxlDistance = distance(polygon[0],e.Location);
double actualDistance;
try
{
actualDistance = Convert.ToDouble(Interaction.InputBox("What distance does this correspond to in metres?","Actual Distance"));
}
catch (FormatException)
{
Interaction.MsgBox("Numeric value required,actual distance set to 1","Numeric value required");
actualDistance = 1;
}
if (actualDistance < 0)
{
Interaction.MsgBox("Positive value required,"Positive value required");
actualDistance = 1;
}
else
{
scale = pxlDistance / actualDistance;
}
imgCatchment.Invalidate();
polygon.Clear();
}
counter++;
}
else
{
polygon.Add(e.Location);
}
}
}
}
private void imgCatchment_Paint(object sender,PaintEventArgs e)
{
if (polygon.Count >= 1)
{
if (polygon.Count > 1)
{
for (int i = 0; i < polygon.Count - 1; i++)
{
e.Graphics.DrawLine(new Pen(Color.Black,polygon[i],polygon[i + 1]);
}
e.Graphics.DrawLine(new Pen(Color.Black,lastMousePos);
}
e.Graphics.DrawLine(new Pen(Color.Black,lastMousePos);
}
}
}
}
'''
[Drawn graphics][1]
[1]: https://i.stack.imgur.com/JjKuE.jpg
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)