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. C#
  4. Bitmap to BitmapImage Conversion Not Working - WPF

Bitmap to BitmapImage Conversion Not Working - WPF

Scheduled Pinned Locked Moved C#
graphicscsharpwpfperformancequestion
6 Posts 5 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.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.

        public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = null;
            using (var memory = new System.IO.MemoryStream())
            {
                bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                memory.Position = 0;
    
                bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.EndInit();
    
                return bitmapImage;
            }            
        }
    
      public void ShowPicture(Bitmap bmp)
      {
           pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
           return;
      }
    

    It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.

    This world is going to explode due to international politics, SOON.

    R M L V 4 Replies Last reply
    0
    • A AmbiguousName

      Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.

          public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
          {
              BitmapImage bitmapImage = null;
              using (var memory = new System.IO.MemoryStream())
              {
                  bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                  memory.Position = 0;
      
                  bitmapImage = new BitmapImage();
                  bitmapImage.BeginInit();
                  bitmapImage.StreamSource = memory;
                  bitmapImage.EndInit();
      
                  return bitmapImage;
              }            
          }
      
        public void ShowPicture(Bitmap bmp)
        {
             pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
             return;
        }
      

      It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.

      This world is going to explode due to international politics, SOON.

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      I don't know the answer to this, but Googling suggests others might: c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^] Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something. I'd so some Googling!

      Regards, Rob Philpott.

      A 1 Reply Last reply
      0
      • R Rob Philpott

        I don't know the answer to this, but Googling suggests others might: c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^] Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something. I'd so some Googling!

        Regards, Rob Philpott.

        A Offline
        A Offline
        AmbiguousName
        wrote on last edited by
        #3

        I did google. I know it is some small mistake that I am not seeing. But I did google alot. Thanks again.

        This world is going to explode due to international politics, SOON.

        1 Reply Last reply
        0
        • A AmbiguousName

          Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.

              public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
              {
                  BitmapImage bitmapImage = null;
                  using (var memory = new System.IO.MemoryStream())
                  {
                      bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                      memory.Position = 0;
          
                      bitmapImage = new BitmapImage();
                      bitmapImage.BeginInit();
                      bitmapImage.StreamSource = memory;
                      bitmapImage.EndInit();
          
                      return bitmapImage;
                  }            
              }
          
            public void ShowPicture(Bitmap bmp)
            {
                 pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
                 return;
            }
          

          It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.

          This world is going to explode due to international politics, SOON.

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          Here's the converter I wrote years ago. It converts a Bitmap to ImageSource (specifically a BitmapFrame):

          using System;
          using System.Drawing;
          using System.Globalization;
          using System.IO;
          using System.Runtime.InteropServices;
          using System.Windows.Data;
          using System.Windows.Media;
          using System.Windows.Media.Imaging;

          namespace ResourcesLibrary
          {
          [ValueConversion(typeof(Bitmap), typeof(ImageSource))]
          public class BitmapToImageSourceConverter : IValueConverter
          {
          [DllImport("gdi32.dll")]
          private static extern bool DeleteObject(IntPtr hObject);
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          Bitmap bmp = value as Bitmap;
          if (bmp == null)
          return null;
          using (Stream str = new MemoryStream())
          {
          bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
          str.Seek(0, SeekOrigin.Begin);
          BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
          return bdc.Frames[0];
          }
          }

          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
            throw new NotImplementedException();
          }
          

          }
          }

          "Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed." - G.K. Chesterton

          1 Reply Last reply
          0
          • A AmbiguousName

            Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.

                public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
                {
                    BitmapImage bitmapImage = null;
                    using (var memory = new System.IO.MemoryStream())
                    {
                        bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                        memory.Position = 0;
            
                        bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.StreamSource = memory;
                        bitmapImage.EndInit();
            
                        return bitmapImage;
                    }            
                }
            
              public void ShowPicture(Bitmap bmp)
              {
                   pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
                   return;
              }
            

            It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.

            This world is going to explode due to international politics, SOON.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Compared to my working code:

                    bitmapimage.BeginInit();
                    bitmapimage.StreamSource = memory;
                    **bitmapimage.CacheOption = BitmapCacheOption.OnLoad;**
                    bitmapimage.EndInit();
            

            and

            window.Content = imageControl;
            window.UpdateLayout();
            window.ShowDialog();

            Don't know enough about your app ... you may be able to apply .UpdateLayout() somewhere else in the visual tree.

            1 Reply Last reply
            0
            • A AmbiguousName

              Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.

                  public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
                  {
                      BitmapImage bitmapImage = null;
                      using (var memory = new System.IO.MemoryStream())
                      {
                          bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                          memory.Position = 0;
              
                          bitmapImage = new BitmapImage();
                          bitmapImage.BeginInit();
                          bitmapImage.StreamSource = memory;
                          bitmapImage.EndInit();
              
                          return bitmapImage;
                      }            
                  }
              
                public void ShowPicture(Bitmap bmp)
                {
                     pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
                     return;
                }
              

              It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine. What is wrong with my code. Thanks for any input.

              This world is going to explode due to international politics, SOON.

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              this should work:

              /// /// makes the image uniform between operations, size wise and concerning pixelformat.
              ///
              ///
              private BitmapImage SetModifiedImage(System.Drawing.Bitmap bmp){
              bmp = CreateNonIndexedImage(bmp);
              BitmapImage bmpi = new BitmapImage();
              bmpi.BeginInit();
              bmpi.StreamSource = new System.IO.MemoryStream((byte[])(new System.Drawing.ImageConverter()).ConvertTo(bmp, typeof(byte[])));
              bmpi.EndInit();
              return bmpi;
              }

              /// /// Removes the index color table of the image and makes it uniform.
              ///
              ///
              ///
              private System.Drawing.Bitmap CreateNonIndexedImage(System.Drawing.Bitmap bmp){
              System.Drawing.Bitmap newbmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
              System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newbmp);
              g.DrawImage(bmp, 0, 0);
              return newbmp;
              }

              You don't need the "CreateNonIndexedImage" normally. I had different sets of input and the difference in the PixelFormat gave me problems when working on the bytes of the bitmap. Hope this helps.

              V.

              (MQOTD rules and previous solutions)

              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