I am trying to get some pictures out of PicSet1.dll into a WPF image control. I am not sure how to put the bitmap into the control. Don't see any methods for this in the image class.
System.Reflection.AssemblyName aName;
aName = System.Reflection.AssemblyName.GetAssemblyName("c:\\Pic开发者_如何学PythonSet1.dll");
asm = System.Reflection.Assembly.Load(aName);
var result = asm.GetManifestResourceNames();
var bitmap= new System.Drawing.Bitmap(asm.GetManifestResourceStream(res[0]));
image1 = bitmap.
You can bind the Image in XAML to an ImageSource once you load the image from your DLL.
The key is to make sure your images in the reference DLL have a Build Action = Embedded Resource.
Here is the XAML:
<Window x:Class="LoadImage.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding EmbeddedPicture}"/>
</Grid>
</Window>
Here is the ViewModel:
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace LoadImage.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
System.Reflection.AssemblyName aName;
aName = System.Reflection.AssemblyName.GetAssemblyName("PicSet1.dll");
if (aName != null)
{
Assembly asm = System.Reflection.Assembly.Load(aName);
if ( asm != null )
{
string[] resources = asm.GetManifestResourceNames();
if ((resources != null) && (resources.Length > 0))
{
string name = resources[0];
if (!string.IsNullOrEmpty(name))
{
using (Stream myImage = asm.GetManifestResourceStream(name))
{
if (myImage != null)
{
using (System.Drawing.Image photo = System.Drawing.Image.FromStream((Stream) myImage))
{
MemoryStream finalStream = new MemoryStream();
photo.Save(finalStream, ImageFormat.Png);
// translate to image source
PngBitmapDecoder decoder = new PngBitmapDecoder(finalStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
EmbeddedPicture = decoder.Frames[0];
}
}
}
}
}
}
}
}
private ImageSource _embeddedPicture;
public ImageSource EmbeddedPicture
{
get
{
return _embeddedPicture;
}
set
{
_embeddedPicture = value;
OnPropertyChanged("EmbeddedPicture");
}
}
}
}
精彩评论