byte[] bimage = (byte[])(new ImageConverter().ConvertTo(dataGridView1[i, j].Value, typeof(byte[])));
//all the rows show no error when putting the picture but if I do not put a picture of the ballast, it gives an error.
What I have tried:
private void button5_Click(object sender, EventArgs e)
{
{
Microsoft.Office.Interop.Excel.Application uyg = new Microsoft.Office.Interop.Excel.Application();
uyg.Visible = true;
Microsoft.Office.Interop.Excel.Workbook kitap = uyg.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)kitap.Sheets[1];
Microsoft.Office.Interop.Excel.Range myRange;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[1, i + 1];
myRange.Value2 = dataGridView1.Columns[i].HeaderText;
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[j + 2, i + 1];
if (dataGridView1[i, j].GetType() == typeof(DataGridViewImageCell))
{
if (dataGridView1[i, j].Value != null)
{
byte[] bimage = (byte[])(new ImageConverter().ConvertTo(dataGridView1[i, j].Value, typeof(byte[])));
MemoryStream ms = new MemoryStream(bimage);
Image image = Image.FromStream(ms);
image.Save(@"D:\dosya.jpg");
float Left = (float)(myRange.Left);
float Top = (float)(myRange.Top);
const float ImageSize = 32;
sheet1.Shapes.AddPicture(@"D:\dosya.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
myRange.RowHeight = ImageSize + 2;
File.Delete(@"D:\dosya.jpg");
}
else {
myRange.Value2 = dataGridView1[i, j].Value;}
}
}
}
}
}