IntroductionIn a recent Windows Forms project I needed the ability to import and export graphics data from bitmaps to and from XML files. Since the bitmap data is binary and XML files only allow ASCII, it was apparent that a binary to ASCII conversion would be needed. A review of other articles and an internet search did not turn up anything much to my liking, so I decided to design and implement the needed functions myself. I hope that perhaps my efforts might be useful to anyone else with a similar problem to solve. The particular application I was working on involved relatively small graphics symbols used in describing oil well cores and geological outcrops. The application stores all data, including symbols, in a database so that projects can be backed up or transferred simply by copying the database. In the application, the graphics symbols are stored as BLOB's or equivalent, however in transferring or sharing symbols between projects it was desirable to be able to save the data to an ASCII file containing all of the symbols needed. In that way, a corporate standard symbol library could be built up and made available with minimal effort. The main problem in converting image data to and from an XML file format is the encoding of the binary data into an ASCII format that is compatible with XML files. Unfortunately I was unable to find a generic and generally accepted specification for saving binary data in XML files. Several approaches were considered including Base64 encoding[^]. In the end I decided to use straight forward hexadecimal encoding, where each 8-bit byte is represented as two 4-bit nybbles encoded in ASCII using the characters 0 .. 9, A .. F. The disadvantage of using straight hexadecimal encoding rather than Base 64 or higher level techniques is that two ASCII characters are required for each byte of binary data, since a hex-digit can encode only 16 values. Base64 can encode 6 bits per ASCII character, so it requires 1.5 characters on average to encode each byte. The encoding of 7 bits is precluded by the use of the special XML characters ='"<> that could occur in the encoded strings. The advantage of hexadecimal encoding is the relative simplicity of the encoding/decoding algorithms. Since the images I needed to encode were relatively small, hexadecimal encoding works fine. The sample project included with this article contains the VS2008 C# project for reading and writing the XML files, as well as a couple of bitmap files (*.bmp) for testing. The main program allows the bitmaps to be imported and saved to the XML file and also imported from the XML file and displayed on the screen. In the sample project, the methods to create and read the XML files are included in the main form. The code to do the conversions in my real project is embedded in the methods that save and restore graphics symbols to the XML files. Description of CodeThe conversion from Bitmap to XML is done in 2 steps. First the Bitmap data is converted to a byte array, then the byte array is converted to an ASCII hexadecimal string. This could be coded in a single step, however I chose to use two steps to permit easily changing the encoding later if needed. The resulting string is then be saved as the value of the bitmap attribute of an XML <symbol name="sym_name" bitmap="hexstring" /> node. In converting the bitmap to a byte array, a format was defined that saves the width and height (in pixels) of the image followed by an array of the pixel colors in order by rows. To accomodate 4 byte integers, the width and height numbers are specified as uints then coded most significant byte (MSB) first into 4 bytes. The pixel colors are extracted from the Bitmap using the GetPixel(i,j).ToArgb() method and the 32 bit color is then encoded in 4 bytes. Read more: Codeproject
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment