wpf - 如何从我创建的资源中获取图像源 [英] wpf - How to get image source from resource i created
问题描述
我在项目的资源文件夹中创建了一个资源文件,名为Class1 ...
i created a resource file in the resource folder of my project named Class1...
在我的窗口中我只有一个图像(在网格内)我想要通过c#代码改变它的来源。
in my window i have only a image (inside a grid) i want to change its source through c# code.
我试过的:
MyImage.Source = (ImageSource) Hotel_Reservation_Application.Resources.Class1.imageA;
[Hotel_Reservation_Application.Resources.Class1.imageA]我得到位图图像而不是源码我应该怎么做?
[Hotel_Reservation_Application.Resources.Class1.imageA] i gets the bitmap image and not the source what should i do?
推荐答案
嗨hassaan,
Hi hassaan,
如果你想改变图片源代码背后,你可以看看。 Photo是我的应用程序中的文件夹,此文件夹中有一些图像。
If you want to change image source by code behind, you can take a look. The Photo is the folder in my application, there are some images in this folder.
xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<Button
Name="btnshow"
Width="200"
Height="30"
Click="btnshow_Click"
Content="btnshow" />
<Image
Name="image1"
Grid.Row="1"
Width="300"
Height="100"
Source="../Photo/1.jpg" />
</Grid>
xaml.cs:
private void btnshow_Click(object sender, RoutedEventArgs e)
{
image1.Source = new BitmapImage(new Uri(@"/Photo/2.jpg", UriKind.Relative));
}
最好的问候,
Cherry
这篇关于wpf - 如何从我创建的资源中获取图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!