Not sure why I am getting this error on the last line of code MyOnlineImg.Save. I am using Visual Studio 2017 on Windows 10 Pro. I am pretty sure 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
CodePudding user response:
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
.
CodePudding user response:
I found that Image was corrupted. Thanks!