1

I have a database on my private host, which contains a column for inserting pictures. I completely converted my picture from PictureBox to Binary code and when I go to the database, I can see the binary codes properly. I also programmed a function for converting binary codes to image, and set an event handler for user to click on a cell in DataGridView and all of the values will be displayed on the specific controls.

First I got an error and that error was something like this :

Cannot convert from object to byte[]

I solved this problem by programming a function and convert object binary to byte[], but now, when I try to convert byte[] (binary array) to image, I face an error and I cannot solve this problem. and problem is :

Parameter is invalid.

Here is my code:

Converting Image to Binary

byte[] ConvertImageToBinary(Image img)
{
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
}

Convert Object to Binary

byte[] ObjectToByteArray(object obj)
{
        if (obj == null)
            return null;

        BinaryFormatter bf = new BinaryFormatter();

        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
}

Convert Binary to Image

Image ConvertBinaryToImage(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream(data))
        {
            return Image.FromStream(ms);
        }
    }

And at last, here is my code in

private void DataCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e):

if (DataCustomer.CurrentRow.Cells[9].Value == null)
    PictureCustomer.Image = null;
else
{
    byte[] ImageByte = ObjectToByteArray(DataCustomer.CurrentRow.Cells[9].Value);
    PictureCustomer.Image = ConvertBinaryToImage(ImageByte);
}

Any helpful suggestion would be appreciate.

Thanks in advance

  • the `BinaryFormatter` does something completely different. it will take the .NET properties of whatever object it is told to serialize, and not the raw image stream. what is really stored in `DataCustomer.CurrentRow.Cells[9].Value`? – Cee McSharpface Nov 24 '18 at 21:45
  • You have a `MemoryStream` in a `using` block. You should `return (Image)Image.FromStream(ms).Clone();`. The `MemoryStream` is being disposed – Jimi Nov 25 '18 at 01:05
  • @dlatikay DataCustomer.CurrentRow.Cells[9].Value this stored Binary data which I converted from image to binary. and I'm trying to retrieve that binary and convert it to image. – Ali Shahbazi Nov 25 '18 at 07:02
  • @Jimi I did what you've said, but nothing has changed. I still have that problem. – Ali Shahbazi Nov 25 '18 at 07:23
  • get rid of that `BinaryFormatter`. make sure the picture is stored as `byte[]`. could you show the code where you assign that value to the gridview's cell? is it databound to a db table? – Cee McSharpface Nov 25 '18 at 08:00
  • @dlatikay Thanks a gazillion. I just deleted that BinaryFormatter and everything looks fine. But can I ask one more question ? Is it possible to make the photo of the picture box movable ? I mean I do not want to change the properties of picturebox to StretchImage and I want to set normal one, but I want user to move the picture inside the picture box. is it possible ? I tried this link and I created DDPanBox it works perfect but when I retrieve the picture from SQL Server, I cannot move the picture inside it. The link :https://www.codebus.net/d-aQ3b.html – Ali Shahbazi Nov 25 '18 at 08:13
  • panning: this has been asked and answered before, https://stackoverflow.com/a/8985909/1132334, https://stackoverflow.com/a/12059058/1132334, https://stackoverflow.com/a/37563883/1132334 – Cee McSharpface Nov 25 '18 at 08:18
  • That comment was an addition to what @dlatikay already said. You need to take care of both problems. You just need a MemoryStream, but treated the way I shown. Also, set `ms.Position = 0` before accessing the Stream. – Jimi Nov 25 '18 at 14:28

0 Answers0