1. 程式人生 > >Winform下如何上傳圖片並顯示出來。同時保存到數據庫

Winform下如何上傳圖片並顯示出來。同時保存到數據庫

內存 pbm 命令 數據 lena bms south 類對象 private

通常,我們在開發軟件或者網站是否,通常有時候需要添加圖片,我們怎麽做呢,直接貼例子。

前提是添加openFileDialog控件哈

技術分享圖片

#region 定義公共的類對象及變量
SqlConnection sqlcon; //聲明數據庫連接對象
SqlDataAdapter sqlda; //聲明數據橋接器對象
DataSet myds; //聲明數據集對象
//定義數據庫連接字符串
//string strCon = @"Data Source=lll;Database=db_CSharp;uid=sa;pwd=;";


string strCon = "Data Source=ASUS-PC;Initial Catalog=BookManager;Integrated Security=True";
#endregion

private void AddPhoto_Load(object sender, EventArgs e) //初始化
{
ShowInfo();//顯示用戶信息
}

【1】選擇圖片按鈕

private void button1_Click(object sender, EventArgs e)//選擇圖片


{
//定義可選擇的頭像類型
openFileDialog1.Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf";
openFileDialog1.Title = "選擇用戶頭像";
//判斷是否選擇了頭像
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//顯示選擇的用戶頭像

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
textBox1.Text = "";
}
}

【2】保存圖片按鈕

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("用戶名不為空","信息提示");
}
if (openFileDialog1.FileName == "")
{
MessageBox.Show("用戶頭像不為空", "信息提示");
}

sqlcon = new SqlConnection(strCon);//創建鏈接對象
sqlcon.Open();//打開數據庫
string sqlstr = "select * from userphoto where name=‘"+textBox1.Text.Trim()+"‘";
SqlCommand Mycom = new SqlCommand(sqlstr,sqlcon);
Mycom.ExecuteNonQuery();
if (null != Mycom.ExecuteScalar())
{
MessageBox.Show("用戶名 "+textBox1.Text.Trim() + " 已經存在,請重新註冊用戶名", "信息提示");
textBox1.Text = "";
textBox1.Focus();
}
//sqlcon.Close();//關閉數據庫
/***************************************************************/
else if (openFileDialog1.FileName != "" && textBox1.Text != "")//如果用戶名不為空,並且文件被選中
{
//添加用戶信息
if (AddInfo(textBox1.Text, openFileDialog1.FileName))
{
MessageBox.Show("用戶信息添加成功", "信息提示");
}

}
//else
//{
// MessageBox.Show("請您輸入用戶名", "信息提示");
//}
sqlcon.Close();//關閉數據庫
ShowInfo();
}

【3】調用的函數:

#region 添加用戶信息
/// <summary>
/// 添加用戶信息
/// </summary>
/// <param name="strName">用戶名稱</param>
/// <param name="strImage">選擇的頭像名稱</param>
/// <returns>執行成功,返回true</returns>
private bool AddInfo(string strName, string strImage)
{
sqlcon = new SqlConnection(strCon);//創建數據庫連接對象


FileStream FStream = new FileStream(//創建文件流對象
strImage, FileMode.Open, FileAccess.Read);
BinaryReader BReader = new BinaryReader(FStream);//創建二進制流對象
byte[] byteImage = BReader.ReadBytes((int)FStream.Length);//得到字節數組


SqlCommand sqlcmd = new SqlCommand(//創建命令對象
"insert into userphoto(name,photo) values(@name,@photo)", sqlcon);

sqlcmd.Parameters.Add("@name", //添加參數並賦值
SqlDbType.VarChar, 50).Value = strName;
sqlcmd.Parameters.Add("@photo",//添加參數並賦值
SqlDbType.Image).Value = byteImage;

sqlcon.Open();//打開數據庫連接
sqlcmd.ExecuteNonQuery();//執行SQL語句
sqlcon.Close();//關閉數據庫連接
return true;//方法返回布爾值
}
#endregion

*******************************************************************

#region 在DataGridView中顯示用戶名稱
/// <summary>
/// 在DataGridView中顯示用戶名稱
/// </summary>
private void ShowInfo()//顯示到DataGridView表格
{
sqlcon = new SqlConnection(strCon);//創建鏈接對象
sqlda = new SqlDataAdapter("select name as 用戶名稱 from userphoto", sqlcon);//查詢SQL
myds = new DataSet();//填充數據集
sqlda.Fill(myds);
dataGridView1.DataSource = myds.Tables[0];//顯示
}
#endregion

********************************************************

#region 顯示用戶信息
/// <summary>
/// 顯示用戶頭像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//記錄選擇的用戶名
string strName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
if (strName != "")
{
sqlcon = new SqlConnection(strCon); //實例化數據庫連接對象
//實例化數據橋接器對象
sqlda = new SqlDataAdapter("select * from userphoto where name=‘" + strName + "‘", sqlcon);
myds = new DataSet(); //實例化數據集對象
sqlda.Fill(myds); //填充數據集
//顯示用戶名稱
textBox1.Text = myds.Tables[0].Rows[0][1].ToString();
//使用數據庫中存儲的二進制頭像實例化內存數據流
MemoryStream MStream = new MemoryStream((byte[])myds.Tables[0].Rows[0][2]);
pictureBox1.Image = Image.FromStream(MStream); //顯示用戶頭像
}
}
#endregion

文章出處:https://blog.csdn.net/paullink520/article/details/19563881

Winform下如何上傳圖片並顯示出來。同時保存到數據庫