Sign in to follow this  
cignox1

[.net] storing a bitmap in a database

Recommended Posts

Well, it's just the logo of the company. It will be displayed from every client connected to the server, so I thought to put it on the database since its hust one image...

Anyway, what is this Blob? I thought to make a byte array column, and store the bitmap as a sequence of bytes... but I'm not sure about which tools .Net has to do this and wich is the best way. What if I read the file, convert (somehow) it in a stream of bytes, store it, and do it the way around when I read the image?

Share this post


Link to post
Share on other sites
Quote:
Original post by cignox1
Well, it's just the logo of the company. It will be displayed from every client connected to the server, so I thought to put it on the database since its hust one image..


Sounds like it's a lot easier if you embed the image into the executable of your application.

Share this post


Link to post
Share on other sites
The image will be the logo of our customers. Each customer has a server and one or more client. The administrator, from the client, can set a logo that each client will show. There are two possibilities, as far as I can tell: the image is uploaded on the server machine, and clients ask the server for the image, or the image is stored in the same table where all the informations about the company are. I suppose this second way is the best one, but I have problems in finding how to do that in C#....

Share this post


Link to post
Share on other sites
blob = binary large object

If the image is really small, storing it in the database is not that much of a problem. But generally you'd indeed not store that kind of information in a DB.

If you google for ".net database image" or so you'll find plenty of information on how to read/write images from/to a database, e.g.:

Uploading Images to a Database
Retrieving Images from a Database

It boils down to using an appropriate column type (image in Sql Server, bytea in postgres IIRC) and using a DataReader to read/write the byte array that contains the image.

Two hints:
System.Graphics.Bitmap to byte array:

byte[] GetImageBytes(Bitmap img, ImageFormat format){
using(var ms = new MemoryStream()){
img.Save(ms, format):
return ms.ToArray();
}
}


byte array to Bitmap :

Bitmap GetImageFromBytes(byte[] data){
using( var ms = new MemoryStream(data)){
return new Bitmap(ms);
}
}

Share this post


Link to post
Share on other sites
Thank you very much, this is exactly what I was doing, glad to see that I was on the right path :-)

rate++, so you beat me :-)

EDIT: I tried to rate you up, but your rating didn't change... maybe I already rated you up once?

Share this post


Link to post
Share on other sites
Ok, for those who are interested, this is how I did it:

I used a MemoryStream to convert bitmaps in byte vectors and the way around. This made me possible to use a bytea column in postgress, and throught the ODBCCommand class and the use of parameters I was able to upload this array on the database. Retrieving it was just a matter of calling GetValue on the IDataReader returned by a query.

Quite easy, once you know wich classes and methods to use...

Thank you all again!

Share this post


Link to post
Share on other sites
Storing images in databases, even very large images, is a perfectly reasonable thing to do. I've worked on projects that have stored several gigabytes of image data in the database (mapping applications that include satellite imagery).

The alternative, storing the physical file on the file system but referencing it from the database as a filename, is a good way to end up with broken file links. It also complicates your security model, as databases and file shares aren't necessarily ran on the same servers as each other or the application host. By storing the image in the database, it removes one "point of failure" when it comes to authenticating access. It's also generally easier to secure a database properly.

Any decent database should be easily store image data, it's just a matter of finding the right data type.

Share this post


Link to post
Share on other sites
Quote:
Original post by cignox1
EDIT: I tried to rate you up, but your rating didn't change... maybe I already rated you up once?


The size of the rating boost (or hit) depends on the two user's relative ratings. So if, say, Superpig rates you postively, your rating would jump by a couple dozen points. If I rated you, then it would change by maybe 5 or 6. Because you and he are pretty close together, it doesn't change much, if at all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this