0

I wrote the entire program that generates an x9.37 ICL file. The only issue I am having is on the image data part of the record 52. When I validate using the x9 Validator tool and bunch of others, the ICL file breaks on the record 52 on field 19. It is unable to recreate the image form the binary string. If I remove the check information and just try to send an empty file, it goes through without any issues. It is able to correctly read all records and fields from 01 > 10 > 20 > 25 > 50 > 52 > 70 > 90 > 99. I just don't know why it doesn't work with the image when it is formated correctly (240dpi, B&W Group IV Tiff, and is less than 200kb file size).

The code snippet I am using to convert the image to binary string is as follows:

public static string ConvertImage(Image image)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, ImageFormat.Tiff);
            byte[] imageBytes = ms.ToArray();
            string encodedString = Encoding.Default.GetString(imageBytes);
        }
        return encodedString;
    }

I am not sure where I am messing up. Greatly appreciate any feedback. Thanks.

gungrayve
  • 21
  • 7
  • If Encoding.Default is ASCII for example, every non-ASCII character gets changed to question marks. Try `new UTF8Encoding(false)` instead of `Encoding.Default`. The constructor's parameter tells the class to not add the byte order mark. – cbr Feb 17 '15 at 16:16
  • Also, may I ask, how are you using the string obtained from `ConvertImage`? – cbr Feb 17 '15 at 16:18
  • Thanks. I tried as you suggested, it breaks at the same point. However, the binary string looks different in this one compare to the Encoding.Default. They both do start with II* which indicates that its little-endian which what x9.37 requires. – gungrayve Feb 17 '15 at 16:56
  • I am taking the string output of ConvertImage and concatenating it with other strings in its proper location. – gungrayve Feb 17 '15 at 16:57
  • Could you please show the part where you use the string and where it breaks? I'm trying to wrap my head around why you'd use a string instead of the byte array. – cbr Feb 17 '15 at 17:00
  • I cleaned out some of the sensitive info, I updated my post above. – gungrayve Feb 17 '15 at 17:08
  • Is the image data really processed from a string? How do you create the file? – cbr Feb 17 '15 at 17:30
  • I am using StreamWriter to open up a new stream and appending all records in sequence. – gungrayve Feb 17 '15 at 17:42
  • Here is an excerpt from x9.37 file: The following information describes the image data that must be included in field 19 of the Image View Data Record (Type 52). All images must be Black and White TIFF images in a TIFF wrapper. No other combination of image and wrapper format is acceptable. These images must be encoded in little-endian byte order. The little-endian byte order is specified by the first two bytes in a TIFF 6.0 image file header as: “II” (4949.H). In the “II” format, byte order is always from the least significant byte to the most significant byte. – gungrayve Feb 17 '15 at 17:44
  • Another place it notes the file specifications as follows: CHARACTER CODE: ASCII or EBCDIC except for BINARY image data. – gungrayve Feb 17 '15 at 17:48
  • Why not append the bytes straight with the streamwriter? – cbr Feb 17 '15 at 18:00
  • Isn't that complicates things considering you have to keep a hierarchical structure. I have to do some serious restructuring of my classes and methods to be able to test. Is there a difference if we write a string of binary code vs actual binary code? Regardless I will let you know how that goes. Thanks. – gungrayve Feb 17 '15 at 18:10
  • Well, I don't know StreamWriter's default settings but it may just write the strings as UTF-16 as that is the encoding in which C# strings are kept in memory as – cbr Feb 17 '15 at 18:16

1 Answers1

0

Thanks GrawCube. You asked me all the right questions leading me to the solution. Instead of writing to a StreamWriter, I wrote it to a BinaryWriter and the ICL file was able to be successfully processed.

Community
  • 1
  • 1
gungrayve
  • 21
  • 7