如何从Silverlight应用程序将图像存储在SQL Server数据库中?

时间:2022-09-05 21:21:42

I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:

我试图从Silverlight项目中将一些图片存储到我的SQL Server数据库中,我需要一些帮助,所以我的问题是:

  1. How to convert an image to binary from a url to store it into my database (store all the image and not only the url)

    如何将图像从url转换为二进制文件以将其存储到我的数据库中(存储所有图像而不仅仅是url)

  2. Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)

    有没有其他解决方案,没有通过二进制类型? (因为它存在于SQL Server中的图像类型)

  3. Finally, when the image is stored, how to read it from Silverlight?

    最后,当存储图像时,如何从Silverlight中读取它?

Thank you in advance .

先谢谢你 。

3 个解决方案

#1


3  

You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.

您需要将System.Drawing.Image转换为字节数组并将字节数组保存到数据库中。

System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;

// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)

To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).

要从字节数组中将数据返回到System.Drawing.Image对象,请使用System.Drawing.Image.FromStream(System.IO.Stream流)。

#2


0  

  • Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/

    下载文件内容并放入内存流。见:http://www.csharp-examples.net/download-files/

  • Add bytes from memory stream into database

    将内存流中的字节添加到数据库中

  • Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

    使用图像类和方法在C#中使用silverlight获取图像FromStream请参阅http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

#3


0  

http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx

It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

它功能齐全,因为用户可以从本地磁盘中选择图像并将其上载到存储在数据库中的服务。用户可以检索数据库中所有图像的列表并将其下载到客户端以供观看,并且他们可以从数据库中删除现有图像。

#1


3  

You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.

您需要将System.Drawing.Image转换为字节数组并将字节数组保存到数据库中。

System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;

// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)

To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).

要从字节数组中将数据返回到System.Drawing.Image对象,请使用System.Drawing.Image.FromStream(System.IO.Stream流)。

#2


0  

  • Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/

    下载文件内容并放入内存流。见:http://www.csharp-examples.net/download-files/

  • Add bytes from memory stream into database

    将内存流中的字节添加到数据库中

  • Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

    使用图像类和方法在C#中使用silverlight获取图像FromStream请参阅http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

#3


0  

http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx

It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

它功能齐全,因为用户可以从本地磁盘中选择图像并将其上载到存储在数据库中的服务。用户可以检索数据库中所有图像的列表并将其下载到客户端以供观看,并且他们可以从数据库中删除现有图像。