gpt4 book ai didi

c# - 在 PictureBox 上画线

转载 作者:行者123 更新时间:2023-11-30 17:20:14 25 4
gpt4 key购买 nike

我的问题与 Stack Overflow 问题有关 Draw lines on a picturebox using mouse clicks in C# ,但是当鼠标按钮向上时,绘制的线消失了。我该如何解决这个问题?

private void GainBox_MouseDn(object sender, MouseEventArgs e)
{
mouse_dn = true;
}

private void GainBox_MouseMv(object sender, MouseEventArgs e)
{
//Line drawn from lookup table
if (mouse_dn)
{
img = new Bitmap(256, 256);

//Get the coordinates (x, y) for line from lookup table.

for (x = x1; x < x2; x++)
img.SetPixel(x, y, Color.Red);

//Same for y coordinate
}
GainBox.Refresh();
}

private void GainBox_MouseUp(object sender, MouseEventArgs e)
{
mouse_dn = false;
}

最佳答案

这是一个完整的小程序,它根据点绘制线(在本例中,它跟随鼠标)。我认为您可以将其重新加工成您需要的。

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


// Variable that will hold the point from which to draw the next line
Point latestPoint;


private void GainBox_MouseDown(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// Remember the location where the button was pressed
latestPoint = e.Location;
}
}

private void GainBox_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
using (Graphics g = GainBox.CreateGraphics())
{
// Draw next line and...
g.DrawLine(Pens.Red, latestPoint, e.Location);

// ... Remember the location
latestPoint = e.Location;
}
}
}
}

您的解决方案中的一个问题是您在临时位图中绘图,但该位图中的图像永远不会传输到您的 PictureBox。在此处介绍的解决方案中,不需要任何额外的位图。

关于c# - 在 PictureBox 上画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com