2

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 :

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).

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.

Any idea what should I do ?

Many thanks in advance for your help,

Regards,

Joël

Joel
  • 115
  • 1
  • 2
  • 10
  • possible duplicate of [Getting the raw format of an image "Value cannot be null. Parameter name: encoder"](http://stackoverflow.com/questions/13269491/getting-the-raw-format-of-an-image-value-cannot-be-null-parameter-name-encode) – sloth Mar 28 '13 at 13:54
  • possible duplicate of [Create a thumbnail and then convert to byte array](http://stackoverflow.com/questions/894098/create-a-thumbnail-and-then-convert-to-byte-array/894211#894211) – sloth Mar 28 '13 at 13:54
  • Can you provide any details of where the image is coming from (i.e. file, database, etc) and what the format of the file is (png, bmp, jpg, etc)? – Tim Greaves Mar 28 '13 at 13:55
  • @TimGreaves, the images are received from cameras (sort of barcode scanners). Some of them come through the vendor SDK directly as `System.Drawing.Image` and they are in a JPEG format and others are received as bytes through multiple TCP packet directly via a socket opened with the camera. For this second case, the bytes are concatenated to a unique byte array and are also representing an image in a JPEG format. – Joel Apr 02 '13 at 07:30

1 Answers1

1

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.

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:

image.Save(ms, ImageFormat.Png)
Tim Greaves
  • 755
  • 5
  • 6
  • Thanks @Tim Greaves for this answer. The image I got from the vendor SDK should be a jpg, but is in fact a png (if I save it to a file, I can see by the header that it's a png ). And the image.rawFormat corresponded to MemomoryBmp in this specific case. This is the reason it crashed with ArgumentNullException. – Joel Apr 15 '13 at 16:31