Microsoft Developer Network >
Página Inicial dos Fóruns
>
Windows Presentation Foundation (WPF)
>
Is there to store an encoded PNG in a Canvas?
Is there to store an encoded PNG in a Canvas?
- I have a system that uses Canvases as the base UI element. I am slowly moving of all of my resources to be Path based, but I still have a lot of material in raster images (mostly PNG). The systems needs to be able to have the Canvas be self-contained (i.e., it can't load resources from anywhere) - is there a way to store/encode the PNG within the Canvas markup? If not, is there a way to convert the PNG to a Path?
Thanks,
Erick
Respostas
- If you are serializing Xaml, I don't think you're going to be able to store images inside there.A better option would be to save all of your images in a seperate file (in a format of your choice or even as loose files).So you would have YourSerializedXaml.xaml and YourSerializedXaml.IMGS or something of that nature, and you would just have to manually load your images and stick them in as StaticResources.This is not an easy task I would say. I'm not sure if the Xaml serializer will keep StaticResource names for images inside Xaml or not.
- Marcado como Respostaerick.thompson quinta-feira, 2 de julho de 2009 21:53
Todas as Respostas
...needs to be able to have the Canvas be self-contained (i.e., it can't load resources from anywhere)
From anywhere? Can you use resources embedded in your application?
If so, add the png files to your project and make sure their build action is set to resource.
Then to use the images in a canvas you can use the Image element. For example, if you added an image called myimage.png to a project folder called Images, you could use it like this:
<Canvas > <Image Canvas.Left="10" Canvas.Top="10" Source="Images/myimage.png" /> </Canvas>
Mark Salsbery Microsoft MVP - Visual C++- That would work in some situations, but not mine. I can't reference resources embedded in the application, as these Canvas are application independent. Basically, these are XAML fragments that need to stand alone, be stored/serialized, and will be instantiated in many different applications (with different security contexts, so I can't even load from a Uri). What I am hoping is that there is some way to encode the binary data, and store that in the XAML (along with something that will decode it correctly).
Thanks,
Erick - Hi
are you talking about a adding a folder full of images and load it at runtime?!?
Have you got some code to show pleas paste it... Will be easyer to understand what you actually want that way...
Kenneth - If you are serializing Xaml, I don't think you're going to be able to store images inside there.A better option would be to save all of your images in a seperate file (in a format of your choice or even as loose files).So you would have YourSerializedXaml.xaml and YourSerializedXaml.IMGS or something of that nature, and you would just have to manually load your images and stick them in as StaticResources.This is not an easy task I would say. I'm not sure if the Xaml serializer will keep StaticResource names for images inside Xaml or not.
- Marcado como Respostaerick.thompson quinta-feira, 2 de julho de 2009 21:53
- I was afriad of that. I need something totally self-contained, but unless I want to code it up myself, I think I'm out of luck.
Thanks,
Erick - Well, it's definitely do-able.Make a Serializable/XmlSerializable class, that has:
- A string property for xaml
- A List<byte[]> of the raw png encoded images
- Come up with some standard way to map your images to where they go
Then you could serialize your whole class to a file.It is just a thought, and .Net has most of that built in... you will just have to put it all together.

