Qorbaniのソリューションを機能させるには、ImageSource.Bindingにコンバーターを追加します。
XAML-名前空間
xmlns:properties="clr-namespace:YourNameSpace.Properties"
xmlns:converter="clr-namespace:YourNameSpace.Converter"
Xaml-リソース(UserControlまたはWindow)
<UserControl.Resources>
<ResourceDictionary>
<converter:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter" />
</ResourceDictionary>
</UserControl.Resources>
Xamlコード
<StackPanel Orientation="Horizontal">
<Image Width="32" Height="32" Source="{Binding Source={x:Static properties:Resources.Import}, Converter={StaticResource BitmapToImageSourceConverter}}" Stretch="Fill" />
<TextBlock Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">Import</TextBlock>
</StackPanel>
BitmapToImageSourceConverter.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace YourNameSpace
{
public class BitmapToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var bitmap = value as System.Drawing.Bitmap;
if (bitmap == null)
throw new ArgumentNullException("bitmap");
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var bitmapData = bitmap.LockBits(
rect,
ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
var size = (rect.Width * rect.Height) * 4;
return BitmapSource.Create(
bitmap.Width,
bitmap.Height,
bitmap.HorizontalResolution,
bitmap.VerticalResolution,
PixelFormats.Bgra32,
null,
bitmapData.Scan0,
size,
bitmapData.Stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}