Display Picture in Picturebox

Bernie

Well-known member
Joined
Aug 13, 2007
Messages
98
Programming Experience
3-5
OK, this is so simple its embarrassing to ask. How do you display a picture in a picturebox?

I want to have a static picture displayed on my single form application. I tried adding a picturebox control to the form and setting the InitialImage setting to my pgn file. But when the app loads they image is not displayed. I've googled around and found many references to adding pictures out of databases and etc, but nothing as simple as my. I just want to display the one image.

Please help, I feel like an idiot.

Bernie
 
Did you read the documentation? If you had simply opened your MSDN Library to the index, typed picturebox into the Look For box, clicked on all members under the PictureBox Class entry and then looked down the list you'd have found that the InitialImage property is described as:
Gets or sets the image displayed in the PictureBox control when the main image is loading.
That is obviously not what you want. A little more looking and you'd have found the Load method:
Displays an image in the PictureBox.
and the Image property:
Gets or sets the image that is displayed by PictureBox.
All fairly clear and a few minutes work at most, but if you don't look then you won't see.

You've now got one method and one property. You can use either in code or set the Image property in the designer.
 
Why all the condesention? I told you I was struggling with this, I told you I had googled for a reference, I didn't tell you I struggled with this for over an hour. I read the documentation, I read the class documentation.

BTW, I did try the Image property and it didn't work. I got a greyed out/ghosted image. So please go drink some coffee and get over your over assuming pissy attitude!

Bernie
 
Thanks Matt!

I ran across both of those last night and was frustrated they didn't work in this application. This problem appears to be the image file. It's a PNG I got from the marketing house. If I double click on it in Windows it displays fine in the Windows Picture and Fax Viewer. In VS, it displays fine in design mode. When I run the app, it greys out.

The reason I say it's the image, I replaced the image with another PNG image I found on my system and it works fine. So I have to do more research on PNGs and why this one is different.

Bernie
 
Have you tried saving the image in a different format, like a GIF or JPG instead of PNG? I've had trouble with a couple of PNG files and .Net in the past.

Also, setting the Image property of the control does cause it to show the image, so it sounds like you're doing it right and it might just be a weird PNG format that the control's not reading correctly.

Edit: I guess I didn't refresh the tab when I returned to my comp this morning.
Further: You can use a program like Paint.Net to re-save a picture in another format.
 
I opened the image in Paint.net and saved it as a BMP. I then opened the BMP and converted it to PNG with Paint.net. That image works fine. So the problem appears to be with Photo Shops implementation of PNG. Thanks for the suggestion on Paint.net. I also had the same results using GIMP. So I think I switch to using those for image creation.

Thanks,
Bernie
 
BTW, I did try the Image property and it didn't work. I got a greyed out/ghosted image. So please go drink some coffee and get over your over assuming pissy attitude!
I must have missed the bit where you explained that problem before. The Image property is, as you now know, what you should be using. If you had already tried using it and had an issue then that would have been information worth providing. I see at least 20 questions a day that could be answered by a quick look at the relevant documentation. The question you asked was one of them, but the question you asked turned out to not be the problem you needed solved. With all the relevant information we could perhaps have addressed the actual problem sooner.
 
I guess you've never worked to late, run into a problem when you really needed something to have been done hours ago, got frustrated, and tried to reach out for help. Fortunately others on the board didn't jump to a snap judgement and offered what they had to help a fellow developer. Because they did, I found my problem, and maybe others in the future will see this and know there is a problem with picturebox's reading PNGs made by Photoshop.

Bernie
 
I guess you've never worked to late, run into a problem when you really needed something to have been done hours ago, got frustrated, and tried to reach out for help. Fortunately others on the board didn't jump to a snap judgement and offered what they had to help a fellow developer. Because they did, I found my problem, and maybe others in the future will see this and know there is a problem with picturebox's reading PNGs made by Photoshop.

Bernie
And hopefully you and others will post a more complete description of their issues in future. Then you and they can get the answers you and they need sooner. It's a win-win-win situation.
 
I had the same issue and I figured out what is the problem: metadata. If you did not include Copyright metadata in your PNG, then it will not load up on .NET picturebox and maybe in other components.

Solution? Fire up Photoshop and load the PNG, then use "Save for Web..." with settings PNG24 & Transparency and set Metadata to Copyright.
 
Back
Top