As you migrate your business solution application from previous .NET Winform/ASP.NET to microsoft WPF/Silverlight and you wish to reused the existing satellite resource dll which you have created in the pass.
Due to the image object is different between the .NET WinForm/ASP.NET (Bitmap) and WPF/Silverlight (BitmapImage). Which mean, we are not able to call GetObject() in ResourceManager and assign the image into the
BitmapImage object in WPF/Silverlight.
BitmapImage bmpRaw = (BitmapImage)ResourceManager.GetObject("MyImage");
Instead, we have to load the return Bitmap object into MemoryStream before further assign to the BitmapImage object via the BitmapImage.StreamSource property.
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmpRaw.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.StreamSource = stream;
Again, here we have another hidden trap in the MemoryStream object! That is the MemoryStream data position (data address pointer) is set as end of data position after the Bitmap.Save()
Therefore, we must move the data position back to the begin position before we further use it in BitmapImage.StreamSource()
stream.Seek(0, System.IO.SeekOrigin.Begin);
Below is the complete sample code
private void LoadResxImage(Image ctrl, String id)
{
ResourceManager resxMgr = new ResourceManager("devCode.ImageResx.Resources", Assembly.LoadFile(this.Path + "devCode.ImageResx.dll"));
System.Drawing.Bitmap bmpRaw = (System.Drawing.Bitmap)resxMgr.GetObject(id);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmpRaw.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
if (bmpRaw != null)
bmpRaw.Dispose();
bmpRaw = null;
stream.Seek(0, System.IO.SeekOrigin.Begin);
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = stream;
bmp.EndInit();
ctrl.Source = bmp;
stream.Flush();
stream.Close();
stream = null;
resxMgr.ReleaseAllResources();
}

ImageFromResxDll.zip (612 kb)
c681babc-145f-4828-8042-f1f40b286c4e|0|.0
Quick Tips
WPF, Silverlight