本文介绍了转换图像到Ba​​se64String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换图像一些帮助base64string。我用了相似的问题解决方案,计算器,但发生错误。

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

解决方案应用于:

问题在于对图片的来源我曾经设置为WriteableBitmap的,但结果是空例外。

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

这是我的形象来源:

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

错误在此行中发生:

Error occured in this line :

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

我现有的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!

推荐答案

在codeS工作正常,只是它需要一个事件处理程序即可。我用了一个 Button_Click监听器运行codeS它完美地工作!

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!

这篇关于转换图像到Ba​​se64String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:29