我從客戶端獲取圖像將其轉換爲byte []並將其發送到服務器。並將byte []轉換爲Base64String並插入到數據庫中。從silverlight中以字節[]的形式顯示數據庫中的圖像
而且我倒轉顯示圖像。但我不能看到圖像。爲什麼???
//Convert to byte array
public static byte[] ImageToByteArray(WriteableBitmap bitmap)
{
int[] p = bitmap.Pixels;
int len = p.Length << 2;
byte[] result = new byte[len];
Buffer.BlockCopy(p, 0, result, 0, len);
return result;
}
//Converter
public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
if (value == null)
{
return null;
}
BitmapImage image = new BitmapImage();
MemoryStream stream = new MemoryStream();
stream.Write((byte[])value, 0, ((byte[])value).Length);
image.SetSource(stream);
return image;
}
//While writing to database
else if (value.GetType() == typeof(byte[]))
{
return "'" + Convert.ToBase64String((byte[])value) + "'";
}
else if ((type == typeof(byte[])))
{
return Convert.FromBase64String((string)value);
}
克里斯 - 嗨。我使用MS-SQL,但我通過反射和查詢對象使用元數據映射。在這個領域模型中,我不能將圖像數據添加爲圖像。而我卻無法讓事情奏效。 – turgut
@turgut - 我認爲你需要解決問題。是圖像轉換爲字符串失敗嗎?或者也許是字符串在數據庫中的存儲?驗證每一步的數據。從byte [] - > string - > byte []進行轉換,並檢查結果是否與輸入相同。 – ChrisF
轉換前和轉換後匹配的數據令人驚訝。 – turgut