gpt4 book ai didi

c# - 将 varbinary 转换为图像

转载 作者:行者123 更新时间:2023-11-30 20:07:32 27 4
gpt4 key购买 nike

我设法将 Image 转换为 varbinary 并将其存储在我的数据库中。我一直在尝试将 varbinary 转换为图像,但我在这里遇到了一些麻烦。

首先,我从服务中的数据库中获取二进制文件。

    public Binary getAfbeelding(int id)
{

var query = (from p in dc.Afbeeldings
where p.id == id
select p.source).Single();

Binary source = query;
return source;
}

然后我尝试使用在 StackOverflow 上找到的代码将 varbinary 转换为图像:

    public static string convertToImage(Binary source)
{
byte[] b = source.ToArray();
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
return img.Source.ToString();
}

我在调用 new MemoryStream 之前遇到了麻烦:

'OndernemersAward.EditAfbeeldingServiceReference.Binary' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'OndernemersAward.EditAfbeeldingServiceReference.Binary' could be found (are you missing a using directive or an assembly reference?)

出于某些奇怪的原因,我不能使用“普通”二进制文件,它总是会告诉我使用 OndernemersAward.EditAfbeeldingServiceReference.Binary,这是我的 ServiceReference,如您所见。

如何解决?

最佳答案

1) 在您的项目中添加对 System.Data.Linq.dll 的引用

2) 然后试试这个:

public static Image ConvertToImage(System.Data.Linq.Binary iBinary)
{
var arrayBinary = iBinary.ToArray();
Image rImage = null;

using (MemoryStream ms = new MemoryStream(arrayBinary))
{
rImage = Image.FromStream(ms);
}
return rImage;
}

关于c# - 将 varbinary 转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8279553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com