对象引用未设置为对象的示例,System.Drawing.Image保存在VB.NET中从MySQL数据库从.SQL文件恢复

zmeyuzjn  于 2023-03-22  发布在  Mysql
关注(0)|答案(2)|浏览(134)

我在代码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)

我试着为带有断点的null添加测试,但它们都没问题:

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
bweufnob

bweufnob1#

尝试以下操作(在文件顶部添加适当的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

f0brbegy

f0brbegy2#

我发现我的图像在数据库中损坏。我认为这是从SQL文件恢复后。我认为图像很难从SQL文件恢复,但我会尝试发布更多关于这一点。谢谢!

相关问题