【问题标题】:Save image to Sql Server image to image field using C# and Subsonic使用 C# 和 Subsonic 将图像保存到 Sql Server 图像到图像字段
【发布时间】:2011-10-24 22:48:45
【问题描述】:

在一个 ASP.NET 应用程序(使用框架 2.0)中,这个应用程序的 ORM 是 Subsonic 2.2,这是我第一次使用它。

我有一个从 URL 检索到的图像,我有一个 HttpWebresponse 对象,我已将其转换为图像,如下所示:

    string uri = ...;
    HttpWebRequest lxRequest = (HttpWebRequest) WebRequest.Create(uri);
    HttpWebResponse lxResponse = (HttpWebResponse) lxRequest.GetResponse();
    System.Drawing.Image image = System.Drawing.Image.FromStream(lxResponse.GetResponseStream());

在 Sql Server 中,我创建了一个 Image 列(可以是任何其他二进制格式,这不是问题),我想将我的图像写入其中。我也想知道如何从列中读取,以测试图像是否已正确保存。

任何帮助表示赞赏。谢谢。

【问题讨论】:

  • Image 在 SQL Server 中是一种已弃用的数据类型,因此您可能希望将数据库中的数据类型更改为 varbinary(max)。

标签: c# asp.net sql-server-2008 subsonic2.2


【解决方案1】:

对于image 列,Subsonic 创建一个byte[] 属性。

因此,您必须将数据(在本例中为 System.Drawing.Image 转换为 字节数组。

Othersites把这个盖上,无耻地抄袭:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

【讨论】:

  • 谢谢。这正是我所需要的。
【解决方案2】:

看过Read and write SQL image data,这可能对你有帮助

【讨论】:

  • 感谢您的链接,但我的数据不是二进制格式,而且我没有使用 ADO.NET 连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 2018-08-14
  • 2016-02-12
  • 1970-01-01
  • 2013-06-26
相关资源
最近更新 更多