C# 从数据库以编程方式设置按钮图像
这是我第一次在这里发帖,真的很想有人帮我 我有一个select语句,它从数据库中ProductsTable中的每一行检索数据,并从中创建一个按钮。表中的每一行都有一个图像,我想将该图像指定为按钮图标。 其他一切都很好。e、 g.按钮已成功创建,带有标签和价格,但图像位不起作用。这是我检索和创建按钮的代码C# 从数据库以编程方式设置按钮图像,c#,C#,这是我第一次在这里发帖,真的很想有人帮我 我有一个select语句,它从数据库中ProductsTable中的每一行检索数据,并从中创建一个按钮。表中的每一行都有一个图像,我想将该图像指定为按钮图标。 其他一切都很好。e、 g.按钮已成功创建,带有标签和价格,但图像位不起作用。这是我检索和创建按钮的代码 try { string sqlQuery2 = @"SELECT tp.Description AS [Description], tcpf.Frequency AS [Freq
try
{
string sqlQuery2 = @"SELECT tp.Description AS [Description], tcpf.Frequency AS [Frequency],
tcpf.Price AS [Price]
FROM tblCustProdFreq tcpf
INNER JOIN tblProduct tp ON tp.ProductID = tcpf.ProductID
WHERE tcpf.CustomerID = " + custID +
"ORDER BY tp.Description";
using (SqlCommand comm = new SqlCommand(sqlQuery2, DatabaseConnectionClass.conn))
{
using (SqlDataReader reader = comm.ExecuteReader())
{
while (reader.Read())
{
Button newBtn = new Button();
newBtn.FlatStyle = FlatStyle.Popup;
newBtn.Text = reader["Description"].ToString();
newBtn.Tag = reader["Price"].ToString();
newBtn.Size = new System.Drawing.Size(70, 40);
newBtn.Click += new EventHandler(newBtn_Click);
flowLayoutPanel1.Controls.Add(newBtn);
有人能帮我完成代码,以便它为按钮指定一个图像…(我故意不在SELECT语句中包含图像)您需要先将二进制数据取出,然后将其“转换”为图像:
byte[] data = (byte[])reader["YourColumnName"];
using (MemoryStream ms = new MemoryStream(data))
{
//here you get the image and assign it to the button.
Image image = new Bitmap(ms);
}
还有一件事。如何设置图像的大小,对于按钮来说它看起来太大了。可能您应该设置button.BackgroundImage并使用button.BackgroundImageLayout。