我在代码MyOnlineImg.Save的最后一行上得到这个错误。我在Windows 10 Pro上使用Visual Studio 2017。代码过去是有效的。当我调试它时,我觉得一切都很正常。
Dim MyOnlineImg As System.Drawing.Image
MyOnlineImg = GetImagefromDB(ImgID, Now)
Dim ms As New MemoryStream()
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As System.Drawing.Imaging.Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
myImageCodecInfo = GetEncoderInfo(ImageFormat.Png)
myEncoderParameters = New EncoderParameters(1)
myEncoder = System.Drawing.Imaging.Encoder.Quality
myEncoderParameter = New EncoderParameter(myEncoder, CType(50L, Int32))
myEncoderParameters.Param(0) = myEncoderParameter
MyOnlineImg.Save(ms, myImageCodecInfo, myEncoderParameters)
我也试过这个,但我仍然有相同的错误。
ms = {System.IO.MemoryStream}
pngCodec = {System.Drawing.Imaging.ImageCodecInfo}
myEncoderParameters = {System.Drawing.Imaging.EncoderParameters}
代码:
Dim ms As New MemoryStream()
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As System.Drawing.Imaging.Encoder
'Dim myEncoderParameter As EncoderParameter
'Dim myEncoderParameters As EncoderParameters
myImageCodecInfo = GetEncoderInfo(ImageFormat.Png) 'lets think about switching from PNG to JPG to speed up performance (reduce quality)
'myEncoderParameters = New EncoderParameters(1)
myEncoder = System.Drawing.Imaging.Encoder.Quality
'myEncoderParameter = New EncoderParameter(myEncoder, CType(50L, Int32))
'myEncoderParameters.Param(0) = myEncoderParameter
Dim pngCodec As ImageCodecInfo = GetEncoderInfo(ImageFormat.Png)
Dim myEncoderParameters As New EncoderParameters()
myEncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 50)
Dim MyOnlineImg As System.Drawing.Image = GetImagefromDB(ImgID, Now)
'Dim ms As New MemoryStream()
MyOnlineImg.Save(ms, pngCodec, myEncoderParameters)
我尝试添加带有断点的空值测试,但它们都可以:
If ms Is Nothing Then
Dim blah As String = ""
End If
If pngCodec Is Nothing Then
Dim blah As String = ""
End If
If myEncoderParameters Is Nothing Then
Dim blah As String = ""
End If
试试这个(在文件的顶部添加适当的Imports
指令后):
Dim pngCodec As ImageCodecInfo = GetEncoderInfo(ImageFormat.Png)
Dim myEncoderParameters As New EncoderParameters()
myEncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 50)
Dim MyOnlineImg As Image = GetImagefromDB(ImgID, Now)
Dim ms As New MemoryStream()
MyOnlineImg.Save(ms, pngCodec, myEncoderParameters)
如果它不工作,检查GetEncoderInfo()
和GetImagefromDB()
做你所期望的,并且都不返回Nothing
。
我发现我的图像在数据库中损坏了。我想是在从SQL文件恢复之后。我认为图像很难从SQL文件中恢复,但我会尝试发布更多。谢谢!
编辑:到目前为止,我发现由于尺寸限制,一些图像可能无法导入。
如果你得到一个类似的错误,它可能是由于null或空对象。