Cannot convert image to bytes [] C #

I am very new to the matter of storing images in the database, and even when I thought it was very straightforward, it wasn’t. What I'm trying to do is read the image file from the same computer in any format, display it in the image window, and then convert the image to bytes to save it to the database. So far, I can display the image in the image window, but I cannot convert the image to bytes. Here is my code:

private void DisplayImage() { if (openFileDialog.ShowDialog(this) == DialogResult.OK) { try { Stream file; if ((archivo = openFileDialog.OpenFile()) != null) { using (file) { pictureBox.Image = Image.FromStream(file); } } } catch (Exception ex) { ... } } } 

This is a simple method that simply displays an image in an image window. The real problem is the following method:

  public static byte[] ConvertImageToBytes(Image image) { if (image != null) { MemoryStream ms = new MemoryStream(); using (ms) { image.Save(ms, ImageFormat.Bmp); byte[] bytes = ms.ToArray(); return bytes; } } else { return null; } } 

When he tries to save the image in a memory stream, I get an error message:

 System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. 

Any ideas on what's going on?

+4
source share
5 answers

It might be silly to answer my question, but I just found out that if I want to convert the Image object to bytes, I have to leave the original stream open. I saw this problem on another page, which I can’t remember, and I tested it, leaving the stream open and it’s true. Therefore, the format was not a problem. But I will take the advice of all of you and save the images in a separate directory. Thanks for the help guys!

+1
source

You should use the RawFormat property of the source image as a parameter to the Save method, and not the default for Bitmap. This will avoid errors such as image format. eg:

  image.Save(ms, image.RawFormat); ms.Position = 0; byte [] bytes=ms.ToArray(); 

I would recommend saving the images to the file system and just saving the file path (preferably relative) to the database.

BLOBs (i.e., images, etc.) in a database cannot be indexed, often stored in a secondary, slower access database area and quickly purge the database size (slower backups, etc.).

+3
source

It is impossible to simply read the file and load it into byte [] using the File class:

 byte[] imgData = System.IO.File.ReadAllBytes(@"C:\My Pic\Myfile.jpg"); 

You can select the image path in the "Open dialog" dialog box.

+2
source

This particular exception usually means that you are trying to save the image as the wrong format. In your code, do you specify ImageFormat.Bmp - is it really a bitmap image, or could you have downloaded it from JPEG or PNG? Trying to save as another format from the one you loaded will be with an ExternalException , as indicated in the documentation .

By the way, I do not recommend storing images in a database, and I believe that most people will agree. Databases can handle this task, but they are not optimized for it, and you will ultimately hurt the performance of both your database and your application. If you are not using SQL Server 2008 FILESTREAM columns, storing images in the file system is more efficient.

+1
source

The problem with this is that the stream must be open for the entire lifetime of the image, otherwise it will fail.

One solution that worked for me is to simply create a copy of the image as follows:

 using (var ms = new MemoryStream(bytes)) { _image = new Bitmap(Image.FromStream(ms)); } 
0
source

Source: https://habr.com/ru/post/1299712/


All Articles