//定义将图片转化为长二进制代码的函数getphoto()
public Byte[] getphoto(string photopath)
{
string str = photopath
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read)
Byte[] bytBLOBData = new Byte[file.Length]
file.Read(bytBLOBData, 0, bytBLOBData.Length)
file.Close()
return bytBLOBData
}//这是定义函数..
然后..就是将转换成二进制码的图片插入数据库中..下面是简单的也是重要的sql语句..
if (this.pictureBox1.Image != null)
{
sql1 = sql1 + ",Photo"
sql2 = sql2 + ",bytBLOBData"
Byte[] bytBLOBData = getphoto(openFileDialog1.FileName)
cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData))
}
接下来..是读取...
string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString()
OleDbCommand cmd = new OleDbCommand(sql, connection1)
if (Convert.DBNull != cmd.ExecuteScalar())
pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()))//读取长二进制为图片..
插入: //单击图片选择添加的图片 private void pic_Click(object sender, EventArgs e){ dlg.Filter = "JPG|*.jpg|BMP|*.bmp|PNG|*.png"
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pic.Image = Image.FromFile(dlg.FileName)
txtFilePath = dlg.FileName
}
} public byte[] picData public string txtFilePath = "" 添加确定按钮代码: f (txtFilePath != "")
{
try
{
FileStream fs = new FileStream(txtFilePath, FileMode.Open, FileAccess.Read)
int len = Convert.ToInt32(fs.Length)
b = new byte[len]
fs.Read(b, 0, len)
fs.Close()
}
catch
{
b = null
}
}SqlConnection conn = new SqlConnection(strConn)
conn.Open()SqllCommand cmdInsert = new SqlCommand()
cmdInsert.Connection = conn
cmdInsert.CommandText =插入语句;cmdInsert.Parameters.Add("@照片", SqlDbType.Image) if (txtFilePath == "")
{
cmdInsert.Parameters["@照片"].Value = DBNull.Value
}
else
{
cmdInsert.Parameters["@照片"].Value = b
}
cmdInsert.ExecuteNonQuery()
conn.Close()获取: public byte[] picDataSqlConnection conn = new SqlConnection(strConn)
SqlCommand cmd = new SqlCommand()
cmd.Connection = conn
cmd.CommandText = "select * from 联系人 where 编号=" + ID.ToString()
SqlDataAdapter sda = new SqlDataAdapter(cmd)
DataSet ds = new DataSet()sda.Fill(ds) if (ds.Tables[0].Rows.Count == 1)
{
if (ds.Tables[0].Rows[0]["照片"] == DBNull.Value)
{ //pic为picturebox控件
pic.Image = PhoneBoook.Properties.Resources.DeskShade//为空的话给个默认图片
}
else
{
byte[] b = (byte[])(ds.Tables[0].Rows[0]["照片"])
pic.Image = Image.FromStream(new MemoryStream(b))
picData = b
}
}
这个问题不太清楚,不过文件的最终存储方式都是二进制的数据流,所以你得先把图片转化为二进制的文件,那就得用汇编了,得到二进制文件后程序就好写了,在你写入文件后要得到图片还得在用汇编让它转化问图片格式。具体怎么转化不清楚,c语言里或许有相应的函数库。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)