469,621 Members | 1,822 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

changing image on picturebox in user control runtime

hi All,
This problem is for C# windows application.
I have a user control whcih contains one picture box along with other
controls.
Depending on certain conditions I want to change images on the picturebox.
But at the same time I want to refer only assembly of the user control in
other solution.
If I am using method System.Drawing.Image.FromFile("filename") I have to put
gif files containing those images in bin directory of solution in whcih I am
using user control.
But I don't want that.
I want that only assebly needs to be referenced.
Is there any way for this.
If yes please send code snipet.
Thanks
Shailaja
Nov 15 '05 #1
3 29529
You can save your images as embedded resources and put them into the
picturebox by loading them from a resource stream.

Put your images in the solution, set their build action to "Embedded
resource" and then to load them use;

picBox.Image=Image.FromStream(this.GetType().Assem bly.GetManifestResourceStr
eam("<nameofassembly>.<nameofimage>");

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Shailaja Kulkarni" <cr*********@hotpop.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
hi All,
This problem is for C# windows application.
I have a user control whcih contains one picture box along with other
controls.
Depending on certain conditions I want to change images on the picturebox.
But at the same time I want to refer only assembly of the user control in
other solution.
If I am using method System.Drawing.Image.FromFile("filename") I have to put gif files containing those images in bin directory of solution in whcih I am using user control.
But I don't want that.
I want that only assebly needs to be referenced.
Is there any way for this.
If yes please send code snipet.
Thanks
Shailaja

Nov 15 '05 #2
Hi Bob,
thanks for your input, I tried this way but still I could not get the
desired result.
While I am trying to compile the project getting this exception message
" An exception occurred while trying to create an instance of
ShapeLibrary.BaseShape. The exception was "'null' is not a valid value for
'stream'.".
Whereas "ShapeLibrary" is name of assembly containing Embedded resources.
BaseShape is the user control in this Library in which I am putting image
from this perticular code above.
Have I missed anything?
Regards
Shailaja
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Ok**************@TK2MSFTNGP10.phx.gbl...
You can save your images as embedded resources and put them into the
picturebox by loading them from a resource stream.

Put your images in the solution, set their build action to "Embedded
resource" and then to load them use;

picBox.Image=Image.FromStream(this.GetType().Assem bly.GetManifestResourceStr eam("<nameofassembly>.<nameofimage>");

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Shailaja Kulkarni" <cr*********@hotpop.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
hi All,
This problem is for C# windows application.
I have a user control whcih contains one picture box along with other
controls.
Depending on certain conditions I want to change images on the picturebox. But at the same time I want to refer only assembly of the user control in other solution.
If I am using method System.Drawing.Image.FromFile("filename") I have to put
gif files containing those images in bin directory of solution in whcih

I am
using user control.
But I don't want that.
I want that only assebly needs to be referenced.
Is there any way for this.
If yes please send code snipet.
Thanks
Shailaja


Nov 15 '05 #3
You have to ensure that you use the fully qualified name for the resource.
This will depend on your solution structure.

The resource name is probably something like "MyApplication.MyImage.bmp"

If you can't devine the resource name add a bit of temporary code to the
constructor...

public MyClass
{
InitializeComponent();

//tempororary code
string[] names=this.GetType().Assembly.GetManifestResourceN ames();
foreach(string s in names)
System.diagnostics.Trace.WriteLine(s);
//temporary code
}

This will dump all the names to the output pane and you can see how they're
named. Once you know, delete the temporary code.
--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Shailaja Kulkarni" <cr*********@hotpop.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Hi Bob,
thanks for your input, I tried this way but still I could not get the
desired result.
While I am trying to compile the project getting this exception message
" An exception occurred while trying to create an instance of
ShapeLibrary.BaseShape. The exception was "'null' is not a valid value for
'stream'.".
Whereas "ShapeLibrary" is name of assembly containing Embedded resources.
BaseShape is the user control in this Library in which I am putting image
from this perticular code above.
Have I missed anything?
Regards
Shailaja
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Ok**************@TK2MSFTNGP10.phx.gbl...
You can save your images as embedded resources and put them into the
picturebox by loading them from a resource stream.

Put your images in the solution, set their build action to "Embedded
resource" and then to load them use;

picBox.Image=Image.FromStream(this.GetType().Assem bly.GetManifestResourceStr
eam("<nameofassembly>.<nameofimage>");

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Shailaja Kulkarni" <cr*********@hotpop.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
hi All,
This problem is for C# windows application.
I have a user control whcih contains one picture box along with other
controls.
Depending on certain conditions I want to change images on the picturebox. But at the same time I want to refer only assembly of the user control in other solution.
If I am using method System.Drawing.Image.FromFile("filename") I have
to put
gif files containing those images in bin directory of solution in

whcih I
am
using user control.
But I don't want that.
I want that only assebly needs to be referenced.
Is there any way for this.
If yes please send code snipet.
Thanks
Shailaja



Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ed Landau | last post: by
15 posts views Thread by Anand Ganesh | last post: by
5 posts views Thread by Jeff | last post: by
1 post views Thread by M West | last post: by
7 posts views Thread by lgbjr | last post: by
1 post views Thread by Filippo Bettinaglio | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.