locked
BitmapImage From BitmapSource RRS feed

  • Question

  •  

    How to create or convert BitmapImage from BitmapSource?
    Tuesday, October 2, 2007 6:35 AM

Answers

  • Hello, if you want to create a BitmapSource from a BitmapImage, please try this SDK sample:

    // Create the image element.

    Image simpleImage = new Image();   

    simpleImage.Width = 200;

    simpleImage.Margin = new Thickness(5);

     

    // Create source.

    BitmapImage bi = new BitmapImage();

    // BitmapImage.UriSource must be in a BeginInit/EndInit block.

    bi.BeginInit();

    bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);

    bi.EndInit();

    // Set the image source.

    simpleImage.Source = bi;

     

    If you want to do the reverse, you can use a RenderTargetBitmap and save the image to a temporary file:

               RenderTargetBitmap rtb = new RenderTargetBitmap((int)image1.ActualWidth, (int)image1.ActualHeight, 96, 96, PixelFormats.Pbgra32);

                rtb.Render(image1);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();

                encoder.Frames.Add(BitmapFrame.Create(rtb));

                using (FileStream o = File.Open("d:\\temp.jpg", FileMode.Create))

                {

                    encoder.Save(o);

                }

                BitmapImage bi = new BitmapImage();

                bi.BeginInit();

                bi.UriSource = new Uri("d:\\temp.jpg", UriKind.Absolute);

                bi.EndInit();

     

    Thursday, October 4, 2007 4:55 AM

All replies

  • Not sure if this is exactly what u are looking for..
    But I found this sample on MSDN

    http://msdn2.microsoft.com/en-us/library/ms752364.aspx
    Tuesday, October 2, 2007 6:50 AM
  • This is not exactly what i am looking for.

     

    Tuesday, October 2, 2007 7:23 AM
  • Hello, if you want to create a BitmapSource from a BitmapImage, please try this SDK sample:

    // Create the image element.

    Image simpleImage = new Image();   

    simpleImage.Width = 200;

    simpleImage.Margin = new Thickness(5);

     

    // Create source.

    BitmapImage bi = new BitmapImage();

    // BitmapImage.UriSource must be in a BeginInit/EndInit block.

    bi.BeginInit();

    bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);

    bi.EndInit();

    // Set the image source.

    simpleImage.Source = bi;

     

    If you want to do the reverse, you can use a RenderTargetBitmap and save the image to a temporary file:

               RenderTargetBitmap rtb = new RenderTargetBitmap((int)image1.ActualWidth, (int)image1.ActualHeight, 96, 96, PixelFormats.Pbgra32);

                rtb.Render(image1);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();

                encoder.Frames.Add(BitmapFrame.Create(rtb));

                using (FileStream o = File.Open("d:\\temp.jpg", FileMode.Create))

                {

                    encoder.Save(o);

                }

                BitmapImage bi = new BitmapImage();

                bi.BeginInit();

                bi.UriSource = new Uri("d:\\temp.jpg", UriKind.Absolute);

                bi.EndInit();

     

    Thursday, October 4, 2007 4:55 AM
  • I will take a look at this.

    Thanks.

     

    Thursday, October 4, 2007 7:28 AM
  • Where can I find this sample?

    My Visual Studio can't find BitmapImage class in Silverlight app. And there is no System.Windows.Media.Imaging namespace...

    Wednesday, October 10, 2007 3:55 PM