これは、次の質問のフォローアップです: SQL Server BLOB 文字列を System.Drawing.Image に変換する方法は?
バックグラウンド
c# を使用して、csv ファイルから写真と共にユーザーに関する情報をインポートします。私が使用する独自の SDK では、写真が System.Drawing.Image である必要があります。
以下は、csv ファイルの例です。
surname firstname photo
Blogs Joe 0xFFD8FFE000104A46494600010101005F005F0000FFDB0043000D090A
以前の質問 ( How to convert an SQL Server BLOB string to System.Drawing.Image? ) への回答にあるコードを使用して、16 進文字列で表される BLOB をバイト配列に変換し、続いて System. .Drawing.Image.
これは、BLOB の 16 進文字列からバイト配列に変換するために使用する (前の質問からの) コードです。この原則は機能します。
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);
ただし、一部の高解像度画像ではしきい値が設定されているため、写真の下半分は黒く、予測可能な白い点のパターンがいくつかありますが、上半分は意図したとおりです。
正しくインポートされる写真は
>=640x480
そうでないものは
<640x480
サイズで。すべてビット深度 24 です。これは私が観察したことですが、関連性があるかどうかはわかりません。元の画像は24ビットで、バイト配列への変換により下位ビットの「画像」が作成されるように思われるため、写真の半分は黒で、予測可能な白い点のパターンがあります。
質問
意図しないしきい値処理を回避するために、16 進文字列からバイト配列への変換を変更するにはどうすればよいですか?