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>
Related
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>
I've got resources in my assembly which I can Access using Properties.Resources.MyImage.
And I have some class which I bind to a WPF MenuItem containing a property
public System.Windows.Controls.Image Icon {get; set;}
This I want to set programmatically using:
dummy.Icon = Properties.Resources.MyImage;
Now I want to convert the resource System.Drawing.Image to the WPF System.Windows.Controls.Image. I thought this should be straightforward, but I found no working solution for my Images (which are png files using transparency).
So how do I convert System.Drawing.Image into System.Windows.Controls.Image?
Instead of using Properties.Resources, which are Windows Forms Embedded Resources, use WPF resources. In Solution Explorer, click the image file and in the properties window, set its Build Action to Resource (not Embedded Resource). This also embeds the image into the assembly, but in a different way.
Unlike Windows Forms, WPF does not generate a resource manager class, so you'd have to use strings to load the images dynamically:
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri("pack://application:,,,/NameOfAssembly;component/Path/To/Image.png");
image.EndInit();
Note the application and component parts of the URI are constant strings, while NameOfAssemly is the name of the assembly where the image is in. You can build a helper class that builds the URI and loads images.
You can also call image.Freeze() if you don't plan on making any changes to the image (improves performance and allows image source to be created on non-UI threads).
In your data class, expose an ImageSource property instead of an Image. Then you use the Image control to display it:
<Image Source="{Binding Icon}" />
Or inside a style:
<Style TargetType="MenuItem">
<Style.Resources>
<Image x:Key="Icon"
x:Shared="False"
Source="{Binding Icon}"
Width="16"
Height="16" />
</Style.Resources>
<Setter Property="Icon" Value="{StaticResource Icon}" />
</Style>
I am building an application using WPF for Windows 8 in VB.NET and I need to load several images from the disk.
I don't want to add the images as a reference to the project, I just want to load them as is.
I already studied the "Imaging Overview" at MSDN and several other articles over the internet but nothing solved my problem.
I used both XAML and code but nothing worked. I can't get the image presented.
Dim myimage As New Image
myimage.Width = 200
Dim bitmapimage As New BitmapImage()
bitmapimage.UriSource = New Uri("C:\Users\MyName\Documents\Database\image.jpg")
myimage.Source = bitmapimage
XAML code:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660">
<Image.Source>
<BitmapImage UriSource="C:\Users\MyName\Documents\Database\image.jpg"/>
</Image.Source>
</Image>
</Grid>
When I load a referenced image (ie. Assets/Logo.png) it loads it correctly.
I also added all the necessary capabilities and declerations in the application manifest.
Your XAML looks correct. Maybe the image path is invalid. However it could be written simpler like this:
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660"
Source="C:\Users\MyName\Documents\Database\image.jpg" />
The path string is automatically converted to an ImageSource instance.
In code you should use the BitmapImage constructor with Uri parameter. Otherwise you would have to call BeginInit and EndInit.
Dim uri As New Uri("C:\Users\MyName\Documents\Database\image.jpg")
Dim bitmapimage As New BitmapImage(uri)
myimage.Source = bitmapimage
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"];
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" />