Karthik Malla
Karthik Malla

Reputation: 5820

image in database

Is it possible to store an Image file (.jpg, .gif, etc) in MYSQL database? Or do it just stores in system and takes reference path of image?

I am using ASP.NET C#, so if you have sample code, it would be great if you could share it.

Upvotes: 5

Views: 2567

Answers (6)

Karthik Malla
Karthik Malla

Reputation: 5820

Take this... ASP.NET Image uploading with Resizing You can find variety of examples.

Upvotes: 1

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

My company currently uses a database with BLOB data for documents and images, and we are trying to mOve it over to only storing references to the files on the server. The databse is many many GBs, when it should be 20 MB. I would recommend against BLOB because it definitely slows down the DB.

Upvotes: 0

MUG4N
MUG4N

Reputation: 19747

First of here a post which is pretty similar to this one: Should I store my images in the database or folders?

Secondly a sample for storing images into database: http://www.dotnetcurry.com/ShowArticle.aspx?ID=129&AspxAutoDetectCookieSupport=1

In my opinion it depends whether you choose to store images in your database or in folders. Both methods have there pros and cons.

Here a short comment from asp.net forums:

Should You Really Store Images in the Database?

About ten years ago, I started one of the first Internet projects—an image data bank. We were supposed to deliver images of various sizes and resolution to registered users. Each image was designed as a collection of images, from the thumbnail to the highest resolution. The largest image available was about 4 MB. Each image stored in the archive took up a total of 6 MB of space.

The back-end database was not running on a Microsoft platform but provided support for BLOB fields. Without much debate, we decided to store descriptions and other catalog information in the database and to store images as individual files under a well-known path. We also stored in the database enough information for the software to retrieve the file. Being designed for a relatively small number of registered users, the application never showed scalability problems and at no time did anyone on the team, or any users, complain about performance.

Can this brief experience—especially an experience from a relatively old age of software and database technologies—be used as an example of the superiority of file-based storage over database storage? Certainly not, but reading between the lines of how modern DBMS systems implement BLOB fields, I've used this experience to formulate an idea about image storage and databases.

In short, should you consider storing images in a database? If you need to edit the images frequently, I suggest storing the images as separate files on the server's hard drive. If the size of the images are very large (for example, hundreds of megabytes), I suggest storing the images as separate files in the file system. If your images are essentially read-only and relatively static, and if you measure the size in kilobytes, you can consider storing your images in the database.

Upvotes: 4

Koen
Koen

Reputation: 2571

You have to create a byte array from the image and store that in the database.

public static byte[] ConvertImageToByteArray(Image imageIn)
{
    var ms = new MemoryStream();
    imageIn.Save(ms, ImageFormat.Png);
    return ms.ToArray();
}

To convert the data back to an image:

public static Image ConvertByteArrayToImage(byte[] byteArrayIn)
{
    var ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

Upvotes: 1

Dave
Dave

Reputation: 4412

4GuysFromRolla.com has an article on this that I used as reference when I was writing code to store binary data directly in a database: https://web.archive.org/web/20210304133428/https://www.4guysfromrolla.com/articles/120606-1.aspx

Personally I find it to be far less trouble to store the image in the filesystem and a pointer in the database.

Upvotes: 1

Donut
Donut

Reputation: 112915

Yes, you can store image files (and any other files) in a database as binary data.

In MySQL, the BLOB data type can be used to accomplish this.

A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. [...]

BLOB values are treated as binary strings (byte strings). They have no character set, and sorting and comparison are based on the numeric values of the bytes in column values.

Upvotes: 6

Related Questions