Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,

My question is i have lots of images in my application , so storing into image folder and retriving from there during run time is more faster or keeping the image in binary data in sql and fetching image from database and then display is much faster .

well in that case , how to make all images to binary so that i can store all data to database for the image .

Say for example i have productdetails table .// => please explain me this

Thanks
Posted

Converting the image into binary data can be done like this using c#
C#
foreach (string fileName in Directory.GetFiles(@"C:\SomePath", ".jpg")) // For example
{
    using (FileStream fs = new FileStream("<imagepath>", FileMode.Open, FileAccess.Read))
    {
        byte[] binaryImage = new byte[fs.Length];
        fs.Read(binaryImage, 0, (int)fs.Length);
        // Store the BLOB n your database
    }
}


However, your database will grow a lot if your images are large, like from a system camera, and will make backups slower, so storing only the path to the image and keep them on a hard drive might be more attractive.
This is a bit of religious war between the two sides, so this research paper might be of interest for you to make your own decision.
To BLOB or Not To BLOB[^]
 
Share this answer
 
v2
Comments
george4986 25-Sep-14 12:45pm    
thanks 4 sharing ;-) my +5v
George Jonsson 25-Sep-14 21:21pm    
Thanks. I guess there is not an absolute "right" answer to this question.
Torakami 26-Sep-14 0:55am    
no man , this is what i was searching for .. when to store in db and when not ..
George Jonsson 27-Sep-14 1:52am    
Good you found it useful
Store your photos in 'Image' format coverting them to byte array that is available in SQL and is much faster. I am sure you must have searched some. Make a loop of all images that may be done for all images.

Refer this link : Storing and Retrieving Images from SQL Server using Microsoft .NET
 
Share this answer
 
Comments
Torakami 26-Sep-14 0:57am    
What is the size limit then ?? i mean is there any range for fast retrival ??
KaushalJB 26-Sep-14 9:58am    
You can get the size by executing this query

SELECT DATALENGTH(imagecol) FROM table

This would help you to find out the size... And if you want to reduce the size of image then apply compression technique of image. There is no size limit to store in sql. Storing images in sql is far better option then any other.
Member 9654496 11-Nov-17 7:50am    
how to covert image in binary foramat?
in sql imagedata must be in varbinary format

byte[] FileData = File.ReadAllBytes(image path);
save this into database

insert into productdetails values("+FileData+")
 
Share this answer
 
here in my project am using this code please modify it to whatever you want. but if give image path then it will return byte format.

 private Byte[] GetImageStream()
        {
            string path;
            System.Drawing.Image dmaImg = null;
            if (hdnPhoto.Value != "")
                path = Server.MapPath("../UploadedFiles/" + GetDoctorID() + "/cropped.jpg");
            else if (rdbtnFemale.Checked) // if we didnt select any image then default female image will be saved in database
                path = Server.MapPath("../UploadedFiles/" + GetDoctorID() + "/eMeditime_ladyimage.png");
            else
//if we didnt select any image then default male image will be saved in database
                path = Server.MapPath("../UploadedFiles/" + GetDoctorID() + "/eMeditime_maleimage.png");

            dmaImg = System.Drawing.Image.FromFile(path); // path is the location of the image

            var stream = new System.IO.MemoryStream();
            dmaImg.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            dmaImg.Dispose();
            return stream.ToArray();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900