我尝试使用资源中的图片。
这是课程:
命名空间Spirit.Util { 使用属性;
public class PublicResources
{
private readonly static Resources Resources = new Resources();
public Resources SpiritResources { get { return Resources; } }
}
}
我添加到app.xaml:
<Util:PublicResources x:Key="SpiritResources"/>
尝试使用图像控制。
<Image Style="{StaticResource InfoIcon}">
<Image.Source>
<!--<MultiBinding Converter="{StaticResource imageToGrayConverter}">-->
<Binding Path="SpiritResources.heart" Source="{StaticResource SpiritResources}"/>
<!--<Binding Path="Oponent.Info.IsFriend" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>-->
</Image.Source>
</Image>
首要问题是图像控制是空的,为什么?
我的完整目标是使用多重绑定和多转换器从图像控制上的资源绑定图像。 如果属性Isfriend(Oponent.Info.IsFriend)为false,我想将图像转换为灰度。
另一个问题在这里。我在转换图像上将此转换器类用于灰度。
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();
}
}
Value是System.Drawing.BitmapImage的类型,我认为我可以使用这个简单的类将Bitmap转换为BitmapImage类:
http://dog-net.org/content/development/wpf/system-drawing-bitmap-to-bitmapimage/
但我必须先解决第一个问题。感谢您的建议。
答案 0 :(得分:0)
由于您的帖子不是标记为Silverlight,我已经解决了您在WPF应用程序中的问题。
我刚刚将现有的PNG文件添加到标准资源中。然后将资源放入XAML作为静态资源,并将PNG文件的内容绑定到Image元素:
<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>
我的转换器方法看起来像这样(它的标准IValueConverter,但没关系):
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;
}
<强> EDITED 强>
为了获得保持透明度的灰度位图,我建议您使用下一个方法(来自this article):
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;
}