none
How to Add a BitmapImage into a resource file(.resx)?

    Question

  • Hi,

     I am trying to add a BitmapImage to a resourcefile, but it says Bitmapimage cannot be serialized and hence ResourceWriter.Generator does not work.
     
     I have the file path of the image so I can save the Image as a System.Drawing.Image into the .resx file, but
     while displaying I am attaching the Source of System.Windows.Controls.Image to this retrieved System.Drawing.Image which I am not able to typecast.
     
     Pls provide a solution to either coonvert a System.Drawing.Image to a System.Windows.Controls.Image or a solution for
     saving a BitmapImage into the resource file.

    Thanks,
    Mani
    Wednesday, November 07, 2007 7:13 PM

Answers

  • Hi Mani,

    This is a tad hacky but does what you're looking for: (Converting System.Drawing.Image to System.Windows.Controls.Image)

     

    Code Block

    // Winforms Image we want to get the WPF Image from...

    System.Drawing.Image imgWinForms = System.Drawing.Image.FromFile("test.png");

    // WPF version of Image

    System.Windows.Controls.Image imgWpf = new System.Windows.Controls.Image();

    // Source for the image...

    BitmapImage bi3 = new BitmapImage();

    bi3.BeginInit();

    MemoryStream ms = new MemoryStream();

    // Save to a memory stream...

    imgWinForms.Save(ms, ImageFormat.Bmp);

    // Rewind the stream...

    ms.Seek(0, SeekOrigin.Begin);

    // Tell the WPF image to use this stream...

    bi3.StreamSource = ms;

    bi3.EndInit();

    // And your WPF image is ready to go! (good idea to close that stream once your image is loaded...)

    imgWpf.Source = bi3;

     

     

     

    ... that said, you can probably just use a .dll to store your images if you dont want to do this workaround.  Not sure why you need .resX.

    Wednesday, November 07, 2007 10:16 PM
    Moderator
  •    You can also this the following method to do the conversion:

    public static BitmapSource ConvertToBitmapSource(System.Drawing.Bitmap gdiPlusBitmap)

    {

        IntPtr hBitmap = gdiPlusBitmap.GetHbitmap();

        return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

    }

     

       Hope this helps
    Friday, November 09, 2007 6:49 AM

All replies

  • Hi Mani,

    This is a tad hacky but does what you're looking for: (Converting System.Drawing.Image to System.Windows.Controls.Image)

     

    Code Block

    // Winforms Image we want to get the WPF Image from...

    System.Drawing.Image imgWinForms = System.Drawing.Image.FromFile("test.png");

    // WPF version of Image

    System.Windows.Controls.Image imgWpf = new System.Windows.Controls.Image();

    // Source for the image...

    BitmapImage bi3 = new BitmapImage();

    bi3.BeginInit();

    MemoryStream ms = new MemoryStream();

    // Save to a memory stream...

    imgWinForms.Save(ms, ImageFormat.Bmp);

    // Rewind the stream...

    ms.Seek(0, SeekOrigin.Begin);

    // Tell the WPF image to use this stream...

    bi3.StreamSource = ms;

    bi3.EndInit();

    // And your WPF image is ready to go! (good idea to close that stream once your image is loaded...)

    imgWpf.Source = bi3;

     

     

     

    ... that said, you can probably just use a .dll to store your images if you dont want to do this workaround.  Not sure why you need .resX.

    Wednesday, November 07, 2007 10:16 PM
    Moderator
  •    You can also this the following method to do the conversion:

    public static BitmapSource ConvertToBitmapSource(System.Drawing.Bitmap gdiPlusBitmap)

    {

        IntPtr hBitmap = gdiPlusBitmap.GetHbitmap();

        return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

    }

     

       Hope this helps
    Friday, November 09, 2007 6:49 AM

  • Hello

         Try to this code.

    Uri iconUri = new Uri("pack://application:,,,/ImageName.ico", UriKind.RelativeOrAbsolute);
                        NotifyIcon.Icon = BitmapFrame.Create(iconUri);

    Regards

    Bharat Thanki

    Monday, July 05, 2010 9:03 AM
  • Nice
    Friday, May 13, 2011 3:03 PM