2

This is a follow-up to this question: How to convert an SQL Server BLOB string to System.Drawing.Image?

Background

I am going to import information about users together with their photo from a csv file using c#. The proprietary SDK that I use requires the photo to be a System.Drawing.Image

Below is an example of the csv file:

surname firstname   photo                       
Blogs    Joe        0xFFD8FFE000104A46494600010101005F005F0000FFDB0043000D090A

I use the code found in the answer to my previous question (How to convert an SQL Server BLOB string to System.Drawing.Image?) to convert the BLOB that is represented by a hex string into a byte array and then subsequently into a System.Drawing.Image.

This is the code (from my previous question) that I use to convert from a hex string of a BLOB into a byte array. This principle works.

  strPhoto = csvuser.photo; // The string that represents the BLOB

  //remove first 2 chars (the '0x')
  strPhoto = strPhoto.Remove(0, 2);

  //convert hex-string to bytes:
  int NumberChars = strPhoto.Length/2;
  byte[] bytes = new byte[NumberChars];
  using (StringReader sr = new StringReader(strPhoto)){
        for (int i = 0; i < NumberChars; i++)
           bytes[i] = Convert.ToByte(new string(new char[2]{(char)sr.Read(), (char)sr.Read()}), 16);
  }

  // Then we create a memory stream that holds the image
  MemoryStream photostream = new MemoryStream( bytes );

  // Then we can create a System.Drawing.Image by using the Memory stream
  var photo = Image.FromStream(photostream);

However, some higher resolution images are thresholed so that the bottom half of the photo is black with some predictable patterns of white dots whilst the upper half is as intended.

The photos that import correctly are

>=640x480

and the ones that don't are

<640x480

in size. All with bit depth 24. This is something I have observed but not sure if that is relevant. The original images are 24 bit and it seems to me that the conversion to a byte array creates a lower bit "image" and therefore half of the photo is black with a predictable patterns of white dots.

Question

How do I change the conversion of the hex string to byte array to avoid the unintentional thresholding?

Community
  • 1
  • 1
kkuilla
  • 2,226
  • 3
  • 34
  • 37
  • 1
    Most likely the loss of quality you are seeing is happening when the image is being encoded for storage in to the database, NOT when you convert from Hex > byte > image. How are the images getting in to the database? I'd be willing to bet that this is where the problem actually lies. – Bradley Uffner Aug 22 '13 at 17:50
  • The photos are taken through a capture toolthat belongs to a commercial system. That system consists of two parts. One capture tool and one viewing tool where you can view the details of a person including view the photo. All Photos look as they should with the viewing tool so I am a bit puzzled. – kkuilla Aug 23 '13 at 08:45

0 Answers0