locked
MemoryStream error convert base64string to bitmapimage

    Question

  • I am trying to convert a base64string to Bitmapimage. The below code is used in my windows phone project and it works fine, however I am reusing this code in my windows store app project and getting this error. I have no clue in fixing this error.

    Currently using windows 8 and visual studio 2012

    Error Msg:

    > The best overloaded method match for 'Windows.UI.Xaml.Media.Imaging.BitmapSource.SetSource(Windows.Storage.Streams.IRandomAccessStream)' has some invalid arguments 

    > cannot convert from 'System.IO.MemoryStream' to 'Windows.Storage.Streams.IRandomAccessStream'

    DATADB.cs
    
    class DATADB
        {
            public class NewsObject
            {
                BitmapImage thumb = null;
                        
                public BitmapImage Thumb { 
                    get {
                        if (thumb==null)
                        {
                            Regex rgx = new Regex("^[^,]*,");
                            thumb = Utilities.base64image(rgx.Replace(this.default_photo, ""));
                        }
                        return thumb;
                    }
                }
    			
                public string date { get; set; }
                public string id { get; set; }
                public string info { get; set; }
            }
        }
    
    
    Utilities.cs
    
        class Utilities
        {
            public static BitmapImage base64image(string base64string)
            {
                if (base64string == "" || base64string == null) return null;
    
                byte[] fileBytes = Convert.FromBase64String(base64string);
    
                using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
                {
                    ms.Write(fileBytes, 0, fileBytes.Length);
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(ms); //Getting error message here.
                    return bitmapImage;
                }
            }
        }


    Sunday, March 9, 2014 6:12 AM

Answers