Dynamic Image binding
-
Hi Gurus, I have an Image object in my main window XAML file. I need to associate an icon/image to this Image as per the user selection.
AppIcon is the Image object that i have defined in the class and this changes as per user. For e.f: if I select notepad, then the notepad's icon will be stored in this AppIcon object. However my main problem is that the icon is not getting displayed in the 'Image' object. How do I overcome this issue? Thanks in advance for your time and help.
Sunil
-
Hi Gurus, I have an Image object in my main window XAML file. I need to associate an icon/image to this Image as per the user selection.
AppIcon is the Image object that i have defined in the class and this changes as per user. For e.f: if I select notepad, then the notepad's icon will be stored in this AppIcon object. However my main problem is that the icon is not getting displayed in the 'Image' object. How do I overcome this issue? Thanks in advance for your time and help.
Sunil
Source does not accept Icon and Bitmap object, you have to convert it into BitmapImage. The code below is a converter to convert Bitmap to BitmapImage, the code below can convert Bitmap to propert BitmapImage.
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
public static BitmapSource loadBitmap(Bitmap source)
{
try
{
IntPtr hBitmap = source.GetHbitmap();
//Memory Leak fixes, for more info : http://social.msdn.microsoft.com/forums/en-US/wpf/thread/edcf2482-b931-4939-9415-15b3515ddac6/
try
{
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap);
}
}
catch
{
return new BitmapImage();
}}
Regards Joseph Leung
-
Source does not accept Icon and Bitmap object, you have to convert it into BitmapImage. The code below is a converter to convert Bitmap to BitmapImage, the code below can convert Bitmap to propert BitmapImage.
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
public static BitmapSource loadBitmap(Bitmap source)
{
try
{
IntPtr hBitmap = source.GetHbitmap();
//Memory Leak fixes, for more info : http://social.msdn.microsoft.com/forums/en-US/wpf/thread/edcf2482-b931-4939-9415-15b3515ddac6/
try
{
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap);
}
}
catch
{
return new BitmapImage();
}}
Regards Joseph Leung
Thanks Joseph. Well this is what I have implemented. Can you tell me whether this code will also lead to memory leaks.
Icon icon = Icon.ExtractAssociatedIcon(appPath);
if (null != icon)
{
BitmapImage bi = new BitmapImage();
MemoryStream stream = new MemoryStream();
Bitmap bmp = icon.ToBitmap();bi.BeginInit(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Seek(0, SeekOrigin.Begin); bi.StreamSource = stream; bi.EndInit(); MyObject app = new MyObject(); app.AppIcon = bi; }
Sunil
-
Thanks Joseph. Well this is what I have implemented. Can you tell me whether this code will also lead to memory leaks.
Icon icon = Icon.ExtractAssociatedIcon(appPath);
if (null != icon)
{
BitmapImage bi = new BitmapImage();
MemoryStream stream = new MemoryStream();
Bitmap bmp = icon.ToBitmap();bi.BeginInit(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Seek(0, SeekOrigin.Begin); bi.StreamSource = stream; bi.EndInit(); MyObject app = new MyObject(); app.AppIcon = bi; }
Sunil
Greetings, I am not sure about Icon, but I used your code and loaded a png without problem.
BitmapImage bi = new BitmapImage();
MemoryStream stream = new MemoryStream();
Bitmap bmp = new Bitmap(@"C:\Temp\Test.png");
bi.BeginInit();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);
bi.StreamSource = stream;
bi.EndInit();
AppIcon = bi;Check if it's the problem for loading Icon (try load a bmp or png). I suggest the loading code should be placed in MyObject, your BitmapImage may be created by different thread as MyObject. Regards Joseph Leung
-
Greetings, I am not sure about Icon, but I used your code and loaded a png without problem.
BitmapImage bi = new BitmapImage();
MemoryStream stream = new MemoryStream();
Bitmap bmp = new Bitmap(@"C:\Temp\Test.png");
bi.BeginInit();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);
bi.StreamSource = stream;
bi.EndInit();
AppIcon = bi;Check if it's the problem for loading Icon (try load a bmp or png). I suggest the loading code should be placed in MyObject, your BitmapImage may be created by different thread as MyObject. Regards Joseph Leung
-
Thanks Joseph. Well this is what I have implemented. Can you tell me whether this code will also lead to memory leaks.
Icon icon = Icon.ExtractAssociatedIcon(appPath);
if (null != icon)
{
BitmapImage bi = new BitmapImage();
MemoryStream stream = new MemoryStream();
Bitmap bmp = icon.ToBitmap();bi.BeginInit(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Seek(0, SeekOrigin.Begin); bi.StreamSource = stream; bi.EndInit(); MyObject app = new MyObject(); app.AppIcon = bi; }
Sunil
sunilkpv wrote:
Well this is what I have implemented. Can you tell me whether this code will also lead to memory leaks.
I dont think so - I dont see any calls to unmanaged code in there. However, it is always good practise to wrap a
MemoryStream
object within ausing
statement (as shown here). -
Thanks for your inputs. The code that I pasted works. There are no issues :) . I wanted to know if there could be possible memory leaks with my code....
Sunil
Greetings Sorry I assumed your code has error in it :) as Abhinav S said, there are no leaks, if you checked Bitmap.GetHbitmap() in MSDN you can find more information about the leak. Thanks for posting your code, I just learnt that I can set SourceStream in BitmapImage directly, but I believe my code use less memory and faster however (Bitmap vs Bitmap + MemoryStream). Regards Joseph Leung
-
Greetings Sorry I assumed your code has error in it :) as Abhinav S said, there are no leaks, if you checked Bitmap.GetHbitmap() in MSDN you can find more information about the leak. Thanks for posting your code, I just learnt that I can set SourceStream in BitmapImage directly, but I believe my code use less memory and faster however (Bitmap vs Bitmap + MemoryStream). Regards Joseph Leung