【问题标题】:using images from resources - image control is empty使用资源中的图像 - 图像控件为空
【发布时间】:2011-06-13 17:35:53
【问题描述】:

我尝试使用资源中的图像。

  1. 我将一张 png 图片添加到资源中,名称为 heart.png。这是公共财产。
  2. 我在这里公开资源与类作为建议: http://houseofbilz.com/archives/2009/03/15/binding-to-resources-in-silverlightwpf/

这是类:

命名空间 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/

但我必须首先解决第一个问题。感谢您的建议。

【问题讨论】:

    标签: wpf image binding resources converter


    【解决方案1】:

    由于您的帖子未标记为 Silverlight,因此我已在 WPF 应用程序中解决了您的问题。

    我刚刚将现有的 PNG 文件添加到标准资源中。然后将 XAML 中的 Resources 作为静态资源,并将 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;
        }
    

    已编辑

    为了获得保持透明度的灰度位图,我建议您使用下一种方法(来自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;
        }
    

    【讨论】:

    • 我尝试和你一样得到错误:{“在类型'Spirit.Properties.Resources'上找不到匹配的构造函数。你可以使用Arguments或FactoryMethod指令来构造这个类型。”}
    • 我看到了你的新帖子,但是你显然做错了,因为静态资源的声明Res放在app.xaml中,但是ChatView.xaml出现错误。
    • Oki,我有一个问题可以使用您的转换方法将 png 图像转换为灰度而不丢失透明背景?我试了一下,我的背景是黑色的,这是细节,我可以使用谷歌找到一些方法。
    • 我已经添加了渲染灰度透明图像的解决方案。
    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多