在silverlight中将数据库中的图像显示为字节[]
我从客户端获取图像,将其转换为字节[],并将其发送到服务器。并将字节[]转换为Base64String并插入数据库 我做了相反的动作来显示图像。但是我看不到图像。为什么在silverlight中将数据库中的图像显示为字节[],silverlight,Silverlight,我从客户端获取图像,将其转换为字节[],并将其发送到服务器。并将字节[]转换为Base64String并插入数据库 我做了相反的动作来显示图像。但是我看不到图像。为什么 //Convert to byte array public static byte[] ImageToByteArray(WriteableBitmap bitmap) { int[] p = bitmap.Pixels; int len = p.Length <<
//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);
}
//转换为字节数组
公共静态字节[]ImageToByteArray(可写位图)
{
int[]p=bitmap.Pixels;
int len=p.Length我有以下代码将字节数组直接转换为图像:
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;
因此,只要与Base64String
之间的转换正常,您就只需要这样做了
另外,您不需要转换为Base64String
来存储在数据库中。您只需要将列类型设置为image
(假设您使用的是MS SQL Server)我使用了BinaryReader并解决了这个问题
BinaryReader reader = new BinaryReader(fileInfo.OpenRead());
byte[] tempImage = new byte[reader.BaseStream.Length];
reader.Read(tempImage, 0, tempImage.Length);
我的问题是无法正确读取图像。我使用BinaryReader读取图像并解决了问题
BinaryReader reader = new BinaryReader(fileInfo.OpenRead());
byte[] tempImage = new byte[reader.BaseStream.Length];
reader.Read(tempImage, 0, tempImage.Length);
你好,Chris。我使用MS-SQL,但我使用通过反射和查询对象的元数据映射。使用此域模型,我无法将图像数据添加为图像。而且我无法使事情正常进行。@turgut-我认为您需要解决这个问题。是图像到字符串的转换失败了吗?还是字符串在数据库中的存储失败了?验证每一步的数据。从byte[]->string->byte[]进行转换,并检查结果是否与输入相同。令人惊讶的是,转换前和转换后的数据匹配。@turgut-在这种情况下,我不知道为什么它不起作用。