vb.net 图像保存到内存流导致 ArgumentNullException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15683096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net image save to memory stream results in an ArgumentNullException
提问by Joel
In VB.NET I have the following function that allows me to calculate a hash for an image I have not yet saved to a file :
在 VB.NET 中,我有以下函数可以计算尚未保存到文件的图像的哈希值:
Public Function pictureHash(ByVal image As System.Drawing.Image) As String
Try
If image Is Nothing Then Return Nothing
Dim ha As HashAlgorithm = HashAlgorithm.Create()
Dim ms As New MemoryStream()
image.Save(ms, image.RawFormat)
ms.Position = 0
Dim imageHash As Byte() = ha.ComputeHash(ms)
ms.Close()
Return BitConverter.ToString(imageHash)
Catch ex As Exception
Return Nothing
End Try
End Function
The problem is I get an ArgumentNullException on the instruction image.Save(ms, image.rawFormat).
问题是我在指令上得到一个 ArgumentNullException image.Save(ms, image.rawFormat)。
Here is the detail of the exception :
这是异常的详细信息:
System.ArgumentNullException occurred
Message="Value cannot be null. Parameter name: encoder"
ParamName="encoder"
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at MyProgram.pictureHash(Image image)
The thing is, when I look at the stack trace, the last call to Image.Save with 3 parameters (the one that crashes) isn't even made by me, but by the previous Image.Save call.
问题是,当我查看堆栈跟踪时,最后一次使用 3 个参数(崩溃的那个)对 Image.Save 的调用甚至不是由我完成的,而是由之前的 Image.Save 调用完成的。
Any idea what should I do ?
知道我该怎么办吗?
Many thanks in advance for your help,
非常感谢您的帮助,
Regards,
问候,
Jo?l
乔尔
采纳答案by Tim Greaves
According to the community content for the Image.Save method, certain image formats do not have an associated encoder and fail with the error you are reporting.
根据Image.Save 方法的社区内容,某些图像格式没有关联的编码器并且会因您报告的错误而失败。
Could you not use a standard format (e.g. bmp or png) when saving the file to the MemoryStream? To do this replace the call to Save with something similar to:
将文件保存到 MemoryStream 时,您不能使用标准格式(例如 bmp 或 png)吗?为此,请使用类似于以下内容的内容替换对 Save 的调用:
image.Save(ms, ImageFormat.Png)

