This question already has answers here:
WPF - Import image as resource
(3 answers)
How do I change an image source dynamically from code-behind in WPF with an image in Properties.Resources?
(1 answer)
Closed 4 years ago.
I have done quite a bit of research but still cannot figure this out.
I have made a program that I would like to include a jpg image as an embedded resource, in the exe file. The reason is I don't want anyone tampering or changing the image once the program is released, and also when I distribute the program it would be good if there was only one file to send.
This is what I have at the moment, which works fine when loading an image on the hard drive.
<Border.Resources>
<BitmapImage x:Key="Logo" UriSource="C:\Users\gmcco\Pictures\Development\Logo.jpg"/>
</Border.Resources>
<Grid>
<Image Source="{StaticResource Logo}"/>
</Grid>
I have loaded the image into the program and changed the properties to make it an embedded resource, but cannot seem to use it.
How do you reference a embedded image in XAML please?
Snapshot
Related
I'm making an app with custom visual design. My plan is to reuse the custom design elements for other future apps as well, so I thought the use of a resource dictionary in a separate file would be a good idea.
Somewhere in the resource dictionary file I have some BitmapImage objects for use in various controls.
<BitmapImage x:Key="xIconClose" CacheOption="OnLoad" UriSource="C:\_x\source\Refs\EpicStyle\IMG\Frame\xCross.png"/>
<BitmapImage x:Key="xCheckboxChecked" CacheOption="OnLoad" UriSource="C:\_x\source\Refs\EpicStyle\IMG\xCheckbox_checked.png"/>
<BitmapImage x:Key="xCheckboxUnchecked" CacheOption="OnLoad" UriSource="C:\_x\source\Refs\EpicStyle\IMG\xCheckbox_unchecked.png"/>
<BitmapImage x:Key="xHeaderBTNMask" CacheOption="OnLoad" UriSource="C:\_x\source\Refs\EpicStyle\IMG\Frame\HeaderBTN.png"/>
The question is, how do I embed these linked image files in the built application?
Currently, when I build the application and the try to run it, it still uses the files from those locations. I would like to have a result where all the needed files are either embedded in the exe or somehow packed in some other resource file that would come with the built exe.
How would I do that for files defined in a resource dictionary?
I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.
I just started work with winRT, shared projects etc. and faced with some problems.
I want to put all of my resources (strings, images etc.) in one project (portable class library or shared project).
When I put an image in PCL everything works fine in xaml by referencing with ms-appx:
<ImageBrush ImageSource="ms-appx:///Resources/Images/baby.jpg" Stretch="Uniform"/>
But when I put a string resource in PCL, I have following weird xaml behavior with ResourceDictionary:
http://postimg.org/image/ysbkvv6d7/
Ok. Then I decided to put every resources in Shared project. For this time it works perfectly with strings but fails with images: I couldn't get the right URI string in ImageSource of ImageBrush.
So the questions are:
How can I add ResourceDictionary in PCL
What is the correct URI format to reference image from shared project.
Thanks in advance!
To access an image your shared project file, just use the direct file path. The
<Image Source="Assets/ImageName.png"></Image>
Edited by #JerryNixon
<Image Source="ms-appx:///Assets/ImageName.png"></Image>
To access an image in your PCL, use the longer syntax
<Image Source="ms-appx:///ProjectName/...path.../"/>
This is a nubie question, I have a silverlight 5 (VB) application that runs fine and displays all 3 png images when I run it from my PC. When the app is put onto the web it doesn't show the png Images. The build I have selected for the png files is Resource? I checked the XAP.zip to see if the png files are there and they are.
I've done a bit of research and found that I may need some coding to enable me to get these up and running on the web unfortunately everything I've looked at is in C. I have found these two pieces of code :
<Imagex:Name="myIamge"Source="../computer.JPG"></Image>
myIamge.Source = new BitmapImage(new Uri("../Image/computer.JPG", UriKind.Relative));
Are these two pieces of code what I need?
Do I need them both together?
Does anyone know the conversion Of the second piece of code from c to vb
Is there anyone out there that can tell me how to do this in silverlight VB
Regards
Will
Are these two pieces of code what I need?
Probably, hard to say because you did not explain us where you put physically your 'computer.JPG'
Do I need them both together?
No, if you display this image only once, and never change it after, you do no need the code behind. Just use your xaml declaration.
So, what you have done seems pretty correct: your image should be declared as ressource, and you use the path to specify where the image is, compare to the XAML file using your image.
Thus, for instance if the declaration is in the MainPage.xaml (to the root of your project), and your image in an Image folder, this declaration is correct.
<Image x:Name="myImage" Source="Image/computer.jpg"/>
If despite this, you cannot see your image, I will recommand you to check the website where you deploy your xap file, maybe there is an error explaining why the image can not show up (MIME type, .net version?...)
I got a bunch of Pictures and Videos in a directory and needs to be displayed in the WPF application, I am thinking about displaying the Win7 Large Icon itself as the preview image. Because that will save me the work of grabbing an appropriate frame from the video file since Windows is doing that job pretty well already.
Any code snippet to access this Shell Icon image data?
When using the Windows API Code Pack, you can do:
XAML
<Image x:Name="img" />
C#
img.Source = ShellFile.FromFilePath(#"C:\path\to\file.ext").Thumbnail.BitmapSource;
This requires adding the Microsoft-WindowsAPICodePack-Shell package and the appropriate namespaces.