将图像转换为Base64String

本文关键字:Base64String 转换 图像 | 更新日期: 2023-09-27 18:21:09

我需要一些帮助来将图像转换为base64string。我在stackoverflow中使用了类似问题的解决方案,但出现了错误。

使用的解决方案:在wp8 中将图像转换为base64

问题出在图像的源上,我曾将其设置为可写位图,但结果是一个null异常。

这是我的图片来源:

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

这一行出现错误:

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

错误:

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

我现有的代码作为参考:

        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;

我真的很想得到帮助,因为这是我的主要项目。提前感谢!

将图像转换为Base64String

代码工作正常,只是需要一个EventHandler。我使用了Button_Click Listener来运行代码,它运行得很好!