-2

I am getting this error on the last line of code MyOnlineImg.Save. I am using Visual Studio 2017 on Windows 10 Pro. The code used to work. When I debug it everything seems normal to me.

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)

I also tried this but I still had the same error.

ms = {System.IO.MemoryStream} 
pngCodec = {System.Drawing.Imaging.ImageCodecInfo}
myEncoderParameters = {System.Drawing.Imaging.EncoderParameters}

Code:

    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)
    

I tried adding tests for nulls with breakpoints but they were all ok:

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
Jeff
  • 155
  • 1
  • 10
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – David Jun 14 '22 at 20:30
  • Writing as `Dim MyOnlineImg = GetImagefromDB(ImgID, Now) If MyOnlineImg isnot nothing then .. End if` or `Dim myImageCodecInfo = GetEncoderInfo(ImageFormat.Png)` could help the debugging process. You clearly have a null object, just inspect those objects at run-time to find out which one. -- You're the only one that can evaluate the return value of two of he methods you have there. – Jimi Jun 14 '22 at 20:46
  • You say, "When I debug it everything seems normal to me," but that's clearly not the case since you're getting an exception. When you get the exception, look at which variable is Nothing, and go backward from there. That should lead you to an assignment statement that isn't having the desired affect. Maybe then it will be obvious what's wrong, or alternatively, you can update the question with much greater specificity about what isn't working. – Craig Jun 14 '22 at 22:04
  • Your memory stream has no name. – user18521918 Jun 15 '22 at 02:48
  • I added null checks with breakpoints but none of them were nothing. – Jeff Jun 15 '22 at 17:06

2 Answers2

0

Try this (after adding appropriate Imports directives at the top of the file):

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)

If it doesn't work, check that GetEncoderInfo() and GetImagefromDB() do what you expect, and that neither return Nothing.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

I found that my Image was corrupted in the database. I think it was after a restore from a SQL file. I think images are hard to restore from a SQL file but I will try and post more on that. Thanks!

Edit: so far, I have found some of the images may not be importing due to size limits.

Also if you get a similar error it may be due to a null or empty object.

Jeff
  • 155
  • 1
  • 10