0
votes

I have news list, which contain thumbnails:

Example

I'm getting JSON from server, which means pictures are - links

My code for inserting pictures:

foreach (Article article in NewsList.Result.Articles)
{
    NewsListBoxItem NLBI = new NewsListBoxItem();
    NLBI.Title.Text = article.Title;
    NLBI.Date.Text = US.UnixDateToNormal(article.Date.ToString());
    NLBI.id.Text = article.Id.ToString();
    if (article.ImageURL != null)
    {
        BitmapImage image = new BitmapImage(new Uri(article.ImageURL));
        NLBI.Thumbnail.Source = image;
    }
    NewsListBox.Items.Add(NLBI);
}

If i enter application in offline mode - they wont show up, so i need to save them, the most prefered method - as string!

Which means i need to convert them to Base64:

BitmapImage image = new BitmapImage(new Uri(article.ImageURL));
byte[] bytearray = null;
using (MemoryStream ms = new MemoryStream())
{
    WriteableBitmap wbitmp = new WriteableBitmap(image );
    wbitmp .SaveJpeg(ms, wbitmp.PixelWidth, wbitmp.PixelHeight, 0, 100);
    ms.Seek(0, SeekOrigin.Begin);
    bytearray = ms.GetBuffer();
}
string str = Convert.ToBase64String(bytearray);

Code fails with NullReferenceException where is my mistake? Code fails on line:

WriteableBitmap wbitmp = new WriteableBitmap(image);

Error:

An exception of type 'System.NullReferenceException' occurred in System.Windows.ni.dll but was not handled in user code

I even tried to use this image from my project:

BitmapImage image = new BitmapImage(new Uri("Theme/MenuButton.png",UriKind.Relative)); 

But still fails with NullRef - but i know that image exists, if it was nul i wouldn't see it in ImageBox

1
Where does your code fail? - Cédric Bignon
have you used break points to see which object is comming null.. - loop
I even tried to use this image from my project: BitmapImage image = new BitmapImage(new Uri("RTUTheme/MenuButton.png", UriKind.Relative)); But still fails with NullRef - but i know that image exists, if it was nul i wouldn't see it in ImageBox - Cheese
Perhaps it has something to do with the state of the image. If the image is accessed before it is shown it is not rendered so it could trigger that exception somewhere a long the lines internal. See if there is a way to preload the image when creating the BitmapImage object. - Silvermind
This won't solve your problem since the error is occurring before, but I just wanted to point out that if you want to save the picture to your phone then you don't need to convert it to base64. Save the binary stream directly to the isolated storage. - Kevin Gosse

1 Answers

0
votes

The problem is that image needs to be loaded. You can try by saving the image the first timem it is shown:

foreach (Article article in NewsList.Result.Articles)
{
    NewsListBoxItem NLBI = new NewsListBoxItem();
    NLBI.Title.Text = article.Title;
    NLBI.Date.Text = US.UnixDateToNormal(article.Date.ToString());
    NLBI.id.Text = article.Id.ToString();
    if (article.ImageURL != null)
    {
        BitmapImage image = new BitmapImage(new Uri(article.ImageURL));
        image.ImageOpened += (s, e) =>
            {
                byte[] bytearray = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    WriteableBitmap wbitmp = new WriteableBitmap(image );
                    wbitmp .SaveJpeg(ms, wbitmp.PixelWidth, wbitmp.PixelHeight, 0, 100);
                    bytearray = ms.ToArray();
                }
                string str = Convert.ToBase64String(bytearray);
            };
        NLBI.Thumbnail.Source = image;
    }
    NewsListBox.Items.Add(NLBI);
}