I try use images from resources.
- I add one png image to resources, name heart.png. It’s public property.
- I expose resources with class as advice here: http://houseofbilz.com/archives/2009/03/15/binding-to-resources-in-silverlightwpf/
Here is class:
namespace Spirit.Util { using Properties;
public class PublicResources
{
private readonly static Resources Resources = new Resources();
public Resources SpiritResources { get { return Resources; } }
}
}
I add to app.xaml:
<Util:PublicResources x:Key="SpiritResources"/>
And try use on image control.
<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>
Frist problem is that image control is empty, why?
My complete goal is bind image from resources on image control with multibinding and multiconverter. If property Isfriend (Oponent.Info.IsFriend) is false I want convert image to grayscale.
Another problem is here. I use this converter class on conversion image to grayscale.
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 is type of System.Drawing.BitmapImage, I think that I can convert Bitmap to BitmapImage class with this simple class:
http://dog-net.org/content/development/wpf/system-drawing-bitmap-to-bitmapimage/
But I must solve first problem on the beggining. Thank for advice.
As your post is not taggeg as Silverlight, I've solved your problem in WPF application.
I just added existing PNG file to standart Resources. Then put Resources in XAML as static resource and binded PNG file's content to Image element:
<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>
My converter method looks like this (it's standart IValueConverter, but it doesn't matter):
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
In order to get grayscaled bitmap with keeping transparency I would recommend you use next method (from 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;
}
精彩评论