I need to load a single jpg image to a windows form at run time.
file name / path is a given.
Any insight on how to do this?

Recommended Answers

All 7 Replies

Well, there are plenty of methods to do this, one I would try first before other things would be to make a picture box, and make the default image the image you want to display, set the default Visible property to false. Then during runtime, set Visible to true, read any resize event and resize the pictureBox accordingly. A complex method you could do would be to alter the OnPaint or OnPaintBackground method of the form, and manually place an image as the background during on of these functions, I believe OnPaint, I have never actually done that with an image before, but I am sure its possible. Hope this helps :D

Thank you.
At least now I have something that I can try.
I did a lot of reading ref. the OnPaint subject and thought it much more trouble than it was worth.

This is a much more eloquent solution that I will try shortly.

Thanks again.

Nice try WargRider...no cigar.
I tried your suggestion and basically it behaves like an embedded image i.e. a resource that is "compiled into the executable".

I am trying to do something for a friend with early onset Alzheimer.
This person has several (6) children.
We don't have recent photos for all of them just yet but...

I want to set up the app such that it looks for a JPG file for each child and if the ChildName.jpg file exists then load it to a picturebox or something similar.

As more photos become available we can drop them where they belong and have them appear in the form.

For the long haul, as time passes, we can just drop fresh pictures where they belong and have current memory prompts.

I think the key word here is LOAD the file(s) at run time much like any other data file is loaded.

All insights will be greatly appreciated.

The System.Drawing name space holds a class called Image. It is used as a type and has instance methods. It cannot be directly initialized but is easily used a runtime.

Image pic = Image.fromFile("path to image");
then you can show the image anyway you like, as the image property of a picturebox or in a drawimage method in the paint event.

or even short cut it. Picturebox1.Image = Image.FromFile("where file is");

But more importantly the picturebox has a ImageLocation property that is a string. set that to a path to a file, and it will load the image.

All of these methods work from run time, not build time.

Bingo!
Thank You!
This works exactly how I had hoped it would.
I set the picture box Visible or not Visible depending on whether or not the corresponding image is available.

Thanks again!

Bingo!
Thank You!
This works exactly how I had hoped it would.
I set the picture box Visible or not Visible depending on whether or not the corresponding image is available.

Thanks again!

Congratulations, I am sorry my suggestion did not work, and I am happy your found a working one ;), but explaining that there would be more than ONE picture would have helped me give you a better solution, I was thinking you were having some kind of splash logo display on a load or something, not multiple pictures slideshow kind of thing, but I wish you the best of luck and your friend the best of luck.

At first I thought you had the answer and I will keep your input for use in other things that I am trying to do.

I have been writing software since 1970 but am new to the C# dot net world as if the whole world does not know that.

On the up-side, your input gave me reason to hope that I could finger this thing out.

Thanks again.

Congratulations, I am sorry my suggestion did not work, and I am happy your found a working one ;), but explaining that there would be more than ONE picture would have helped me give you a better solution, I was thinking you were having some kind of splash logo display on a load or something, not multiple pictures slideshow kind of thing, but I wish you the best of luck and your friend the best of luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.