【发布时间】:2016-09-19 11:46:35
【问题描述】:
我的数据库中有一个表格,如下所示:
Id | Description | Icon
Icon 列的类型为 varbinary(max)
我在此表中有一行,其中 Icon 列中的值显示在 pastebin 链接中(因为它是一个长值):
我正在尝试使用here 提到的以下代码在我的程序中将此 varbinary 值转换为图像:
var binary = new System.Data.Linq.Binary(GetBytes(StreamText)).ToArray();
using (MemoryStream stream = new MemoryStream(binary))
{
var image = new System.Drawing.Bitmap(stream);
image.Save(DownloadPath, ImageFormat.Png);
}
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
StreamText 是 pastebin 链接中的字符串
但在var image... 行,我不断收到异常。
参数无效
我做错了什么?
【问题讨论】:
-
为什么你的 varbinary 列作为字符串从数据库中获取,而不是直接作为字节数组?
-
@Evk 为方便起见,我正在复制并粘贴我的 varbinary 列的
string值,并且只想将其转换为图像。如果我直接得到值,我需要更多的 UI 让用户选择一个表和行 -
@user1 从什么复制粘贴?
-
@Juharr 登录 Sqlserver,使用
Select * from table查看表并复制并粘贴Icon列的值
标签: c# image memorystream varbinary