Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在silverlight中将数据库中的图像显示为字节[]_Silverlight - Fatal编程技术网

在silverlight中将数据库中的图像显示为字节[]

在silverlight中将数据库中的图像显示为字节[],silverlight,Silverlight,我从客户端获取图像,将其转换为字节[],并将其发送到服务器。并将字节[]转换为Base64String并插入数据库 我做了相反的动作来显示图像。但是我看不到图像。为什么 //Convert to byte array public static byte[] ImageToByteArray(WriteableBitmap bitmap) { int[] p = bitmap.Pixels; int len = p.Length <<

我从客户端获取图像,将其转换为字节[],并将其发送到服务器。并将字节[]转换为Base64String并插入数据库

我做了相反的动作来显示图像。但是我看不到图像。为什么

//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-在这种情况下,我不知道为什么它不起作用。