1

I've been looking for a better answer to my problem, and even when I solved it (not very elegantly) I would like to know if there's any other way. I've inherited a project I cannot modify too much, I mean, I can only add some other properties or override methods but not changing the base class or interface.

This project contains a class that inherits a WPF grid to modify certain aspects of the behavior. I have to add a new column (by code) with an image. Every single image is already stored into a Resource assembly and declared through static properties, for example, an icon I need is declared like this:

MyIcon.Source="{x:Static images:Common.Size_22.icon}"

I need to use this icon for my column and I tried every kind of declaration through a package and it didn't work. Finally I decided to embed this Bitmap sending it through a property:

<mygrid:MyGridControl Model="{Binding}" Refresh="{Binding Refresh}" 
CommentsImageSource="{x:Static images:Common.Size_22.icon}" />

When I create the image cell I have to also create an image object and set source using a conversion between Bitmap and BitmapImage

image.Source = ToBitmapSource((System.Drawing.Bitmap)CommentsImageSource);

It works but I'm very disappointed with this resolution, is there any better way to do it? I couldn't reference the source file to avoid sending a Bitmap by property.

4

1 回答 1

5

当您有机会在代码中分配 ImageSource 时,我BitmapToBitmapSource(Resources.image_name)使用

public static BitmapSource BitmapToBitmapSource(Bitmap bitmap)
{
  return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

这将创建一个 BitmapSource,然后您可以将其指定为控件的 ImageSource。

于 2013-07-12T23:29:46.213 回答