Save and Retrieve Image (Binary) from SQL Server Using Entity Framework 6

Save and retrieve image (binary) from SQL Server using Entity Framework 6

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)
{
using(var ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

return ms.ToArray();
}
}

public Image ByteArrayToImage(byte[] byteArrayIn)
{
using(var ms = new MemoryStream(byteArrayIn))
{
var 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

This stops working when the byte[] gets to big. It will work for files under 100Mb

How to save and retrieve images from database using Entity Framework Core in ASP.NET Core web application?

You need to save the image data in a Base64 string in the database and read it from DB then assign to src in img tag.

You can refer the below sample.

C# Code

string imageData = @"data:image / jpeg; base64," + Convert.ToBase64String(File.ReadAllBytes(imgPath));

HTML

<img src=imageData />

Alternative Approach

You can save the images on the server also and read it from there. for this, you need to either disable CORS or allow the header/origin/method in EF Core project.

How to store images using Entity Framework Code First CTP 5?

You can't use SQL FILESTREAM in EF. EF is supposed to work on top of different database servers but filestream feature is specific feature of SQL 2008 and newer. You can try to do it old way - use varbinary(max) in your database table and use byte array in your mapped class.

Edit:

Little clarification - you can use FILESTREAM in the database but EF will not take advantage of streaming. It will load it as standard varbinary(max).

Store files in database using Entity Framework Core

You can convert the file bytes to a byte array.

public byte[] Avatar { get; set; }

Examine the accepted answer in the analogous approach for EF6:
Save and retrieve image (binary) from SQL Server using Entity Framework 6

Save files in database with entity framework

The "right" way to store a file in a SQL Server 2008 database is to use the FILESTREAM data type. I'm not aware that the Entity Framework supports that, but you can certainly try and see what happens.

That said, most of the time when people do this, they don't store the file in the database. Doing so means that you need to go through ASP.NET and the database server just to serve a file which you could be serving directly from the web server. It can also somewhat complicate the backup picture for your database and site. So when we upload files to our MVC/Entity Framework, we store only a reference to the file location in the database, and store the file itself elsewhere.

Obviously, which strategy is right for you depends a lot on the particulars of your application.



Related Topics



Leave a reply



Submit