none
Convert bitmapImage to Image RRS feed

  • Question

  • Hello,

    With the WPF C#.NET example ( Developing a .NET Application Using Bing Maps SOAP Services ), I need to convert the map (BitmapImage) in image
    but the conversion in image does not work : image size is 1x1 pixel!

    private void RequestImage_Click(object sender, RoutedEventArgs e) { // Make label hidden and image visible labelResults.Visibility = Visibility.Hidden; imageResults.Visibility = Visibility.Visible; //Get URI and set image int imgHeight = (int)imageResults.Height; int imgWidth = (int)imageResults.Width; String imageURI = GetImagery(textInput.Text, zoomText.Text, imgHeight, imgWidth); imageResults.Source = new BitmapImage(new Uri(imageURI)); // BitmapImage myBI = new BitmapImage(new Uri(imageURI)); myBI.DecodePixelHeight = imgHeight; myBI.DecodePixelWidth = imgWidth; saveImage(myBI); }

            private void saveImage(BitmapImage bi)
            {
                using (FileStream stream = new FileStream(@"C:\Users\Olivier\Documents\imgBingMaps.jpeg", FileMode.Create))
                {
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bi));
                    encoder.Save(stream);
                    stream.Close();
                }
            }
    I see that my variable is not good :
                myBI.DecodePixelHeight = imgHeight;
                myBI.DecodePixelWidth = imgWidth;

    Is it my problem there?
    Thank you for your help.



    • Edited by Bernardo769 Wednesday, August 29, 2012 3:46 PM
    Wednesday, August 29, 2012 6:54 AM

Answers

All replies

  • I specify my tests:
    I modified the example like this :

    * myBI is now global

        public partial class MainWindow : Window
        {
    
            BitmapImage myBI;
    	...
    

           private void RequestImage_Click(object sender, RoutedEventArgs e)
            {
                // Make label hidden and image visible
                labelResults.Visibility = Visibility.Hidden;
                imageResults.Visibility = Visibility.Visible;
    
                //Get URI and set image
                int imgHeight = (int)imageResults.Height;
                int imgWidth = (int)imageResults.Width;
                String imageURI = GetImagery(textInput.Text, zoomText.Text, imgHeight, imgWidth);
    
                //imageResults.Source = new BitmapImage(new Uri(imageURI));
                //
                myBI = new BitmapImage(new Uri(imageURI));
                myBI.DecodePixelHeight = imgHeight;
                myBI.DecodePixelWidth = imgWidth;
                myBI.UriSource = new Uri(imageURI);
                imageResults.Source = myBI;
                           
                saveImage(myBI, "imgBingMaps1"); 
            }
    
    I Add a button to Window1.xaml.cs named "SAVE to file JPEG"
            private void saveJPG_Click(object sender, RoutedEventArgs e)
            {
                saveImage(myBI, "imgBingMaps2");
            }
    
    My saveImage subroutine :
            private void saveImage(BitmapImage bi, string nameFile)
            {
                using (FileStream stream = new FileStream(@"C:\Users\Olivier\Documents\" + nameFile +".jpeg", FileMode.Create))
                {
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bi));
                    encoder.Save(stream);
                    stream.Close();
                }
            }
    
    To test the programs,
    I enter “47.608, -122.337” (for example) into the input textbox.
    Press the “request Image” button.
    --> the imgBingMaps1.jpeg is 1x1 pixel!
    When the image is on the screen, I click "SAVE to file JPEG"
    --> the imgBingMaps1.jpeg is correct

    So my new question : When to know If the image is available?

    Thank for your help...




    Olivier BERNARD

    Wednesday, August 29, 2012 3:49 PM
  • Hi,

    I think you can refer to the IsDownloading property for that
    More info here:
    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.isdownloading.aspx

    • Marked as answer by Bernardo769 Thursday, August 30, 2012 6:38 PM
    Wednesday, August 29, 2012 6:37 PM
  • Hi,

    Good idea.


    Olivier BERNARD

    Thursday, August 30, 2012 6:38 PM