I'm working with .NET Maui for the first time, and I already have problems with the Image. So when starting the default page, there is this robot image. I put another image into the /Image folder, and replaced it with the robot image in the xaml file.
I added this to my page:
<Image Source="Logo.png"
WidthRequest="250"
HeightRequest="250"
HorizontalOptions="Center"/>
But the image is not displayed on the page, the place where the image is is just blank. I don't know what I did wrong, because everyone said that it should work if the filename is correct, and the Image is in the right folder.
What am I missing?
From Image / Load a local image:
Images can be added to your app project by dragging them to the Resources\Images folder of your project, where its build action will automatically be set to MauiImage.
Sometimes it is necessary to Close Solution, then re-open it, for VS to update its list of included images.
This is the line in .csproj that "automatically" sets build action for all images in folder Resources\Images:
<MauiImage Include="Resources\Images\*" />
Related
I'm using Visual Studio 2019 Community
How I added the .png to my project.
Open Resources.resx in the project
Click Add Resource -> Add Existing File
Select the .png file to add
It is added to the program. I can see it in resources. It exists in my designer and displays properly in it.
There are other images used in this program that have the exact same properties and that I can call at any time and they work just fine. However, these new ones do not. They are both .png files. I have tried cleaning and rebuilding the solution multiple times as well.
Code for my xaml:
<Image Source="Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>
Resources Properties:
Build Action = Embedded Resource
Copy to Output Directory = Do not copy
Image Properties
Persistence = Linked at compile time (same for working and not working resources)
You should mark the Build Action as Resource if you want to access it via folder, otherwise(in your case Embedded Resource) you would have to access the application pack with a more complicated URI.
1) Change build action in the image properties to Resource.
2) If your image is located in the Resources folder of your project
you can refer to it like below:
<Image Source="/Resources/FileName.png" Width="50" HorizontalAlignment="Left" Margin="5,5,0,0"/>
More detailed information is located in the Microsoft Docs here: Pack URIs in WPF
Set Build action to Resource,then use the following code
<Image Source="Pack://Application:,,,/Resources/FileName.png"/>
I am trying to deployment a setup for my WPF c# application. i suppose to do it using clickonce because it allow to auto update when i do any update in my application.
Actually i create a setup for my application. when i install that app in a different computer, it works...when i do some update my app, it automatically detect when program launching and user can install new update and run the app.
But it does not show any gif images when program running. when i install the app in a different computer, it create a desktop shortcut and start menu icon. when i run the program via shortcut or start menu, it does not show any gif images. it work fine without showing gif images.
app installed in
C:\Users\dilan\AppData\Local\Apps\2.0\TA5EZNVO.D56\TY5K2KDD.7B6\rcu_..tion_d1f05f20b6f0835a_0001.0000_d9a8ddd982f2efb7
i went to this location and double click the exe file. then app is running with showing gif images.but it does not update. update only happen when run the app via desktop short cut or start menu.
i tested the app by putting image files in to Images folder , Resources folder...but it does not work..
i think i missed some points when creating setup file. can anyone help me , how to deploy a setup file using clickonce , that app work with gif iamges also.
Here is how XAML looks like:
<Image gif:AnimationBehavior.SourceUri="pack://siteoforigin:,,,/Images/tapcard.gif"
VerticalAlignment="Center" Width="100" Margin="0,0,20,0"/>
I might not have inculded your images in your project.
See this answer. You need to right click the file in Solution Explorer and then Include in project .
When you include them, the Build Action of the image file would be set to Resource - no need to copy images individually (Copy to Output Directory should be by default set to Do not copy).
Let's say you have a WPF project in the folder Images named Progress.gif.
In code behind you reference it like this:
new BitmapImage(new Uri("pack://application:,,,/Images/Progress.gif", UriKind.Absolute))
In XAML you reference the image simply like this
<Image Source="./Images/Progress.gif" >
You can use another URI scheme as well, e. g. pack://siteoforigin:,,,/Images/tapcard.gif refers to an image that is located on the server where the app was deployed.
If you do not want to be bound by the restrictions of having your
application resources declared at compile time, there is another
option for you. No, this doesn't involve using fully qualified Uris to
reference resources over the internet. Although, that is indeed
supported. WPF provides you with an abstraction for the application's
conceptual site of origin i.e. the location from where the application
was deployed. For instance, if your application was launched from
http://nerddawg.blogspot.com, then your application's site of origin
is http://nerddawg.blogspot.com. To access an image at
images/AuntDahlia.gif at that location, you would specify in markup:
<Image Source="pack://siteoforigin:,,,/images/AuntDahlia.jpg" />
So you check in the properties of your WPF project on Publis tab the Publish location or Installation folder URL (if specified). If installation URL is e.g. http://myserver:8080 then you need to verify from the client, that the file http://myserver:8080/Images/tapcard.gif exists by typing this url into a browser. Depending on the folder structure of your web you might need to copy the file manually there.
pack://siteoforigin: means you are getting the resource over the network from the web or file server.
I added some images as resources to my WPF project, changed their Build Action to Resource in Solution Explorer and added them to my Main Window like this:
<Image ... Source="Resources/user.png"/>
Everything was working until I added and removed one project from my solution, then these images stopped showing both in design and runtime. The <Image Source/> tag in XAML editor and the Error List now show the message:
Could not find a part of the path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Resources\user.png".
I tried restarting Visual Studio and the computer, and also cleaned and rebuilt it, but I still don't get the images.
When you use <Image Source="" />, the Source is relative to the XAML file path. The XAML file was on the root, and I moved it to a View folder. I didn't know about this until I read this comment.
The images are stored in a Resources folder inside the project, so in this case the Source property must start with a forward slash to point it to root, like this:
<Image ... Source="/Resources/user.png"/>
I have an image in a Asset class library project. The build action is Content and the Copy to Output Directory setting is Copy Always because I want to be able to allow users to replace these files without my intervention.
The problem is, I'm getting a design-time error (everything works find on compile and run) that says "Could not find a part of the path 'C:\Users\Name\AppData\Local\Microsoft\VisualStudio\12.0\Designer\ShadowCache\oreaxsnr.cjd\orsr3pqd.wnf\Images\TestImage.jpg'."
I'm using Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); to get the path of the assembly, and then I concatenate that with a \\Images\\TestImage.jpg string. Thinking that was the source of the problem, I commented out everything related to getting that image, cleaned the solution, and rebuilt. It still has a problem with finding part of the path.
I went to the orsr3pqd.wnf folder and there's the class library .dll that calls the image, and nothing else. I deleted all of the ShadowCache folder after closing Visual Studio, restarted VS2013, and it immediately put two folders in the ShadowCache folder.
Maybe it's thinking there's supposed to be a resource in the .dll, even though I'm setting the build action to Content? Any direction would be helpful.
You should be able to reference it straight from the XAML if it is in a project that is part of the application like this:
<Image Source="/AssemblyName;component/Images/TestImage.jpg" />
If that doesn't work, then this should:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,
/AssemblyName;component/Images/TestImage.jpg"/>
</Image.Source>
</Image>
If neither of these work for you, then I'd also suggest setting your image file Build Action to Resource, rather than Content.
The Source attribute of the Image tag works only with the files which were added to the 'Assets' folder in the Solution Explorer.
I've got two files in the Assets directory: Logo.png and Logo2.png, but only Logo.png is added to 'Assets' in the Solution Explorer. This image works properly:
<Image Source="/Assets/Logo.png"/>
while this image doesn't show up:
<Image Source="/Assets/Logo2.png"/>
File paths like 'C:\Users\user\Pictures\img.jpg' don't work either.
Is it possible to access images which are not specified in the 'Assets' folder in the Solution Explorer?
EDIT:
Here is what I do:
Create new project in VS2012: Visual C# -> Windows Store -> Blank App (XAML)
Open MainPage.xaml
Choose Image from Toolbox and draw an image element in the Design mode
The only working value for the Source attribute is '/Assets/Logo.png'. These are not working: 'C:\images\img.jpg', '/Assets/Logo2.png', 'Project_name;component/Assets/Logo.png', 'Project_name;component/Assets/Logo2.png', etc.
Here is the format you will want to use:
Project;component/ImagePath
Where Project is the name of the assembly (project in most cases) that you want to reference, and "component" specifies that the assembly being referred to is referenced from the local assembly.
So for the Logo2, you would do:
<Image Source="ImageDemo;component/Assets/Logo2.png" Name="custLogo"/>
Where "ImageDemo" is the project name and "Assets/Logo2.png" is the path inside project.
As for absolute paths, the following worked just fine for me:
<Image Source="C:\Images\001.jpg"></Image>
Ok, here's what I've found out:
Windows Store Apps (Win8) have got limited access to files. So I can only use the files which are shipped with the application. However, it is possible to get the user's files if he chooses them in a file picker.
There is also a great sample here.