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);
开发者_StackOverflow中文版
Try like this
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.
精彩评论