Convert and save photo to BD

I'm working on a project where I have to save some photos. So I am studying the best way to accomplish the task.

I thought about saving to the database, as these photos will be accessed both locally (WinForms application) and via the internet (MVC application Asp.Net).

So I found a solution that consists of turning the photo into a Array of Byte, as the code below:

MemoryStream stream = new MemoryStream();

meuPictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] foto = stream.ToArray();

And then save to the database, where the table has the following characteristics:

CREATE TABLE [dbo].[Crm_Fotos]
    (
    [id] [int] IDENTITY(1,1) NOT NULL,
    [nCrm] [int] NULL,
    [Bfoto] [image] NULL,
    PRIMARY KEY CLUSTERED 
    (

However, I have some doubts:

  1. would this be the best way to save a photo?
  2. Ref. the quality? Do we have quality loss in the photo?
  3. if it is a photo with high resolution (ex: 6000x4000 Pixel's, 24MB), can I perform this process normally?
  4. does any image type work? (Jpeg, PNG, BitMap and others?)
  5. the reverse process, i.e. take the array from Byte and convert to photo, wouldn't it be a slow process?

Thank you.

Author: José Diz, 2017-05-07

2 answers

Thomas, I suggest you declare the column that will store the photo image as varbinary (max). Is that the data type image will be disabled (someday...), according to Microsoft.

If the average image size is 1 MB or more, Microsoft suggests using FILESTREAM to store the images. In this case, the files of each image are not inside the database, but rather in the Windows File System. This improves manager performance, considering memory allocation. You can access the files using Transact-SQL transactions or using Win32 API.

Reading suggestion:

 4
Author: José Diz, 2017-05-21 22:45:44

1-would this be the best way to save a photo?

I prefer, because being in the database, it is accessible in any environment (local or remote / application or web), and eliminates the problem of controlling 2 backups.

2-Ref. the quality? do we lose quality in the photo?

Not, unless you use compression in the middle of the algorithm.

3-if it is a photo with high resolution (ex: 6000x4000 Pixel's, 24MB), can I run this process normally?

Yes. It might slow down by file size, but try to use different threads to load the image so the application doesn't crash while loading. If errors occur during Select, which may be due to out of memory or timeout, SGDB will have settings to work around the situation.

4-does any kind of image work? (Jpge, PNG, BitMap and others?)

I believe that yes, I can not tell you for sure about the type" image " of sql server, but it should be based on VARBINARY. In another field of the table, record the extension of the file, there when the user is saved, it already saves in the correct format. Note: the VARBINARY you can save any type of file: EXE, dll, ZIP, etc.

5-the process in reverse, i.e. taking the Byte array and converting to photo, wouldn't it be a slow process?

Depends on the size of the array, but will only notice slowness if it is absurdly large.

I prefer to always save the images in the bank, and when giving the select, I do not look for the column of the image. When loading the information on the screen, I display a progress "loading image" and open the thread that will give the select in this field. The same I do for files.

In the case of images, follows example of the functions I use:

Byte [] for image

    public static Image ConvertByteToImage(byte[] pic)
    {
        if (pic != null)
        {
            try
            {
                MemoryStream ImageDataStream = new MemoryStream();
                ImageDataStream.Write(pic, 0, pic.Length);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ImageDataStream,true);
                return img;
            }
            catch
            {
                return null;
            }

        }
        else return null;
    }

Image for byte []

public static byte[] ConvertImageToByte(System.Drawing.Image foto, System.Drawing.Imaging.ImageFormat format )
        {
            if (foto != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    foto.Save(stream, format);
                    //stream.Flush();
                    byte[] pic = stream.ToArray();
                    return pic;
                }
            }
            else return null;
        }
 3
Author: Rovann Linhalis, 2017-05-07 23:38:56