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?