In an ASP.NET app (using framework 2.0), and the ORM for this application is Subsonic 2.2, which I'm working with for the first time.

I have an image which I've retrieved from a URL, and I have an HttpWebresponse object, which I've converted to an image, like this:

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

In Sql Server, I've created an Image column (could be any other binary format, that's not a problem), and I'd like to write my image into it. I'd also like to know how to read from the column, so as to test that the image has been correctly saved.

Any help appreciated. Thanks.

有帮助吗?

解决方案

For a image column, Subsonic creates a byte[] property.

Therefore, you'll have to transform your data (System.Drawing.Image in this case) to and from a byte array.

Other sites have this covered, shamelessly copied:

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;
}

其他提示

Have looked at Read and write SQL image data, this might help you

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top