How to save image in SQL Server database?
Q: How to save image in SQL Server database?
A: It's the most common question asked in technical forums, the answer is so simply is to convert your image to binary and save the equivalent binary data to the database.
I am expecting you know how to Insert\get data to\from SQL Server.
To convert image to binary you need to write this piece of code
1: System.IO.FileStream fs = new System.IO.FileStream(@"ImagePath", System.IO.FileMode.Open);
2: byte[] imageAsBytes = new byte[fs.Length];
3: fs.Read(imageAsBytes, 0, imageAsBytes.Length);
4: fs.Close();
You need a table with column of binary\image datatype to be able to insert the equivalent binary data of image.
And what about retrieving binary data to be converted to image
You just need to initiate new Image from MemoryStream object which takes array of bytes as an argument -Array of bytes comes from SQL Server-
1: Image img = Image.FromStream(new System.IO.MemoryStream(imageAsBytes));