Open .BMP from bytearray with mvvm

3 posts, 1 answers
  1. Fredrik
    Fredrik avatar
    58 posts
    Member since:
    Mar 2013

    Posted 22 Jul 2013 Link to this post

    Hello i want to load a .bmp image into ImageEditor.
    i tryed doing it the way described below, but i get "Object reference not set to an instance of an object"  when trying to initialize the radbitmap with the byte array. Other formats like .jpeg and .png works fine with the same code

    <telerik:RadImageEditorUI Image="{Binding Image}" x:Name="ImageEditorUI" >
            private RadBitmap _image = null;
            public RadBitmap Image
            {
                get { return _image; }
                set
                {
                    _image = value;
                    RaisePropertyChanged();
                }
            }
     
     
    private void BuildAttachmentImage(byte[] FileContent)
            {
                try
                {
                    MemoryStream data = new MemoryStream(FileContent);
                    Image = new RadBitmap(data);
                    RaisePropertyChanged("Image");
                }
                catch (Exception ex)
                {
                    ShowMessage(MessageSeverity.FatalError, ex.Message, ex.ToString());
                }
            }
  2. Answer
    Petya
    Admin
    Petya avatar
    975 posts

    Posted 24 Jul 2013 Link to this post

    Hello Fredrik,

    RadBitmap's constructor which receives as a parameter stream can only initialize a RadBitmap in the formats supported by BitmapImage. That said the exception you are observing is normal.

    What I'd suggest is to use the format providers shipped with RadImageEditor to import the image from the stream instead and everything should be working fine.

    I hope this helps! Let us know if you have other comments or questions.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Fredrik
    Fredrik avatar
    58 posts
    Member since:
    Mar 2013

    Posted 30 Jul 2013 Link to this post

    thank you, was just what i needed

    private void BuildAttachmentImage(byte[] FileContent, string extension)
            {
                try
                {
                    MemoryStream data = new MemoryStream(FileContent);
                    IImageFormatProvider formatProvider = ImageFormatProviderManager.GetFormatProviderByExtension(extension);
     
                    Image = formatProvider.Import(data);
                    RaisePropertyChanged("Image");
     
                }
                catch (Exception ex)
                {
                    ShowMessage(MessageSeverity.FatalError, ex.Message, ex.ToString());
                }
            }
Back to Top