您的位置:首页 > 编程语言 > C#

C# WinForm 实现增删改查等功能(Access版) 系列之七-图片处理

2011-01-30 16:47 495 查看
图片处理模块设计与实现

/// <summary>
/// 获得上传图片的图片名称
/// </summary>
/// <param name="txtName"></param>
/// <returns></returns>
public string getStrOfPictureBox(string txtName)
{
string strpictrue = "";
string name = txtName;
OleDbConnection conn = GetConnection();
string sqlText = "select 图片 from MResume where 姓名=@name order by id asc";
OleDbCommand cmd = new OleDbCommand(sqlText, conn);
cmd.Parameters.AddWithValue("@name", name);
try
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
if (!reader[0].ToString().Equals("0") && reader[0].ToString()!="" && reader[0].ToString()!=null)
{
strpictrue = reader[0].ToString();
}
else
{
strpictrue = Application.StartupPath + "//Image//nophoto.jpg";
}
}
/*DataTable dt = new DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter(sqlText, conn);
oda.Fill(dt);
dataGridView1.DataSource = dt;
*/
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
return strpictrue;
}

打开对话框,选择图片,上传图片

/// <summary>
/// UploadImages
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//本方法来源于Spirit's Home http://www.7788sky.cn/,原文地址:http://www.7788sky.cn/post/csharp_winform_upload_image.html
// openFileDialog1.Filter = "All files (*.*)|*.*|jpg files (*.jpg)|*.jpg";
openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
string image = openFileDialog1.FileName;
string picpath = openFileDialog1.FileName;
DateTime dt1 = new DateTime(1970, 1, 1);
TimeSpan ts = DateTime.Now - dt1;
timeStamp = (long)ts.TotalMilliseconds;
//string id = DateTime.Now.Millisecond.ToString();
picPerson.Image = Image.FromFile(image);
File.Copy(openFileDialog1.FileName, Application.StartupPath + "//Image//" + timeStamp.ToString() + ".jpg");
lblImage.Text = timeStamp.ToString(); //设置图片字符串
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: