Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Dynamic Image binding

Dynamic Image binding

Scheduled Pinned Locked Moved WPF
helpwpfquestionwcf
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sunil P V
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • S Sunil P V

      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

      L Offline
      L Offline
      Leung Yat Chun
      wrote on last edited by
      #2

      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

      QuickZip

      S 1 Reply Last reply
      0
      • L Leung Yat Chun

        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

        QuickZip

        S Offline
        S Offline
        Sunil P V
        wrote on last edited by
        #3

        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

        L A 2 Replies Last reply
        0
        • S Sunil P V

          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

          L Offline
          L Offline
          Leung Yat Chun
          wrote on last edited by
          #4

          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

          QuickZip

          S 1 Reply Last reply
          0
          • L Leung Yat Chun

            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

            QuickZip

            S Offline
            S Offline
            Sunil P V
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • S Sunil P V

              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

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              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 a using statement (as shown here).

              1 Reply Last reply
              0
              • S Sunil P V

                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

                L Offline
                L Offline
                Leung Yat Chun
                wrote on last edited by
                #7

                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

                QuickZip

                S 1 Reply Last reply
                0
                • L Leung Yat Chun

                  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

                  QuickZip

                  S Offline
                  S Offline
                  Sunil P V
                  wrote on last edited by
                  #8

                  Thanks Gurus.

                  Sunil

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups