你调用的对象是空的.用于图像.源到wpf中的byte [] [英] Object reference not set to an instance of an object. for image.Source to byte[] in wpf

查看:52
本文介绍了你调用的对象是空的.用于图像.源到wpf中的byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的将image.source转换为byte []的代码

my code for convert image.source to byte[]

public Byte[] BufferFromImage(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}



致电:



call :

var s = BufferFromImage(image1.Source as BitmapImage);




如何在wpf中将image1.Source转换为byte []?




how to convert image1.Source to byte[] in wpf ?

推荐答案

只需使用调试器进行验证:
Just use the debugger to verify:
image1 != null
image1.Source != null
image1.Source is BitmapImage

或添加

System.Diagnostics.Debug.Assert(image1 != null, "image1 is null");
System.Diagnostics.Debug.Assert(image1.Source != null, 
                                "image1.Source is null");
System.Diagnostics.Debug.Assert(image1.Source is BitmapImage, 
                                "image1.Source is NOT a BitmapImage");

就在调用BufferFromImage之前.
然后,您应该知道问题出在哪里以及如何解决.

right before the call to BufferFromImage.
Then you should know what is wrong and how to fix it.


如果遇到对象引用错误,并且该错误发生在image1.Source上,则可能是image1 == null.
If you are getting an Object Reference error, and the error is occuring on image1.Source, then likely image1 == null.


这篇关于你调用的对象是空的.用于图像.源到wpf中的byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆