Trying to get a JPEG file from within XML document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JesseKnott
    New Member
    • Apr 2010
    • 3

    #1

    Trying to get a JPEG file from within XML document

    Hello, I've tried googling the hell of out this and no luck, I have the code that is getting the string out of the file, but when I try to create an Image from the code it's giving me a "Parameter Incorrect" exception
    Here is the code I use to convert the string and get the image
    Code:
    try 
                    {
                        string szImage = FilterResults("PHOTOGRAPH").Replace("\r\n\t\t\t", "");
                        byte[] bytes = Convert.FromBase64String(szImage);
                        MemoryStream mem = new MemoryStream(bytes);
                        Image bmp2 = Image.FromStream(mem);
                        return bmp2; 
                    } 
                    catch (Exception ex) 
                    {
                        Console.WriteLine(ex.Message);
                        return null; 
                    }
    Here is the top of the stream I am trying to open as an image
    ffd8ffe0
    I am thinking that This is not even a JPEG image, but that is what the rest of the file is telling me...

    Any ideas?
    Thanks!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Have you put a breakpoint in at line 3 and walked through this to see if you have valid data at each step?
    There is no error recovery in your code.
    If the path is bad at line 3,
    then the byte[] is blank at line 4
    so the memory stream has no data at line 5
    thus is null at line 6

    Any of those could be your invalid parameter.

    Comment

    • JesseKnott
      New Member
      • Apr 2010
      • 3

      #3
      I thought that at first, but I've stepped through all of the code, it's all there and the Convert.FromBas e64String()is working just fine, the memory stream is not throwing an exception.
      The string in the XML includes a series of \r\n\t\t\t for formatting, I've removed all of these using the following code .Replace("\r\n\ t\t\t", "")

      the Memory stream is reporting that it has data in it, and the first 8 bits of the block conform to a normal JPEG header, I cannot figure out why it's not working at this point.....

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Well if everything is working fine to the point where you are getting a valid path to the image... Then what you are really saying is that your method for loading an image doesn't work.

        Have you tried something a bit simpler than your memory stream approach? (Which BTW I've never seen anyone do. FileStream is the more common)

        Code:
        Image.FromFile(string FilePath);
        Is a really straightforward approach to at least work out the kinks.

        Code:
        try 
        {
              Bitmap temp = null;
               string szImage = FilterResults("PHOTOGRAPH").Replace("\r\n\t\t\t", "");
               if(!string.IsNullOrEmpty(szImage) 
                   temp = (bitmap)Image.FromFile(szImage);
         } 
        catch (Exception ex) 
         {
                 Console.WriteLine(ex.Message);
        }
        return temp;

        Comment

        • JesseKnott
          New Member
          • Apr 2010
          • 3

          #5
          I've thought of trying to save the stream as a file and reopening it, but the image is actually stored within the XML as a stream of bytes, I've checked and the header (at least the first 8 bytes) look good, but the I will try doing it as a filestream rather than a memorystream.
          Here are the first 32 bytes
          Code:
          ffd8ffe000104a464946000101010060
          As far as I can tell this is a valid JPEG header, but I cannot get the image to decode (for a lack of a better word) into the image file.
          Here is an excerpt from the file to show what I am working with...
          Code:
          <rs:original>
          			<z:row PHOTO_GUID='{71C2C015-A31D-48FF-BAD7-007EA052DD00}'
          				 GUID='{A6F98173-2E5D-4B6F-9FED-B52ACFAAE0FE}' PHOTO_TYPE='1'
          				 IMAGEFILE='{71C2C015-A31D-48FF-BAD7-007EA052DD00}.jpg'
          				 DATECREATED='2008-02-28T13:01:43' LASTMODIFIED='2008-02-28T13:01:43'
          				 MD5HASH='{B6ADF1DC-6984-A33A-3C75-DC5B43126AB7}' Photographs_RecState='0'
          				 RecTimestamp='2008-02-28T13:01:43.857000000'/>
          		</rs:original>
                  <z:row PHOTOGRAPH='ffd8ffe000104a464946000101010060
          			00600000ffdb004300030202030202030303030403030405080505
          			0404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b10
          			16101113141515150c0f171816141812141514ffdb004301030404
          			05040509050509140d0b0d14141414141414141414141414141414
          			141414141414141414141414141414141414141414141414141414
          			14141414141414ffc000110801e0028003012200021101031101ff
          			c4001f000001050101010101010000000000000000010203040506
          			0708090a0bffc400b5100002010303020403050504040000017d01
          			020300041105122131410613516107227114328191a1082342b1c1
          			1552d1f02433627282090a161718191a25262728292a3435363738
          			393a434445464748494a535455565758595a636465666768696a73
          			7475767778797a838485868788898a92939495969798999aa2a3a4
          			a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4
          I am fairly sure that I am not doing this right, I cant imagine how I am doing it wrong, but I am sure that I am.

          Here is one idea I am going to attempt, perhaps someone can shed some light on this for me.
          The XML file is designed to be read directly into a SQL database, Fortunately I am using a SQL database as a part of my app, I am considering importing the data into the SQL as type Image, then read it back out for displaying on the screen. Does that sound like a possible route?
          Also do I need to prepend the "0x" to the string?

          Sorry for the overload of what may be stupid questions, I am not very familiar with working with images.

          Thanks again for the help!

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            It's possible the data is big-endian instead of little-endian or vice versa

            Comment

            Working...