image control - dynamic load image from the resource file - wpf

I create in the code some image control and i want to set on this image control some of the picture that i saved ( load those pictures before creating the image control )
How can i do it ?

Specify your image in your Resource like this
<BitmapImage x:Key="SampleImageSource" UriSource="sample.png" />
and assign that to your image control like
<Image Source="{StaticResource SampleImageSource}" />
or if you are trying to do in code
img.Source = (ImageSource) Resources["SampleImageSource"];

Related

How to set Startup directory to a Image in xaml

Sorry for bad describe in the title. I have a image in "C:\Users\aUser\Desktop\Program\Image\Image.png"
But my program is in the same folder with the Image. I can set the Directory manually <Image Source="C:\Users\aUser\Desktop\Program\Image.png" But when the parent directory is moved, the code will no longer work. So how can I set the Source of the image that in the child folder without use of the code behind
Image.Source = new BitmapSource(new Uri(AppDomain.CurrentDomain.BaseDirectory + #"\Image\Image.png")
You will need to be more specific what XAML 'version' you are using (e.g. WPF, UWP or Xamarin.Forms). Anyways, here goes:
UWP
You should read through these docs. Your XAML code could look like this:
<Image Source="ms-appx:///Assets/Image.png"/>
Where Assets/Image.png is a path to your image
Xamarin.Forms
Have a look at these docs. The solution depends on the platform
WPF
Have a look at these docs. Your XAML code could look like this:
<Image>
<Image.Source>
<BitmapImage UriSource="/Images/image.png" />
</Image.Source>
</Image>

How to set our our custom image as icon in mahapps drop down button

I am using the Mahapps drop down button. I am unable to set the image that is local to my application as icon of the drop down button. How to achieve that. Currently am Maintaining my image source like below
<BitmapImage
x:Key="LoginBottom"
UriSource="pack://application:,,,/oApplication;component/Resources/LoginBottom.png" />
after that am refering that image like below
<Controls:DropDownButton
Icon="{StaticResource LoginBottom}"
>
But instead of the image am getting the urisource as text. which means whatever text is mentioned in the urisource is being shown in the drop down button.
You need to use an Image Control with your BitmapImage Resource.
Like so:
<Controls:DropDownButton>
<Controls:DropDownButton.Icon>
<Image Source="{StaticResource LoginBottom}" />
</Controls:DropDownButton.Icon>
</Controls:DropDownButton>

Load image from file to dynamic resource programmatically

I need to load an image file to WPF's dynamic resource programmatically because the directory which contains the image files can be moved.
How can I load an image to WPF's dynamic resource which is used like this: Source="{DynamicResource ...}" in XAML?
In XAML:
<Image Source="{DynamicResource MyDynamicImage}" />
In code:
var myimg = new BitmapImage(new Uri("SomeUriHere"));
Resources["MyDynamicImage"] = myimg;
(The Resources collection should be one which is in the scope of the Image of course, if you have a direct reference to the Image control you can also use the immediate Image.Resources)
Whenever you set a new object to that resource key the DynamicResource will update.

Use wpf image control to show an image from filesystem

I want to show an image from a file using an wpf image control. The image file resides in the application directory.
<Image Stretch="Fill" Source="dashboard.jpg" />
The file dashboard.jpg should be replaceable during or after deployment. How do I have to add the image to the project and what BuildAction do I have to use to have the image read from the file system rather than any source I cannot change after deployment. What source uri do I have to use?
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));
image1.Source = imageSource;
In markup:
<Image Stretch="Fill">
<Image.Source>
<BitmapImage UriSource="dashboard.jpg"/>
</Image.Source>
</Image>

How to display an image in Silverlight?

This should be simple, but...
I created a folder in my solution called Images. I dragged an image into it. How do I now display this image on a Page or View?
Make sure the image is set as a Resource. It can be in any folder in any of your projects in your solution.
You can then reference this as [assembly];component/[path]/[imagename.extension]
For example:
<Image Source="/mynamespace.myassembly;component/ResourcesFolder/image.png" Width="16" Height="16" />
There are a couple of ways to get at it--here's the way that involves setting the image as a Resource in the Visual Studio file properties:
using (var stream = Application.GetResourceStream(
new Uri("SilverlightAssemblyName;component/Images/myImage.png",
UriKind.Relative)))
{
// read from stream
}
Where SilverlightAssemblyName is replaced by the Assembly Name you specified in the Silverlight tab of your Silverlight project.
If you want to use the image in code:
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
myImageControl.ImageSource = bitmap;
Or, if you want to use the resource in XAML, you don't need any of the code:
<Image Source="/Images/myImage.png" Width="16" Height="16" />

Resources