1

I am taking some (jpeg) images with a camera and inserting them to the database (as a blob). In order to insert them to the database, I have to pass the image in a byte array.

Here is a little code that does the conversion to the byte array:

public static byte[] JpegToByteArray(System.Drawing.Image imageIn)
    {                        
       MemoryStream ms = new MemoryStream();
       imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //PROBLEM IS HERE!
       return  ms.ToArray();
    }

Although, I am sure that the image I pass is in jpeg format, "imageIn.Save(...)" throws an error as follows:

enter image description here

Here is the definition of saving method:

    public void Save(Stream stream, ImageFormat format);
    //
    // Summary:
    //     Saves this System.Drawing.Image to the specified file in the specified format.
    //
    // Parameters:
    //   filename:
    //     A string that contains the name of the file to which to save this System.Drawing.Image.
    //
    //   format:
    //     The System.Drawing.Imaging.ImageFormat for this System.Drawing.Image.
    //
    // Exceptions:
    //   System.ArgumentNullException:
    //     filename or format is null.
    //
    //   System.Runtime.InteropServices.ExternalException:
    //     The image was saved with the wrong image format.-or- The image was saved
    //     to the same file it was created from.

The image(s) I retrieve and pass to the function are never stored to/read from the file system. After I insert the image to the database, I dispose it. And the method that I retrieve images just returns a list of System.Drawing.Image (with 4 elements). There is nothing special about it.

Do you guys have any idea why this might happen?

Thanks for your time.

EDIT:

I am able to set the images to the picturebox, for example:

pictureBox1.Image = imageIn;
picturebox.Refresh();

But, cannot save the images neither to MemoryStream, nor to the file system.

blablabla
  • 96
  • 10
  • Are you sure that *imageIn* has System.Drawing.Imaging.ImageFormat.Jpeg format ? – Artavazd Balayan May 31 '16 at 10:33
  • try to convert it first to Bitmap then save it to MemoryStream – Jeric Cruz May 31 '16 at 10:37
  • Yes I am sure it is in JPEG format. I chek for that at "if (ImageFormat.Jpeg.Equals(clonedImage.RawFormat))". And, dear JericCruz, I am asked to insert them in jpeg format, therefore I do not have a luxury to change it. – blablabla May 31 '16 at 10:39
  • It's more like hack, but I've got it from the sources of [Image](http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/CommonUI/System/Drawing/Image@cs/1/Image@cs). If format is JPEG, try to use ImageFormat.Png (internal void Save(MemoryStream stream)) – Artavazd Balayan May 31 '16 at 10:50
  • Please try to create a [mcve], preferably with an acutal image file you use, there's a lot of irrelevant code. – CodeCaster May 31 '16 at 10:52
  • Dear, Artavazd Balayan, it did not work for me. And dear CodeCaster, you are right. I will remove unnecessary parts immediately. (I've included them to avoid questions like 'did you checked that', 'are you sure', 'why it is on there', etc. ) – blablabla May 31 '16 at 10:57
  • There's still no [mcve]. The isolated code you show does not _cause_ the issue, it merely makes the problem manifest itself. Create a small program of a few lines that does everything you want to do which reproduces the problem and post that. – CodeCaster May 31 '16 at 15:57
  • blablabla, you can't save any JPEGs or only from your camera? Because I tried your code with this JPEG [from wiki](https://upload.wikimedia.org/wikipedia/commons/e/e9/Felis_silvestris_silvestris_small_gradual_decrease_of_quality.png) – Artavazd Balayan May 31 '16 at 20:23

1 Answers1

-2

Try this-

    public static void PerisitImage(string path, IDbConnection connection)
{
    using (var command = connection.CreateCommand ())
    {
        Image img = Image.FromFile (path);
        MemoryStream tmpStream = new MemoryStream();
        img.Save (tmpStream, ImageFormat.Png); // change to other format
        tmpStream.Seek (0, SeekOrigin.Begin);
        byte[] imgBytes = new byte[MAX_IMG_SIZE];
        tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);

        command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
        IDataParameter par = command.CreateParameter();
        par.ParameterName = "payload";
        par.DbType = DbType.Binary;
        par.Value = imgBytes;
        command.Parameters.Add(par);
        command.ExecuteNonQuery ();
    }
}

Source- How to save image in database using C#

Community
  • 1
  • 1
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • Dear Souvik Ghosh, I am having trouble at "img.Save" part. Although, thank you for interest, and this useful code. I'm sure it will be helpful for someone else. I appreciate that. – blablabla May 31 '16 at 10:45
  • 3
    If you don't understand a question, don't Google its title and copy-paste the first Stack Overflow hit. That's plagiarism. Either try to understand the question, or move on to the next one to answer. – CodeCaster May 31 '16 at 10:46
  • @blablabla sorry for my confusion and thanks for not blasting on me :). I would follow this post for the correct answer.. – Souvik Ghosh May 31 '16 at 10:59