无法将类型为“System.Drawing.Bitmap”的对象强制转换为“System.Byte []”。 [英] Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.
本文介绍了无法将类型为“System.Drawing.Bitmap”的对象强制转换为“System.Byte []”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我正在使用此代码在databox中从datagridview单元格点击事件中显示图像,但它收到错误: -
当控件受数据绑定时,无法以编程方式将行添加到DataGridView的行集合中。
Hello,
I am using this code for image display in picturebox from datagridview cell click event but it's getting error:-
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
private void dgvDisplayTiles_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
//Get image data from gridview column.
txtTileName.Text = dgvDisplayTiles.CurrentRow.Cells[2].Value.ToString();
cmbtilecategory.Text = dgvDisplayTiles.CurrentRow.Cells[1].Value.ToString();
lblTileID.Text = dgvDisplayTiles.CurrentRow.Cells[0].Value.ToString();
byte[] imageData = (byte[])dgvDisplayTiles.CurrentRow.Cells[3].Value;//Error
i = e.RowIndex;
//Initialize image variable
Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}
//set picture for preview
picTextureDisplay1.Image = newImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}}
请帮帮我。
谢谢提前。
Ankit Agarwal
软件工程师
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
推荐答案
看到这个article
C#Image to Byte Array和Byte Array to Image Converter Class [ ^ ]
see this article
C# Image to Byte Array and Byte Array to Image Converter Class[^]
这篇关于无法将类型为“System.Drawing.Bitmap”的对象强制转换为“System.Byte []”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文