Wpf 使用来自资源的图像-图像控件为空
我尝试使用资源中的图像Wpf 使用来自资源的图像-图像控件为空,wpf,image,binding,resources,converter,Wpf,Image,Binding,Resources,Converter,我尝试使用资源中的图像 我在参考资料中添加了一个png图像,名称为heart.png。这是公共财产 我在这里以类作为建议公开资源: 这是一节课: 名称空间Spirit.Util { 使用性能 public class PublicResources { private readonly static Resources Resources = new Resources(); public Resources SpiritResources { get { return R
public class PublicResources
{
private readonly static Resources Resources = new Resources();
public Resources SpiritResources { get { return Resources; } }
}
}
我添加到app.xaml:
<Util:PublicResources x:Key="SpiritResources"/>
值是System.Drawing.BitmapImage的类型,我想我可以使用以下简单类将位图转换为BitmapImage类:
但我必须解决乞丐的第一个问题。谢谢您的建议。由于您的帖子没有Silverlight的标签,我已经解决了您在WPF应用程序中的问题 我刚刚将现有的PNG文件添加到standart参考资料中。然后将XAML中的资源作为静态资源,并将PNG文件的内容绑定到图像元素:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:props="clr-namespace:WpfApplication1.Properties">
<Window.Resources>
<self:ImageConverter x:Key="Conv"/>
<props:Resources x:Key="Res"/>
</Window.Resources>
<StackPanel>
<Image Source="{Binding Source={StaticResource Res}, Path=dossier_ardoise_images, Converter={StaticResource Conv}}"/>
</StackPanel>
已编辑
为了获得保持透明度的灰度位图,我建议您使用下一种方法(从):
我尝试了与您相同的方法并得到错误:{“在类型'Spirit.Properties.Resources'上找不到匹配的构造函数。您可以使用参数或FactoryMethod指令来构造此类型。”}我看到了您的新线程,但您显然做错了什么,因为静态资源Res的声明放在app.xaml中,但是ChatView.xaml.Oki中发生了错误,我有一个问题,您可以使用转换方法将png图像转换为灰度,而不丢失透明背景吗?我试过了,我的背景是黑色的,这是细节,我可以用谷歌找到一些方法来实现这个目的。我添加了渲染灰度透明图像的解决方案。
public class ImageToGrayConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//string imageUri = values[0] as BimapImage;
//value is type of System.Drawing.Image
var image = values[0] as BitmapImage; //new BitmapImage(new Uri(imageUri, UriKind.Relative));
string s = values[1].ToString();
bool isLogged = System.Convert.ToBoolean(s);
if (!isLogged)
{
try
{
if (image != null)
{
var grayBitmapSource = new FormatConvertedBitmap();
grayBitmapSource.BeginInit();
grayBitmapSource.Source = image;
grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
grayBitmapSource.EndInit();
return grayBitmapSource;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
return image;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:props="clr-namespace:WpfApplication1.Properties">
<Window.Resources>
<self:ImageConverter x:Key="Conv"/>
<props:Resources x:Key="Res"/>
</Window.Resources>
<StackPanel>
<Image Source="{Binding Source={StaticResource Res}, Path=dossier_ardoise_images, Converter={StaticResource Conv}}"/>
</StackPanel>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Bitmap)
{
var stream = new MemoryStream();
((Bitmap)value).Save(stream, ImageFormat.Png);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
return bitmap; // use when you need normal image
var conv = new FormatConvertedBitmap();
conv.BeginInit();
conv.Source = bitmap;
conv.DestinationFormat = PixelFormats.Gray32Float;
conv.EndInit();
return conv; // use when you need grayed image
}
return value;
}
public static Bitmap MakeGrayscale(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Bitmap)
{
var stream = new MemoryStream();
MakeGrayscale((Bitmap)value).Save(stream, ImageFormat.Png);
//((Bitmap)value).Save(stream, ImageFormat.Png);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
return bitmap;
}
return value;
}