简体   繁体   English

ImageSourceConverter抛出NullReferenceException ...为什么?

[英]ImageSourceConverter throws a NullReferenceException … why?

I've been tearing my hair out over this issue for the last hour or so. 在过去一小时左右的时间里,我一直在为这个问题撕掉我的头发。

I have some code that goes like this: 我有一些代码如下:

videoTile.Icon = new ImageSourceConverter().ConvertFrom(coDrivr4.Properties.Resources.Music.GetHbitmap()) as ImageSource;

When I run my code, it says a NullReferenceException occurred. 当我运行我的代码时,它表示发生了NullReferenceException。 Neither 'Music' nor the return of GetHbitmap() are null. “音乐”和GetHbitmap()的返回都不为空。

I'm trying to get the image via the Properties because it's the only way I've figured out how to access the images in my Resources folder. 我试图通过属性获取图像,因为这是我弄清楚如何访问我的Resources文件夹中的图像的唯一方法。 I would just add them to the app.xaml file as a resource, but I'm not using an app.xaml file for a few reasons. 我只是将它们作为资源添加到app.xaml文件中,但由于某些原因我没有使用app.xaml文件。

Am I attempting this wrong? 我试错了吗? All I need to do is get an ImageSource object of an image I have in my Resource directory. 我需要做的就是获取我的资源目录中的图像的ImageSource对象。 I can use them just fine in my XAML, but can't for the life of me do it in any code. 我可以在我的XAML中使用它们,但不能在我的生活中使用任何代码。

PS: I can't just add them as a resource to the XAML file because this is just a class and so there is no XAML file. PS:我不能只将它们作为资源添加到XAML文件中,因为这只是一个类,所以没有XAML文件。

I hit exactly the same issue - I've got all my bitmaps in a nice, statically typed resource file and I just want to set an ImageSource with them. 我遇到了完全相同的问题 - 我在一个漂亮的,静态类型的资源文件中得到了所有的位图,我只想用它们设置一个ImageSource。 So, since the ImageSourceConverter was throwing null reference exceptions, I changed tack and used this piece of code instead: 因此,由于ImageSourceConverter抛出了空引用异常,我更改了大头钉并使用了这段代码:

Bitmap bitmap = entityCol.EntityCollectionImage;
this.Image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // Image is an image source

Hope that helps. 希望有所帮助。

Well you've got plenty of things which could be null in there. 那么你有很多东西可以在那里无效。 I suggest you separate them out: 我建议你将它们分开:

Bitmap bitmap = coDrivr4.Properties.Resources.Music;
object source = new ImageSourceConverter().ConvertFrom(bitmap.GetHbitmap());
ImageSource imageSource = (ImageSource) source;
videoTile.Icon = imageSource;

Note the use of a cast rather than the as operator here. 注意在这里使用强制转换而不是as运算符。 If source isn't an ImageSource , this will throw an InvalidCastException which will be much more descriptive than just ending up as a null reference. 如果source 不是 ImageSource ,则会抛出一个InvalidCastException ,它将比最终作为空引用更具描述性。

EDIT: Okay, so now we know for sure that it's happening in ConvertFrom , I suggest the next step is to find out whether it's a bug in .NET 4.0 beta 1. Are you actually using any .NET 4.0 features? 编辑:好的,所以现在我们肯定知道它正在ConvertFrom发生,我建议下一步是找出它是否是.NET 4.0 beta 1中的一个错误。你真的在使用任何.NET 4.0功能吗? I suggest you try to extract just that bit of code into a separate project (you don't need to display an API, just convert the image. Try to run that code in .NET 3.5. If it fails in the same way, that's eliminated the beta-ness from the list of possible problems. 我建议你尝试将这些代码提取到一个单独的项目中(你不需要显示API,只需转换图像。尝试在.NET 3.5中运行该代码。如果它以同样的方式失败,那就是从可能出现的问题列表中消除了beta-ness。

This is not a bug in .net Framework, this null reference exception occurs because the ImageSourceConverter cannot convert a Bitmap type object, it can to convert: Stream, string, Uri, byte[], so you have to change your code to something like this: 这不是.net Framework中的错误,这个空引用异常的发生是因为ImageSourceConverter无法转换Bitmap类型对象,它可以转换为:Stream,string,Uri,byte [],所以你必须将代码改为类似这个:

var imageSourceConverter = new ImageSourceConverter();
byte[] tempBitmap = BitmapToByte(eventArgs.Frame);
ImageSource image = (ImageSource)imageSourceConverter.ConvertFrom(tempBitmap);

... ...

private byte[] BitmapToByte(Bitmap bitmap)
{
    byte[] byteArray;
    using (MemoryStream stream = new MemoryStream())
    {
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

Before use ConvertFrom save your bitmap to MemoryStream 在使用之前ConvertFrom将您的位图保存到MemoryStream

Bitmap canvas = new Bitmap(secScreen.Bounds.Width, secScreen.Bounds.Height);
Graphics g = Graphics.FromImage(canvas);
g.Clear(System.Drawing.Color.Yellow);

MemoryStream stream = new MemoryStream ();
canvas.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
ImageSource isrg = (ImageSource)new ImageSourceConverter().ConvertFrom(stream);

尝试将coDrivr4.Properties.Resources.Music.GetHbitmap()的返回值放入临时变量中,看看它是否为空 - 这可能是您的null来自的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM