保存并使用实体框架6从SQL Server检索图像(二进制) [英] Save and retrieve image (binary) from SQL Server using Entity Framework 6

查看:198
本文介绍了保存并使用实体框架6从SQL Server检索图像(二进制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存一个位图图像数据库

 位图贴图=新位图(pictureBoxMetroMap.Size.Width,pictureBoxMetroMap.Size.Height);

我创建与数据类型的数据库中的列 imgcontent 二进制,但我的问题是我如何转换这种位图(图)二进制数据?

和如何从数据库中检索数据?

我GOOGLE了它,我发现这样的事情,但它没有工作:

 字节[] ARR;
ImageConverter器=新ImageConverter();
ARR =(字节[])converter.ConvertTo(图中的typeof(字节[]));


解决方案

将图像转换为一个字节[] 并存储在数据库中。


这列添加到您的模型:

 字节公众[]内容{搞定;组; }

那么你的图像转换为字节数组,商店,就像您任何其他数据:

 字节公众[] imageToByteArray(System.Drawing.Image对象imageIn)
{
    MemoryStream的毫秒=新的MemoryStream();
    imageIn.Save(MS,System.Drawing.Imaging.ImageFormat.Gif);
    返回ms.ToArray();
}公众形象byteArrayToImage(字节[] byteArrayIn)
{
     MemoryStream的毫秒=新的MemoryStream(byteArrayIn);
     图像returnImage = Image.FromStream(毫秒);
     返回returnImage;
}

来源:<一个href=\"http://stackoverflow.com/questions/17352061/fastest-way-to-convert-image-to-byte-array\">Fastest办法图像转换为字节数组

  VAR形象=新ImageEntity(){
   内容= imageToByteArray(图)
}
_Context.Images.Add(图片);
_Context.SaveChanges();

当你想获得的图像回来,从数据库中获取字节数组,并使用 byteArrayToImage 做你希望与图像什么

按我的意见,因为它停止工作时的byte []获取到大我没有使用EF我的解决方案。它应为文件工作,但是在100兆

(使用解决方案时,大文件:<一href=\"http://www.syntaxwarriors.com/2013/stream-varbinary-data-to-and-from-mssql-using-csharp/\">http://www.syntaxwarriors.com/2013/stream-varbinary-data-to-and-from-mssql-using-csharp/)

I am trying to save an bitmap image to database

Bitmap map = new Bitmap(pictureBoxMetroMap.Size.Width, pictureBoxMetroMap.Size.Height);

I created a column imgcontent in the database with datatype binary but my problem is how can I convert this bitmap (map) to binary data?

And how can I retrieve data from database?

I googled it and I found something like this but it didn't work:

byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(map, typeof(byte[]));

解决方案

Convert the image to a byte[] and store that in the database.


Add this column to your model:

public byte[] Content { get; set; }

Then convert your image to a byte array and store that like you would any other data:

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

Source: Fastest way to convert Image to Byte array

var image = new ImageEntity(){
   Content  = imageToByteArray(image)
}
_Context.Images.Add(image);
_Context.SaveChanges();

When you want to get the image back, get the byte array from the database and use the byteArrayToImage and do what you wish with the Image

As per my comment I did not use EF for my solution as it stops working when the byte[] gets to big. It should however work for files under 100Mb

(Solution when using big files: http://www.syntaxwarriors.com/2013/stream-varbinary-data-to-and-from-mssql-using-csharp/)

这篇关于保存并使用实体框架6从SQL Server检索图像(二进制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆