Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ToolStripLabel不会立即重新绘制_C#_.net_Windows Forms Designer - Fatal编程技术网

C# ToolStripLabel不会立即重新绘制

C# ToolStripLabel不会立即重新绘制,c#,.net,windows-forms-designer,C#,.net,Windows Forms Designer,我有一个应用程序,它可以打开一个图像并将其显示在窗口中。确切地说,我将此图像存储在表单类的内部字段中,并使用放置在该表单上的面板的事件的绘制方法绘制它。是这样的: private void pnlImage_Paint(object sender, PaintEventArgs e) { var g = e.Graphics; if (image != null) { g.DrawImage(image, imageLocation); } }

我有一个应用程序,它可以打开一个图像并将其显示在窗口中。确切地说,我将此图像存储在表单类的内部字段中,并使用放置在该表单上的
面板的
事件的
绘制
方法绘制它。是这样的:

private void pnlImage_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    if (image != null)
    {
        g.DrawImage(image, imageLocation);
    }
}
private void pnlImage_MouseMove(object sender, MouseEventArgs e)
{
    UpdateCursorAtLabel(e.X, e.Y); // <---- method of interest
    UpdateColorLabel(e.X, e.Y); // other label, showing color of the pixel. It's an identical thing, so I did not mention it
}
变量
image
是表单类的字段(类型
image
),包含打开后的图像<代码>图像位置
连接到滚动条和滚动条,这与本例无关

在那张表格的底部我还放了一个
工具条
,在这个
工具条
上有一些
工具条标签
,它们应该是一些关于图像的指示器。例如,有一个
ToolStripLabel
,我想在图像上显示鼠标光标的当前位置,或者从另一个角度看,显示鼠标光标下的像素位置(在图像上)。因此,我在
MouseMove
事件附加的方法中更新标签的值,如下所示:

private void pnlImage_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    if (image != null)
    {
        g.DrawImage(image, imageLocation);
    }
}
private void pnlImage_MouseMove(object sender, MouseEventArgs e)
{
    UpdateCursorAtLabel(e.X, e.Y); // <---- method of interest
    UpdateColorLabel(e.X, e.Y); // other label, showing color of the pixel. It's an identical thing, so I did not mention it
}
lblCursorAtValue
-这是应该显示鼠标下像素位置的标签,例如“234117”。
SCB水平
SCB垂直
-只是一些滚动条,不相关,我只需要它们来计算位置。
nullText
-
const string
,等于“--”


一开始,没有使用
Invalidate()
方法。当我完成这一切时,我注意到,当我将光标移到面板上时,标签的内容根本不会改变!只有当我停止移动光标时,我才能看到标签的新值。所以我添加了
lblCursorAtValue.Invalidate()
来强制重新绘制标签。还是一样的问题。我甚至还检查了一件事——在这个标签的
Paint
事件中附加了一个方法,没有填充它,并在那里设置了一个断点。在运行时,调试器仅在我停止移动光标或将其移出面板边界后才停止。
ToolStripLabel
上没有
Update()
Refresh()
等方法,因此我不能使用比
Invalidate()
更强的方法。我真的不明白为什么会发生这种情况,我该如何解决它。

解决了它。我忘了一件重要的事。而且
UpdateColorLabel(int,int)
方法并不像一开始看起来的那样无关紧要。因为它就像:

void UpdateColorLabel(int cursorX, int cursorY)
{
    if (image == null)
    {
        lblColorValue.Text = nullText;
    }
    else
    {
        var imageCursorLocation = new Point(scbHorizontal.Value + cursorX, scbVertical.Value + cursorY);
        var px = new Bitmap(image).GetPixel(imageCursorLocation.X, imageCursorLocation.Y);
        lblColorValue.Text = String.Format("R = {0}, G = {1}, B = {2}", px.R, px.G, px.B);
    }            
}
里面有一条线,把一切都搞乱了:

var px = new Bitmap(image).GetPixel(imageCursorLocation.X, imageCursorLocation.Y);
是的,在这一行中,我创建了一个全新的位图,每次鼠标移动一个像素创建位图相当耗时。所以应用程序并没有时间更新标签的外观,因为它正忙于创建数百个位图

我通过存储位图而不是图像来修复它,所以我不必创建它来获得像素颜色