这是我第一次在这里发帖提问,非常希望有人帮助我。
我有一个select语句,它从我的数据库的ProductsTable中的每一行检索数据,并从中创建一个按钮。表中的每一行都有一个图像,我想将该图像指定为按钮图标。 其他一切都很好。例如按钮已成功创建其标签和价格但图像位不起作用。这是我检索和创建按钮的代码。
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语句中包含图像)
答案 0 :(得分:2)
您需要首先获取二进制数据,然后将其“转换”为图像:
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);
}