Convert Image to Base64String

99封情书 提交于 2019-12-20 03:52:20

问题


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!


回答1:


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!



来源:https://stackoverflow.com/questions/17461791/convert-image-to-base64string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!