简体   繁体   中英

C#: Picture Box control not working properly

I am developing a new desktop application in C# using Windows Forms. In one of my form i put the "Picture Box" control which dynamically loads a new pic, each time when user performs a specific operation. The code for changing picture is as follow:

this.pictureBox1.Image = Image.FromFile(PicURI);

The only problem which i face is that some time it displays a red cross rather than displaying proper image. I debug the application and find that PicURI contains the proper path of image as expected so i don't understand where does the problem lies?

Edit: Change Code from

this.pictureBox1.Picture = Image.FromFile(PicURI);

to

this.pictureBox1.Image = Image.FromFile(PicURI);

这样尝试

this.pictureBox1.Image = Image.FromFile(PicURI);

Are you really using PictureBox ? it doesn't have Picture property. If you really mean PictureBox , you could use Image property as suggested by deepi

EDIT:

Red Cross Indicates that an Exception has been thrown inside. Since PictureBox has handled the exception, you are not aware of that. However, you can set the VS to break when exception occurs(even though it is handled), by checking the CheckBox Thrown for a particular exception on Debug -> Exceptions

Refer: http://msdn.microsoft.com/en-us/library/d14azbfh.aspx

He is using some user control with picture box in it. The Picture is public property in user control which return the image of pictureBox.

I think it display red cross because you had set the red cross image in user control's picture box. Some of the picture format are unreadable directly from Image.FromFile() and for that case it's showing same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM