C# 如何将base64的数据类型longtext转换为特定列上从数据库到gridview的图像?
我调用一个数据库来获取数据类型为C# 如何将base64的数据类型longtext转换为特定列上从数据库到gridview的图像?,c#,visual-studio,gridview,base64,C#,Visual Studio,Gridview,Base64,我调用一个数据库来获取数据类型为longtext的列中的数据,该列包含我用base64从图像中编码的字符串 string insertQuery = "SELECT product_id, picture FROM product WHERE aksi != 'Deleted' "; etc... dataGridView1.DataSource = dtblProduct; 数据类型为longtext的列为picture 我想用base64转换longtext列图片中的数据,并将其与产
longtext
的列中的数据,该列包含我用base64从图像中编码的字符串
string insertQuery = "SELECT product_id, picture FROM product WHERE aksi != 'Deleted' ";
etc...
dataGridView1.DataSource = dtblProduct;
数据类型为longtext
的列为picture
我想用base64转换longtext
列图片中的数据,并将其与产品id一起显示在gridview上
我已经尝试过了,但错误显示“输入不是有效的Base-64字符串,因为它包含非Base-64字符、两个以上的填充字符或填充字符中的非法字符。”
根据您的描述,您希望在datagridview的列中显示图片
我建议在设计表单时,在datagridview中添加两列,即product_id、picture
图片列的数据类型为DataGridViewImageColumn
您可以尝试以下代码来解决此问题
public void Table()
{
dataGridView1.Rows.Clear();
MySqlConnection conn = new MySqlConnection("…");
conn.Open();
MySqlCommand cmd = new MySqlCommand("select product_id,picture from product WHERE aksi != 'Deleted'", conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string id, base64;
id = reader["product_id"].ToString();
base64 = reader["picture"].ToString();
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(memStream);
dataGridView1.Rows.Add(new object[] { id,img});
}
reader.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
Table();
}
结果:
根据您的描述,您希望在datagridview的列中显示图片
我建议在设计表单时,在datagridview中添加两列,即product_id、picture
图片列的数据类型为DataGridViewImageColumn
您可以尝试以下代码来解决此问题
public void Table()
{
dataGridView1.Rows.Clear();
MySqlConnection conn = new MySqlConnection("…");
conn.Open();
MySqlCommand cmd = new MySqlCommand("select product_id,picture from product WHERE aksi != 'Deleted'", conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string id, base64;
id = reader["product_id"].ToString();
base64 = reader["picture"].ToString();
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(memStream);
dataGridView1.Rows.Add(new object[] { id,img});
}
reader.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
Table();
}
结果:
您是否检查了什么dataGridView1.Columns[1].ToString()代码>实际返回?顺便说一句,您只需要var image=image.FromStream(newmemoryStream(Convert.FromBase64String(showpic)),true)代码>当showpic
包含真实数据时。它返回picture
列上的数据。顺便问一下,如何在gridview上显示已转换的图片以及其他数据?您可以向DataTable添加一列(并使用特殊构造函数从另一列分配其值),或者向DataGridView(不是DataGrid或gridview)添加DataGridViewImageColumn
,并使用其中一个DGV事件(例如,CellFormatting)格式化数据(如果处理不正确,成本可能会很高)。解决眼前的问题,然后,如果您愿意,您可以更新您的问题并提出此问题。字符串问题很简单,另一个问题更有趣。Btw2,它不返回列图片上的数据,而是返回其他内容。您还没有检查showpic
的内容。我完全困惑。我已经搜索过了对于这些问题,它确实返回了列picture上的数据,但由于非base64字符而出错。可能是因为段落的空格,当我通过dataGridView1.Columns[1].ToString()获取数据时;
visual studio中的字符串无法接受段落空格,因此它会转换为其他内容,然后当我使用base64转换它时,它会出错。有解决方案吗?@jimi您是否检查了dataGridView1.Columns[1].ToString();
实际返回的内容?顺便说一句,您只需要var image=image.FromStream(新的MemoryStream(Convert.FromBase64String(showpic)),true);
当showpic
将包含真实数据时,它将返回列picture
上的数据。顺便问一下,如何在gridview上显示已转换的图片以及其他数据?您可以在DataTable或DataTable中添加列(并使用特殊构造函数从另一列分配其值),或向DataGridView(不是DataGrid或GridView)添加DataGridViewImageColumn
,并使用其中一个DGV事件(例如CellFormatting)格式化数据(如果处理不正确,这可能会很昂贵)。解决眼前的问题,然后,如果您愿意,您可以更新您的问题并提出此问题。字符串问题很简单,另一个问题更有趣。Btw2,它不返回列图片上的数据,而是返回其他内容。您还没有检查showpic
的内容。我完全困惑。我已经搜索过了对于这些问题,它确实返回了列picture上的数据,但由于非base64字符而出错。可能是因为段落的空格,当我通过dataGridView1.Columns[1].ToString()获取数据时;
visual studio中的字符串无法接受段落空格,因此它会转换为其他内容,然后当我使用base64转换它时,它会出错。有解决方案吗?@Jimi