Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on a demo project on ZedGraph and get error on images. I checked the property Build Action of the two images, they are both "Emdedded Resource".

the code structure is as below:
XML
namespace ZedGraph.Demo
{
    /// <summary>
    /// Summary description for TransparentDemo1.
    /// </summary>
    public class TransparentDemo1 : DemoBase
    {
        public TransparentDemo1() : base( "A transparency demo", "Transparency Demo 1", DemoType.Special )
        {
....................................
			// Get an image for the background
			Image image = Bitmap.FromStream(GetType().Assembly.GetManifestResourceStream("ZedGraph.Demo.ngc4414.jpg"));
.....................................................
}

when I compile this project, always get error on this GetManifestResourceStream() statement:
Value of 'null' is not valid for 'stream'.

please help me on this issue related with embedding this image into resource file.

.........
Posted

To use resources reasonably, especially such resources as JPEG, you don't need to know any resource names, use any streams nothing like that. Everything will be done for you.

Create a new resource, say, Images.resx in in some directory. Have some ready to use JPEG file. Open the created resource and use "Add Resource" -> "Add Existing File", add your file. A new file will be generated "Images.Designer.cs". Open in this file. You will find a static property of the type System.Drawing.Bitmap, and the name of the property will be close to the name of the original JPEG file. Use it directly. You don't need to open anything, read, close — nothing. Just use the object. You can expect similar behavior with other well-known file types: text, other images, and a lot more. If the file type is not well-known, you can read it from memory stream, not resource stream.

Be careful not to add a file twice in your project. During this process, your file will be copied to your project directory or appropriate sub-directory, reference it added to the project (with the "copy" attribute "Do not Copy", because it is going to be embedded), and a reference added to the resource. If this file is already under your project directory structure but not at the exact location where it should be placed for adding to the resource, a duplicate copy may appear, so check it up. You can replace the file at any time, under the same name, and rebuild the project.

That's all.

—SA
 
Share this answer
 
v2
Comments
Southmountain 21-Feb-15 16:22pm    
this is a great solution. Thank you very much!
Sergey Alexandrovich Kryukov 21-Feb-15 20:31pm    
You are very welcome.
Good luck, call again.
—SA
You should get the executing assembly, then try to get the resource from the stream.

Also that resource location need to be right too.
C#
System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("ZedGraph.Demo.ngc4414.jpg");

// If the resource is not present it will be null or empty
if (null != file) {
    Image image = Image.FromStream(file);
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Feb-15 20:10pm    
Why all that?
—SA
Ramza360 20-Feb-15 20:20pm    
Yea good point just addin the resource as you have shown above does the trick however m solution does work for getting the embedded resource as shown by msdn. Dont think this was worth a down vote IMO.
Sergey Alexandrovich Kryukov 20-Feb-15 20:24pm    
It does work, tested many times. You have done something wrong. Apparently, I cannot see what. Anyway, you did not explain where that resource string name comes from. But this is a bad approach: a tiny mistake, and the compiler won't detect the problem. Sorry.
—SA
Southmountain 21-Feb-15 16:24pm    
somehow I can not get it right in first attempt and then take SA's solution.
Thank you both!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900