Retrieve image path from MYSQL and display one by one, Please help!
I have saved all the image path in mysql, just like this..(MYSQL is in LINUX system)
No name
1 /root/images/baidu.gif
2 /root/images/train.gif
3 /root/images/flower.gif
4 .......
5 ........
I want to retrieve these path and display the images one by one on the winform
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=11.11.11.11;uid=root;" + "pwd=;database=image;";
BindingSource binds = new BindingSource();
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("select * from spad", conn);
DataSet ds = new DataSet();
da.Fill(ds, "spad");
string loadPic = "SELECT iname from spad";
string fullpath = string.Format("\\root\\images\\{0}", loadPic);
this.pictureBox1.Image = Image.FromFile(fullpath);
conn.Close();
What should I do now?
Thanks very much!!!

Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 11:31 PM EST
You dont need to go for threading to that. With a timer you can do that.
Add a trimer to your class.
Then set an interval.
For your case it's 3000. ( 3 seconds. 3000 in milli seconds.)
Then write the code as follows. In tbhe tick event of the timer you can update the picture box.
//Define two global variables.
private int index = 0;
private string[] imagelist;
private void button1_Click(object sender, EventArgs e)
{
string myConnectionString;
myConnectionString = "server=11.11.11.11;uid=root;" + "pwd=;database=image;";
BindingSource binds = new BindingSource();
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString; conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("select * from spad", conn);
DataSet ds = new DataSet();
da.Fill(ds, "spad");
DataTable dt = da.Tables[0];
if ((dt == null) || (dt.Rows.Count == 0))
{
MessageBox.Show("No Data");
return;
}
//Now you have the data table.
//Get the list of image names.
imagelist = new string[dt.Rows.Count];
for(int i =0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string filepath = dr["iname"].ToString();
string filename = Path.GetFileName(filepath);
imagelist[i] = string.Format("\\root\\images\\{0}", filename);
}
conn.Close();
timer1.Start();
//Start the timer.
}
private void timer1_Tick(object sender, EventArgs e)
{
if (imagelist == null)
return;
if (imagelist.Length < index + 1)
{
timer1.Stop();
return;
}
Image myimage = Image.FromFile(imagelist[index]);
pictureBox1.Image = myimage;
index++;
}
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 6:28 PM EST
Change your code as follows.
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=11.11.11.11;uid=root;" + "pwd=;database=image;";
BindingSource binds = new BindingSource();
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString; conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("select * from spad", conn);
DataSet ds = new DataSet();
da.Fill(ds, "spad");
DataTable dt = da.Tables[0];
int i = 1;
PictureBox xff = new PictureBox();
foreach(DataRow dr in dt.Rows)
{
string filepath = dr["iname"].ToString();
string filename = Path.GetFileName(filepath);
Image myimage = Image.FromFile(string.Format("\\root\\images\\{0}", filename));
//Now you can set 'myimage' to the picture box.
}
conn.Close();
Do you have a set of picture boxes? Or you have only one picture box? Anyway you can set the image 'myimage' to the picture box.
pictureBox1.Image = myimage;
Hope this helps. If so please accept my answer.
Thanks
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 6:58 PM EST
I felt, earlier reply is incomplete as it does not contain creating the picture boxes list. So ignore the previous reply and focus on this.
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=11.11.11.11;uid=root;" + "pwd=;database=image;";
BindingSource binds = new BindingSource();
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString; conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter("select * from spad", conn);
DataSet ds = new DataSet();
da.Fill(ds, "spad");
DataTable dt = da.Tables[0];
System.Collections.ArrayList imagelist = new System.Collections.ArrayList();
int i = 1;
foreach(DataRow dr in dt.Rows)
{
string filepath = dr["iname"].ToString();
string filename = Path.GetFileName(filepath);
Image myimage = Image.FromFile(string.Format("\\root\\images\\{0}", filename));
PictureBox temp = new PictureBox();
temp.Name = string.Format("pictureBox{0}",i.ToString());
temp.Image = myimage;
imagelist.Add(temp);
i++;
}
conn.Close();
PictureBox[] pictureboxarray = (PictureBox[])imagelist.ToArray(typeof(PictureBox));
This creates a set of picture boxes named pictureBox1, pictureBox2,.... and sets the images to them. And adds it to an array.
So if you want to access those picture boxes you can do that as you access a normal array.
If you find any issue in my code, pls ask. And pls spend few seconds to accept this answer if this helps you.
zhang9 reputationPosted Nov 3, 2009, 8:42 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help ont
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help on
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help on that
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help on that let
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help on that let me
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 3, 2009, 9:16 PM EST
Then we don't have to create picture boxes and arrays. You need only one picture box.
But you have to create another thread. If you want my help on that let me know.
zhang9 reputationPosted Nov 3, 2009, 9:27 PM EST
zhang9 reputationPosted Nov 4, 2009, 12:02 AM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 4, 2009, 12:40 AM EST
small mistake in typing. should be corrected as ds.Tables[0].
if you find my naswer useful do not forget to accpet it pls.. :)
zhang9 reputationPosted Nov 4, 2009, 3:26 PM EST
Ron10 reputationPosted Feb 10, 2010, 7:22 PM EST
I have been following your post on how to create a pictureboxarray, I am not sure if I have it all correct but it is not displaying in the form , is this correct
string instrPic = comboBox1.SelectedItem.ToString();
SqlConnection roy1 = new SqlConnection(@"server=CAPTSISKO-PC\SQLEXPRESS;Initial Catalog=cable;Integrated Security=True");
roy1.Open();
string newPic = "Select * from guidePic WHERE picTitle = '" + instrPic + "'";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(newPic, roy1);
//_guidePic = BindingContext[da, "Pic"];
da.Fill(ds, "newPic");
ArrayList imagelist = new ArrayList();
int i = 1;
foreach (DataRow dataRow in ds.Tables["newPic"].Rows)
{
Byte[] byteRoy = new Byte[0];
// DataRow myRow;
// myRow = ds.Tables["newPic"].Rows[0];
byteRoy = (byte[])dataRow["Pic"];
MemoryStream stimRoy = new MemoryStream(byteRoy);
// pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
int originalW = Image.FromStream(stimRoy).Width;
int orignalH = Image.FromStream(stimRoy).Height;
int resizedW = (int)(originalW * .370);
int resizedH = (int)(orignalH * .370);
Bitmap bmp = new Bitmap(resizedW, resizedH);
Graphics graphic = Graphics.FromImage((Image)bmp);
graphic.DrawImage(Image.FromStream(stimRoy), 0, 0, resizedW, resizedH);
graphic.Dispose();
bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
bmp.Save("RoyNew");
PictureBox PictureBox2 = new PictureBox();
PictureBox2.Name = string.Format("pictureBox{0}", i.ToString( ));
PictureBox2.Image = ((Image)bmp);
// pictureBox1.Image = ((Image)bmp);
imagelist.Add(PictureBox2);
i++;
}
roy1.Close();
PictureBox[] pictureboxarray =(PictureBox[])imagelist.ToArray(typeof(PictureBox));
I am not sure how I am suppose to display the picturebox array, maybe this is not correct can you look at the code and tell me what I am missing.
Thanks