0

I have a program, which takes data from xlsx file and put them to dataGridView. Then I'm trying to convert this data(from dataGridView) to byte array for later place it to PictureBox, from Bitmap. So I have datagridView(object) -> byte array - > Bitmap -> Picture.

And problem is occur, on the step, where I'm trin convert byte array to Image.

I've tried as many ways to fix it, as I could find in the Internet(especially modifications of MemoryStream and ImageConverter), but noone didn't help. Debuger Says that data is ok(pic below). May be there is another way to convert it to image?

This is my dataGridView
This is my dataGridView

data from my array
data from my array

    private void buttonShowImage_Click(object sender, EventArgs e)
    {

        int size = (dataGridView1.RowCount) * (dataGridView1.ColumnCount-1);
        byte[] arrOfValues = new byte[size];
        PictureBox picBox = new PictureBox();


        int k = 0;
        for(int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count - 1; j++)
            {
                arrOfValues[k++] = Convert.ToByte(dataGridView1[i, j].Value);
            }

        }

        ImageConverter ic = new ImageConverter();

        Image img = (Image)ic.ConvertFrom(arrOfValues);  // here is the problem 'data is not valid' 

        Bitmap bmp = new Bitmap(img);

        picBox.SizeMode = PictureBoxSizeMode.Zoom;

        picBox.Image = bmp;


        picBox.Show();

    }
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431

0 Answers0