PDA

View Full Version : نمایش تصویر لود شده در Picture Box



nimam2008
پنج شنبه 15 اردیبهشت 1390, 16:38 عصر
با سلام

من تقریبا تمام تاپیک های مربوط به لود تصویر رو خوندم ولی نتونستم مشکلم رو حل کنم لطفا را کد مورد نیاز رو برام بنویسید چون با راهنمایی هایی که کرده بودین نتونستم کاری از پیش ببرم

من یه عکس رو توسط کد زیر در SQL ذخیره کردم


private byte[] ConvertImageToByte(string ImageLocation)
{
byte[] picture = File.ReadAllBytes(ImageLocation);
return picture;
}

private void button3_Click(object sender, EventArgs e)
{
string queryInd = "insert into TIndicator(Id,Name,Pic) " +
"values (@Id,@Name,@Pic)";

SqlCommand cmdSave = new SqlCommand(queryInd, Connection.Set_Con());

cmdSave.Parameters.AddWithValue("@Id", tbxId.Text);
cmdSave.Parameters.AddWithValue("@Name", tbxName.Text);
cmdSave.Parameters.AddWithValue("@Pic", ConvertImageToByte(openLetter.FileName));

cmdSave.Connection.Open();
cmdSave.ExecuteNonQuery();
cmdSave.Connection.Close();
}




و توسط کد زیر در دیتا گرید فراخوانی می کنم




SqlDataAdapter adapterS = new SqlDataAdapter();
adapterS.SelectCommand = new SqlCommand();
adapterS.SelectCommand.Connection = Connection.Set_Con();
DataSet dsS = new DataSet();

string queryS = "select Id,Name,Pic from TIndicator";

adapterS.SelectCommand.CommandText = queryS;
adapterS.Fill(dsS, "TIndicator");
DGVInd.DataSource = dsS.Tables["TIndicator"];

DGVInd.Columns[0].HeaderText = "شماره";
DGVInd.Columns[1].HeaderText = "نام";
DGVInd.Columns[2].HeaderText = "تصویر";


که البته تصویر در دیتاگرید نمایش داده می شه ولی من میخوام وقتی روی هر کدوم از سطر های دیتاگرید که کلیک می کنم تصویر مربوط به اون سطر در یه Picture Box نمایش داده بشه و وقتی روی تصویری که در Picture Box نمایش داده می شه کلیک می کنم تصویر با یکی از نرم افزار های ویندوز مانند Win Picture and Fax Viewer نمایش داده بشه

با تشکر

haghft
پنج شنبه 15 اردیبهشت 1390, 17:01 عصر
خب این الان کجاش کار نمیکنه و میخای انجام بدی؟

nimam2008
پنج شنبه 15 اردیبهشت 1390, 17:07 عصر
میخوام وقتی روی هر کدوم از سطر های دیتاگرید که کلیک می کنم تصویر مربوط به اون سطر در یه Picture Box نمایش داده بشه و وقتی روی تصویری که در Picture Box نمایش داده می شه کلیک می کنم تصویر با یکی از نرم افزار های ویندوز مانند Win Picture and Fax Viewer نمایش داده بشه

bazardeh
پنج شنبه 15 اردیبهشت 1390, 17:21 عصر
برای لود کردن در pictureBox تو رویداد کلیک dataGridView بنویس



SqlCommand cmd = new SqlCommand("select * from teacher where codperson like '" + textBox1.Text + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cbd = new SqlCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
//////...................................لود کردن عکس در pictureBox1
byte[] arrPicture = ((byte[])(dt.Rows[0]["pic"])); //using System.IO;
MemoryStream ms = new MemoryStream(arrPicture);
pictureBox1.Image = Image.FromStream(ms);

nimam2008
پنج شنبه 15 اردیبهشت 1390, 17:39 عصر
مرسی

ولی من متوجه نشدم که تو خط اول کدتون


SqlCommand cmd = new SqlCommand("select * from TIndicator where Id like '" + textBox1.Text + "%'", con);


textBox1.Text و con چی هستند

exlord
پنج شنبه 15 اردیبهشت 1390, 18:34 عصر
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
pictureBox1.Image = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[2].Value as Image;
}

برای نمایش عکس هم توی یه پوشه save کنین و با proccess.Start عکس رو اجرا کنین تا با برنامه پیشفرض باز بشه ...

nimam2008
یک شنبه 18 اردیبهشت 1390, 17:00 عصر
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
pictureBox1.Image = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[2].Value as Image;
}

برای نمایش عکس هم توی یه پوشه save کنین و با proccess.Start عکس رو اجرا کنین تا با برنامه پیشفرض باز بشه ...


مرسی ولی همچنان تصویر داخل Picture Box نمایش داده نمیشه