我正在创建一个使用 flycapture 相机的程序。我创建了一个扩展pictureBox 类的类,以便在屏幕上绘制一个由两条线组成的十字准线。我希望能够将十字准线从中心移动到屏幕上的任何其他位置。
问题是当窗体调整大小时,十字准线移动到不同的位置,如图所示。我希望十字准线与调整大小之前指向图像的同一部分(在示例中,它不再指向灰色网格)。我正在绘制与pictureBox的高度和宽度相关的十字准线。我希望能够在图像上绘制线条,但无论图像大小如何,图像的高度和宽度始终相同。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FlyCapture2SimpleGUI_CSharp
{
class IMSPictureBox : PictureBox
{
private Color colorSetting = Color.Black;
private float width = 1.0f;
public IMSPictureBox()
{
this.Paint += IMSPictureBox_Paint;
}
private void IMSPictureBox_Paint(object sender, PaintEventArgs e)
{
//Draw if image has loaded
if (this.Image != null)
{
//Draw horizontal line
e.Graphics.DrawLine(
new Pen(this.colorSetting, this.width),
new Point(0, this.Size.Height / 2 + 100),
new Point(this.Size.Width, this.Size.Height / 2 + 100));
//Draw vertical line
e.Graphics.DrawLine(
new Pen(this.colorSetting, this.width),
new Point(this.Size.Width / 2 + 100, 0),
new Point(this.Size.Width / 2 + 100, this.Size.Height));
}
}
}
}
编辑: 正如 DiskJunky 建议的那样,我现在正在绘制图像本身,而不是使用上面的 Paint 功能。
这是设置的图像:
private void UpdateUI(object sender, ProgressChangedEventArgs e)
{
UpdateStatusBar();
pictureBox1.SetImage = m_processedImage.bitmap;
pictureBox1.Invalidate();
}
这是在图像上绘制的线条:
public System.Drawing.Image SetImage
{
set
{
using (Graphics g = Graphics.FromImage(value))
{
g.DrawLine(new Pen(Color.Red, 3.0f), new Point(0, 0), new Point(value.Width, value.Height));
g.Dispose();
}
this.Image = value;
}
get
{
return this.Image;
}
}
我现在有一条与图像一起缩放的线,但现在它一直在闪烁。