问题
我正在尝试为包含图像的每一行创建一个控件。
private void ass_wijzig_Load(object sender, EventArgs e)
{
//query's
string count = "select COUNT(Image) from product";
string query = "select image from product";
//commands
MySqlCommand cmd = new MySqlCommand(count, connection);
cmd.CommandType = CommandType.Text;
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());
MySqlCommand cmd2 = new MySqlCommand(query, connection);
MySqlDataReader reader = cmd2.ExecuteReader();
if (reader.Read())
{
for (int i = 0; i <= totalcount; i++)
{
//What do I need to put here?
}
}
案例
程序会查看产品表中有多少行。每个创建的行都包含一个图像。我希望程序检查有多少行(有效),然后创建一个包含行中图像的图片框。正如您所看到的,我已经确定每一行都被计算在内。我被告知不要使用数组。要从数据库中检索图片,您需要使用数据适配器和数据表。
问题
如何为表格中的每一行创建一个包含正确图像的图片框?
附件
将一张图片放入图片框的代码
private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;
if (count > 0)
{
var data = (Byte[])(ds.Tables["Image"].Rows[count - 1]["Image"]);
var stream = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(stream);
}
非常重要!,高级代码仅用于确保我能够将图像放入图片框中,我已经包含此代码以支持我的声明,即我必须使用数据表和数据适配器。此代码不再包含在我的项目中。
提前致谢
答案 0 :(得分:0)
试试这个
private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;
List<PictureBox> pbList = new List<PictureBox>();
for (int i = 0; i < count-1; i++)
{
var data = (Byte[])(ds.Tables["Image"].Rows[i]["Image"]);
var stream = new MemoryStream(data);
pbList[i] = new PictureBox();
pbList[i].Name = "pic" + i;
pbList[i].Size = new Size(300, 75);
pbList[i].Image = Image.FromStream(stream);
myform.Controls.Add(pbList[i]);
}
}
<强>更新强>
您不需要为PictureBox循环数组
private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlCommand cmd = new MySqlCommand(query, connection);
// MySqlDataReader reader = cmd.ExecuteReader();
var da = new MySqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Image");
int count = ds.Tables["Image"].Rows.Count;
for (int i = 0; i < count-1; i++)
{
var data = (Byte[])(ds.Tables["Image"].Rows[i]["Image"]);
var stream = new MemoryStream(data);
PictureBox pbList = new PictureBox();
pbList.Name = "pic" + i;
pbList.Size = new Size(300, 75);
pbList.Image = Image.FromStream(stream);
myform.Controls.Add(pbList);
}
}
答案 1 :(得分:0)
private void ass_wijzig_Load(object sender, EventArgs e)
{
string query = "Select Image From Product where productid = 1";
MySqlDataAdapter adpt=new MySqlDataAdapter(query,connection);
DataTable dt=new DataTable();
adpt.Fill(dt);
if(dt.Rows.Count>0)
{
PictureBox pcb[]=new PictureBox[dt.rows.count];
for(int i=0;i<dt.Rows.Count;i++)
{
var data = (Byte[])(dt.Rows[i]["Image"].ToString());
var stream = new MemoryStream(data);
pcb[i].Image = Image.FromStream(stream);
}
}
}