Silverlight 2: Code: 4001. Category: ImageError. Message: AG_E_NETWORK_ERROR
-
I've noticed a lot of posts on various blogs, etc about this error when referencing image files. Apparently it means that Silverlight can't find your file, or Silverlight doesn't support the format (.gif). I think like most people experimenting with Silverlight, I haven't formally studied the language, and so I miss many nuances. Here is one where I'm sure it is documented somewhere but I had to learn the hard way. I had a situation where I was loading an array of image names to be used as a series of ImageBrush elements, so I added the image files to the Properties folder through Blend, and reference them in my code-behind, as... "Properties/MyImage.png" however Silverlight didn't recognize them. I had other images referenced the same way in the XAML code and Silverlight had no problem. It turns out that if you reference an image file (or I assume any other Resource) in the XMAL, Silverlight compiles the Resource into the App.DLL. If they are only referenced in the code-behind this is not the case, so you must include a copy of the Properties folder relative to where the XAP file is run; in this case off my Bin directory. Hopefully this post will help others avoid my time consuming mistake, Steve
-
I've noticed a lot of posts on various blogs, etc about this error when referencing image files. Apparently it means that Silverlight can't find your file, or Silverlight doesn't support the format (.gif). I think like most people experimenting with Silverlight, I haven't formally studied the language, and so I miss many nuances. Here is one where I'm sure it is documented somewhere but I had to learn the hard way. I had a situation where I was loading an array of image names to be used as a series of ImageBrush elements, so I added the image files to the Properties folder through Blend, and reference them in my code-behind, as... "Properties/MyImage.png" however Silverlight didn't recognize them. I had other images referenced the same way in the XAML code and Silverlight had no problem. It turns out that if you reference an image file (or I assume any other Resource) in the XMAL, Silverlight compiles the Resource into the App.DLL. If they are only referenced in the code-behind this is not the case, so you must include a copy of the Properties folder relative to where the XAP file is run; in this case off my Bin directory. Hopefully this post will help others avoid my time consuming mistake, Steve
If you have static images to embed in your Silverlight app, you can just add them to your project and set the Build Action property of the image to "Resource". So, for example, if you add a folder to your project called Images, and add an image to that folder called Test1.png, you can access it like this:
<!-- From XAML -->
<Image Source="Images/Test1.png" Stretch="None" />// From code
BitmapImage bmp = new BitmapImage(new Uri("yourappmodulename;component/Images/Test1.png", UriKind.Relative));Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If you have static images to embed in your Silverlight app, you can just add them to your project and set the Build Action property of the image to "Resource". So, for example, if you add a folder to your project called Images, and add an image to that folder called Test1.png, you can access it like this:
<!-- From XAML -->
<Image Source="Images/Test1.png" Stretch="None" />// From code
BitmapImage bmp = new BitmapImage(new Uri("yourappmodulename;component/Images/Test1.png", UriKind.Relative));Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark, I appreciate your reply. Yes my images are marked with the Resource Build Action. Even if I add the yourappmodulename;component/ to the image file string, I still have no luck. With or without yourappmodulename;component/ being added to the string, it appears to create a valid BitmapImage (at least as far as I can see by examining the object in the watch window), but when it comes time to display it, I still get the same error in a Webpage Error messagebox. If I reference the same image files from XAML, then they are there for the code-behind as well. When I remove the XAML references, the error returns. The only way I have found to prevent this without adding superfluous references in the XAML, is by including the actual images as I indicated in my original post (although they are actually included relative to Bin/Debug, not Bin; i.e. Bin/Debug/Properties). I'm not sure what else I am doing or not doing that prevents your solution from working for me. Thanks again for the response, Steve
-
Mark, I appreciate your reply. Yes my images are marked with the Resource Build Action. Even if I add the yourappmodulename;component/ to the image file string, I still have no luck. With or without yourappmodulename;component/ being added to the string, it appears to create a valid BitmapImage (at least as far as I can see by examining the object in the watch window), but when it comes time to display it, I still get the same error in a Webpage Error messagebox. If I reference the same image files from XAML, then they are there for the code-behind as well. When I remove the XAML references, the error returns. The only way I have found to prevent this without adding superfluous references in the XAML, is by including the actual images as I indicated in my original post (although they are actually included relative to Bin/Debug, not Bin; i.e. Bin/Debug/Properties). I'm not sure what else I am doing or not doing that prevents your solution from working for me. Thanks again for the response, Steve
Just so I'm clear, you want embedded images in your app module, right? Or are the images external resources? If you are embedding images in the module: How are you adding images to your project? I can't add anything to the "Properties" folder in my projects...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Just so I'm clear, you want embedded images in your app module, right? Or are the images external resources? If you are embedding images in the module: How are you adding images to your project? I can't add anything to the "Properties" folder in my projects...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'd like them to be embedded, but other than using XAML references, I can't seem to force them to be. I added the image files to the Properties folder through Expression Blend. Thanks, Steve
SBJ wrote:
I added the image files to the Properties folder through Expression Blend.
Interesting....that may be the problem. For project management, I would highly recommend using Visual Studio. For packaging images with your app, using VS Solution Explorer, you can 1) Create a folder in the project (or use the project's root folder if you want - messier IMO) 2) Right click the folder, choose Add/Existing Item... and add the images. By default, the image's build action will be set to Resource - this will embed the image(s) in the assembly DLL. At this point you have options - 1) The default (as mentioned above) packages the image(s) in the assembly DLL. 2) You could change the image build type(s) to "Content" and the image(s) will be placed in the app's XAP file, separate from the DLL. 3) You could change the build type to "None", set the Copy to Output setting to copy always or copy if newer, and the image(s) will be left on the server - accessible using a URL. You would use this option for loading on demand instead of having the image(s) download with the XAP file at startup. This link shows all these options: Different ways for loading images and files in Silverlight 2 applications[^] That gives you control over how your images are included with the resulting application package, so you don't have to guess or rely on some implicit packaging of the images just because you referenced the image in XAML somewhere :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
SBJ wrote:
I added the image files to the Properties folder through Expression Blend.
Interesting....that may be the problem. For project management, I would highly recommend using Visual Studio. For packaging images with your app, using VS Solution Explorer, you can 1) Create a folder in the project (or use the project's root folder if you want - messier IMO) 2) Right click the folder, choose Add/Existing Item... and add the images. By default, the image's build action will be set to Resource - this will embed the image(s) in the assembly DLL. At this point you have options - 1) The default (as mentioned above) packages the image(s) in the assembly DLL. 2) You could change the image build type(s) to "Content" and the image(s) will be placed in the app's XAP file, separate from the DLL. 3) You could change the build type to "None", set the Copy to Output setting to copy always or copy if newer, and the image(s) will be left on the server - accessible using a URL. You would use this option for loading on demand instead of having the image(s) download with the XAP file at startup. This link shows all these options: Different ways for loading images and files in Silverlight 2 applications[^] That gives you control over how your images are included with the resulting application package, so you don't have to guess or rely on some implicit packaging of the images just because you referenced the image in XAML somewhere :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
Actually this project originated in Blend and was first loaded into VS through the Edit in VS menu command in Blend. When I right-click on the Properties folder in VS the only option I have is to Open it. All the other folders include the menu options you would expect. I think you may be right about Blend causing the problem. I can add that to the numerous other problems encountered when developing using the two packages together. Hopefully, VS 2010 and Blend 3 will be more stable! Thanks again for your help, Steve
-
Actually this project originated in Blend and was first loaded into VS through the Edit in VS menu command in Blend. When I right-click on the Properties folder in VS the only option I have is to Open it. All the other folders include the menu options you would expect. I think you may be right about Blend causing the problem. I can add that to the numerous other problems encountered when developing using the two packages together. Hopefully, VS 2010 and Blend 3 will be more stable! Thanks again for your help, Steve
SBJ wrote:
Hopefully, VS 2010 and Blend 3 will be more stable!
Indeed! I was going to say the same thing. I struggled using the two together at first and found it's best to do project creation and housekeeping with VS and use Blend just for the stuff it's good at. At least until they are 100% interchangeable with their project handling... Also, the latest service packs for both products fixed some of the issues I dealt with at first, so I'd definitely make sure to keep up-to-date on those :) Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: