C#读取数据库IMAGE字段的内容。
C#中如何从数据库读取IMAGE类型的字段内容,然后再如何将其宽度和高度放大一定的倍数或放大到一定的高度和宽度,并存储到硬盘中。问题如能满意的解决,再追加20分。...
C#中如何从数据库读取IMAGE类型的字段内容,然后再如何将其宽度和高度放大一定的倍数或放大到一定的高度和宽度,并存储到硬盘中。
问题如能满意的解决,再追加20分。 展开
问题如能满意的解决,再追加20分。 展开
展开全部
读取... 读取长二进制为图片..
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()));
放大就不知道了
保存:
string filename= textBox1.Text;//"c:\\IMG_0117.jpg";
BinaryReader reader=null;
FileStream myfilestream = new FileStream(filename,FileMode.Open);
try
{
reader=new BinaryReader(myfilestream);
byte[] image = reader.ReadBytes((int)myfilestream.Length);
using (SqlConnection conn = new SqlConnection("server=test05;database=esdb2;uid=datatran;pwd=qyrl"))
{
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText =@"INSERT INTO photo(photo) VALUES (@photo)";
command.Parameters.Add("@photo", image);
conn.Open();
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("文件保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
catch(IOException ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
if(reader!=null)
reader.Close();
}
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()));
放大就不知道了
保存:
string filename= textBox1.Text;//"c:\\IMG_0117.jpg";
BinaryReader reader=null;
FileStream myfilestream = new FileStream(filename,FileMode.Open);
try
{
reader=new BinaryReader(myfilestream);
byte[] image = reader.ReadBytes((int)myfilestream.Length);
using (SqlConnection conn = new SqlConnection("server=test05;database=esdb2;uid=datatran;pwd=qyrl"))
{
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText =@"INSERT INTO photo(photo) VALUES (@photo)";
command.Parameters.Add("@photo", image);
conn.Open();
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("文件保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
catch(IOException ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
if(reader!=null)
reader.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询