开发者

C# serial port-picture box problem

开发者 https://www.devze.com 2023-03-22 11:32 出处:网络
In C# WinForms I\'m writing a program that reads values from serial port and according to the received value it displays an image in picture box: if the value is X, it will display image1 and if the v

In C# WinForms I'm writing a program that reads values from serial port and according to the received value it displays an image in picture box: if the value is X, it will display image1 and if the value is Y it will display image2 ... and so on.

When I received the first value, the program displays the first image v; good, but when the second value is received, no new image is d开发者_运维问答isplayed: only the same image displayed even the values changed.. the picturebox didn't change the image.

I try picturebox.Refresh() and picturebox.Invalidate() but that doesn't work.

Thanks

This is some code that sets images:

if (value == "X")
{
    path = "c:\\c#\\image1.png";
    pictureBox1.Invoke(new OutputUpdateDelegate (OutputUpdateCallback1),path);
}

if (value == "Y")
{
    path = "c:\\c#\\image2.png";
    pictureBox1.Invoke(new OutputUpdateDelegate(OutputUpdateCallback1), path);
}

delegate void OutputUpdateDelegate(string data);

private void OutputUpdateCallback1(string data)
{    
   pictureBox1.Image = Image.FromFile(data);
}


try this

if (value == "X")
{
    path = "c:\\c#\\image1.png";
    pictureBox1.Invoke(new OutputUpdateDelegate (OutputUpdateCallback1),path);
}

if (value == "Y")
{
    path = "c:\\c#\\image2.png";
    pictureBox1.Invoke(new OutputUpdateDelegate(OutputUpdateCallback1), path);
}

delegate void OutputUpdateDelegate(string data);

private void OutputUpdateCallback1(string data)
{    
   pictureBox1.Image = Image.FromFile(data);
}

I removed some bracket "}" which seemed placed wrong - although not sure because there is not enough source to be sure...

EDIT:

to further diagonse put a breakpoint on pictureBox1.Image = Image.FromFile(data); and see what the value of data is...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号