descf
descf

Reputation: 1312

How do I load a BitmapImage for processing in Silverlight?

The following code will only show a size for my bitmap (needed for processing) if I uncomment the commented lines. This doesn't seem the right to go about things but it's all I've come up with so far that works. I don't want to display my bitmap as an image in a UI element, I just want to process it.

    BitmapImage bmpi;

    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        bmpi = new BitmapImage(new Uri("multicolor.png", UriKind.Relative));

        //Image img = new Image();
        //img.Source = bmpi;
        //LayoutRoot.Children.Add(img);
        //LayoutRoot.Children.Clear();

        MessageBox.Show(bmpi.PixelWidth.ToString());
    }

Upvotes: 1

Views: 2083

Answers (1)

foson
foson

Reputation: 10227

To load the image upfront, you need to set CreateOptions to None from its default value, DelayCreation.
Then you can get the width in the ImageOpened event.

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        bmpi = new BitmapImage();
        bmpi.CreateOptions = BitmapCreateOptions.None;
        bmpi.ImageOpened += new EventHandler<RoutedEventArgs>(bmpi_ImageOpened);
        bmpi.UriSource = new Uri("multicolor.png", UriKind.RelativeOrAbsolute);
    }

    void bmpi_ImageOpened(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(bmpi.PixelWidth.ToString());
    }

Upvotes: 2

Related Questions