none
How to convert System.Drawing.image to System.Windows.Media.BitmapImage?

    Question

  • Hi.

    How to convert System.Drawing.image(or System.Drawing.Bitmap) to System.Windows.Media.BitmapImage?

    what's the diff about them?

    Saturday, July 14, 2007 9:30 AM

Answers

  • Hello, try this:

                FileStream fs = File.Open("test.jpg", FileMode.Open);

                System.Drawing.Bitmap dImg = new System.Drawing.Bitmap(fs);

                MemoryStream ms = new MemoryStream();

                dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();

                bImg.BeginInit();

                bImg.StreamSource = new MemoryStream(ms.ToArray());

                bImg.EndInit();

    //img is an Image control.

                img.Source = bImg;

     

    As for their difference, System.Drawing.Bitmap uses GDI, while System.Windows.Media.Imaging.BitmapImage uses DirectX.

    Monday, July 16, 2007 5:58 AM

All replies

  • Hello, try this:

                FileStream fs = File.Open("test.jpg", FileMode.Open);

                System.Drawing.Bitmap dImg = new System.Drawing.Bitmap(fs);

                MemoryStream ms = new MemoryStream();

                dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();

                bImg.BeginInit();

                bImg.StreamSource = new MemoryStream(ms.ToArray());

                bImg.EndInit();

    //img is an Image control.

                img.Source = bImg;

     

    As for their difference, System.Drawing.Bitmap uses GDI, while System.Windows.Media.Imaging.BitmapImage uses DirectX.

    Monday, July 16, 2007 5:58 AM
  • hello,

    it's work.

    thanks a lot

    Monday, July 16, 2007 8:59 AM
  • Hi, Robert A. Wlodarczyk

     

    i saw you sample code with c# ,

    how to convert bitmapimage to bitmap with vb?

    Friday, July 20, 2007 2:31 AM
  • Hi, could you please tell me how to do this the other way around?
    i have a System.Windows.Media.Imaging.BitmapImage and i want to convert it to System.Drawing.Image or .Bitmap
    I´m looking for a way to read in those stupid .dcr files ;) Read in works, but i´m having trouble with converting.
    Regards, Elisha
    Wednesday, July 30, 2008 11:38 AM
  • Anyone, please?
    Thursday, September 04, 2008 7:16 AM
  • Hi, LadyElisha
    try this:

    System.Drawing.Bitmap MakeNet2BitmapFromWPFBitmapSource(BitmapSource src)
            {           
                try
                {
                    MemoryStream TransportStream = new MemoryStream();
                    BitmapEncoder enc = new BmpBitmapEncoder();               
                    enc.Frames.Add(BitmapFrame.Create(src));              
                    enc.Save(TransportStream);               
                    return new System.Drawing.Bitmap(TransportStream);               
                }
                catch { MessageBox.Show("failed"); return null; }
            }
    Yuri
    Thursday, September 04, 2008 7:26 PM
  • Hi, LadyElisha
    try this:


    Bless your heart, Yuri, it works perfectly.
    Friday, October 30, 2009 9:19 AM