Convert and save photo in BD

6

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

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

So I found a solution that consists in transforming the photo into a Array of Byte , according to 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:

  • Would this be the best way to save a photo?
  • Ref. the quality? Do we have quality loss in the photo?
  • If it's a high-resolution photo (eg 6000x4000 pixel, 24mb), can I run this process normally?
  • Does any type of image work? (Jpeg, PNG, BitMap and others?)
  • The reverse process, that is, taking array from Byte and converting to photo, would not it be a slow process?
  • Thank you.

        
    asked by anonymous 08.05.2017 / 00:38

    2 answers

    4

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

    If the average image size is 1 MB or larger, Microsoft suggests that you use FILESTREAM for storing images. In this case, the files in each image are not inside the database, but in the Windows file system. This improves the performance of the manager, considering memory allocation. You can access the files by using Transact-SQL transactions or by using Win32 API.

    Suggested reading:

    08.05.2017 / 11:23
    3

    1-Is this the best way to save a photo?

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

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

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

    3-If it's a high resolution photo (eg 6000x4000 pixel, 24mb), can I run this process normally?

    Yes. It may be slow by file size, but look for different threads to load the image so the application will not crash while loading. If errors occur during the Select, which may be due to lack of memory or timeout, the SGDB will have settings to circumvent the situation.

    4-Any kind of image works? (Jpge, PNG, BitMap and others?)

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

    5-The process to the contrary, ie get the Byte array and convert to photo, would not it be a slow process?

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

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

    In the case of images, the following is an 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 to 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;
            }
    
        
    08.05.2017 / 01:38