l46*_*kok 10 .net c# wpf resources image
我在我的Resources.resx中包含了一个图标文件,我想在一个堆栈面板内的TreeViewItem上显示.
1).ico文件可用于此目的吗?或者它必须是.bmp或jpg?
2)你在XAML中设置了什么来源?以下代码对我不起作用
<StackPanel Orientation="Horizontal">
<Image Margin="2" Source="/Resources/Folder_Back.ico" />
<TextBlock Margin="2" Text="{Binding ProgramName}"
Foreground="White" FontWeight="Bold"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
Qor*_*ani 12
这是访问资源文件中的图像的技巧:
首先,您需要添加对项目属性的引用,如下所示:
xmlns:properties="clr-namespace:MyProject.Properties"
Run Code Online (Sandbox Code Playgroud)
然后通过XAML访问它,如下所示:
<image source="{Binding Source={x:Static properties:Resources.ImageName}}" />
Run Code Online (Sandbox Code Playgroud)
您可以使用PNG/JPG/BMP以及ICO文件,但每个人都推荐使用PNG.
使Qorbani工作的解决方案添加一个转换器到Image Source.Binding!
XAML - 命名空间
xmlns:properties="clr-namespace:YourNameSpace.Properties"
xmlns:converter="clr-namespace:YourNameSpace.Converter"
Run Code Online (Sandbox Code Playgroud)
Xaml - 资源(UserControl或Window)
<UserControl.Resources>
<ResourceDictionary>
<converter:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter" />
</ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
Xaml代码
<StackPanel Orientation="Horizontal">
<Image Width="32" Height="32" Source="{Binding Source={x:Static properties:Resources.Import}, Converter={StaticResource BitmapToImageSourceConverter}}" Stretch="Fill" />
<TextBlock Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">Import</TextBlock>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
BitmapToImageSourceConverter.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace YourNameSpace
{
public class BitmapToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var bitmap = value as System.Drawing.Bitmap;
if (bitmap == null)
throw new ArgumentNullException("bitmap");
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var bitmapData = bitmap.LockBits(
rect,
ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
var size = (rect.Width * rect.Height) * 4;
return BitmapSource.Create(
bitmap.Width,
bitmap.Height,
bitmap.HorizontalResolution,
bitmap.VerticalResolution,
PixelFormats.Bgra32,
null,
bitmapData.Scan0,
size,
bitmapData.Stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
你不能这样做.这只适用于winforms
有关详细信息,请参阅此帖子
使用这篇文章中显示的方法
代替
引用:
如果您将在多个位置使用图像,那么将图像数据仅加载到内存中然后在所有Image
元素之间共享它是值得的.
为此,BitmapSource
在某处创建一个资源:
<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />
Run Code Online (Sandbox Code Playgroud)
然后,在您的代码中,使用以下内容:
<Image Source="{StaticResource MyImageSource}" />
Run Code Online (Sandbox Code Playgroud)
就我而言,我发现我必须将Image.png
文件设置为具有构建操作Resource
而不仅仅是Content
.这会导致图像在编译的程序集中携带.
归档时间: |
|
查看次数: |
21701 次 |
最近记录: |