1

I need some help in convert image to base64string. I had used solution from similar question in stackoverflow but an error occured.

Solution used: convert image into base64 in wp8

The problem lies on the Image's Source I used to set as a writeablebitmap but the outcome was a null exception.

This is my image source :

image123.Source = new BitmapImage(new Uri("/correct1.jpg", UriKind.Relative));

Error occured in this line :

WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)image123.Source);

The Error:

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

My existing code as reference :

        image123.Source = new BitmapImage(new Uri("/correct1.jpg", UriKind.Relative));

        byte[] bytearray = null;

        using (MemoryStream ms = new MemoryStream())
        {
            if (image123.Source != null)
            {
                WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)image123.Source);
                wbitmp.SaveJpeg(ms, 46, 38, 0, 100);
                bytearray = ms.ToArray();
            }
        }
        str = Convert.ToBase64String(bytearray);

        binarytxt.Text = str;

I'm really desperate for help as this is my major project. Thanks in advance!

Community
  • 1
  • 1
  • I copy pasted your code and it works fine, are you sure `correct1.jpg` is loaded into the Image correctly – sa_ddam213 Jul 04 '13 at 04:27
  • @sa_ddam213 yes i commented out my codes expect for the image source, the image was loaded on to the control. I'm still getting the same error after several changes to the Uri string. By the way, I'm using windows phone 8 application template, just to clarify. – Marcus Quek Jul 04 '13 at 05:24

1 Answers1

0

The codes are working correctly, just that it needs an EventHandler. I had used a Button_Click Listener to run the codes and it worked perfectly!