The best way to store picture with SQLite and nHibernate - c#

The application is written in C#. It uses nHibernate and SQLite and runs on Windows. I should store pictures and bind them to a user.
So far, I don't really know what is the best way to store the pictures. If I decide to store the path of the picture and save the image in a directory, I'll have problems as soon as the directory or the file is renamed or moved. In the other hand, storing image as BLOB in SQLite is slow as I have to "cast" the image into a bytes array before storing and when I retrieve, I have to set the bytes array into an image.
What is the recommended way for this?

I wrote an image user type for this purpose. With it, you just pass the Image object, not its bytes: http://weblogs.asp.net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx.

Related

Best Way to insert images with MySQL?

I will create MySQL database for online store. This store has around 3000 products. Each product has 5 photos.
I created tables for products table (product_id, product_name) and images table (image_id, Image_details, Product_id)
What is best way to insert photo?
*i am using C# (desktop app) as GUI
Although, with BLOB objects you can achieve this, it not the preferred way. You have to store the location of the images (similarly for other large files), when the query result turned, use the location to access the images.
Keep in mind, your images will be increased over time, so, in practice, we apply a directory hierarchy, usually two will be enough. One can use the first two letter of the name of the image, however, this will be unbalanced directory structure. The better idea is using hash based randomization to distribute the files evenly over the sub-directories. An example (in Java) can be found here.
Dont save your images in the database, it's not the best way. save the paths to the images in your table related to the products.
Use cloud Blob storages or FileSystem instead of saving in your relational database. It is not recommended storing a lot of images >1mb in your database due performance, backup and maintenability.
However if you have a small app and are really sure about going this way, you could convert your image to a byte array and save in a MySQL BLOB field. Check the link below for a sample:
C# saving images to MySql database as blob
Check:
Should I store my images in the database or folders?
How can I save an Image to the File System?
https://learn.microsoft.com/pt-br/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows
https://www.c-sharpcorner.com/article/azure-storage-crud-operations-in-mvc-using-c-sharp-part-two/

Where is the captured image stored?

I'm using the Xzing lib to capture barcode images, and I'm wondering where do those images get stored if there is no pre-defined or optional destination selection option?
Is the captured image just temporarily stored in the applications isolated storage untill the application terminates/ is in a closed state or what?
Based on a quick look at the code, it appears that image are only held in memory and it is up to you to save to IsolatedStorage if you wish to persist them that way.

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.
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.
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.
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.
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;
}
Take this... ASP.NET Image uploading with Resizing You can find variety of examples.
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.

Adding an image file to a database with C# and ASP.NET

How can I add an image file (for example, JPEG, PNG, GIF ...) to a database with C# and ASP.NET?
I'm just working on the project. And now I need your help.
The best way is to simply store the path of the image in the database. Answered fully here and here
If you want the actual image in the DB, then you will have to perform what is referred to as "Blobbing". You will need to convert the image into a Byte Array(Byte[]) and then store the array in the database. The field in the database is going to need to be appropriate for storing a Blob.
Then when you load the image from the DB (to display it), you will have to convert it back into an image. It would probably be useful to store the image type along with the image data so that you know what format to convert that image data into.
Try this
http://www.aspsnippets.com/Articles/Display-Images-in-GridView-Control-using-the-path-stored-in-SQL-Server-database.aspx

Can anyone tell what is the best way to store images in SQL

I would like to store images in database by converting them in to binary objects or i will store them in to a temp folder and will save the path in my database. But i am unable to do the programming so can any give me a sample code to save images in to sql database using Asp.net.
Here we go with some links:
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
That should get you started, code wise.
better to store image in a folder and save the path in database, which would be much faster than saving in database.
Note dont use datatype image, which is going to change form database itself
: read this
Store the images path on DB instead of storing as Binary, because it hurts performance. In SQL server, BLOB datatypes are stored in seperate pages called LOB_data page and on data pages pointers will be linked with LOB_data page. You cannot create an index over BLOB data type also. Hence, it adversely affects the performance. Ideal solution would be to store the images in a shared drive and storing the image link in database.

Categories

Resources