有关C#程序中图片存入数据库,并取出显示在pictureBox中的问题
namespace图片存储{publicstaticByte[]ConvertPhotoToByte(stringphotoPath){Byte[]bytes=null;...
namespace 图片存储
{
public static Byte[] ConvertPhotoToByte(string photoPath)
{
Byte[] bytes = null;
string PicPath = photoPath;
FileStream file = new FileStream(PicPath, FileMode.Open, FileAccess.Read);
bytes = new Byte[file.Length];
file.Read(bytes, 0, bytes.Length);
file.Close();
return bytes;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog PhotoDialog = new OpenFileDialog();
PhotoDialog.Filter = "jpg|*.jpg|bmp|*.bmp";
PhotoDialog.InitialDirectory = @"F:\图片\";
PhotoDialog.ShowDialog();
if (PhotoDialog.FileName != null)
{
pictureBox1.ImageLocation = PhotoDialog.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection();
sql.ConnectionString = @"Data Source=XXXXX;Initial Catalog=tupian;Integrated Security=SSPI;";
try
{
sql.Open();
SqlCommand ReaderInfo = new SqlCommand("", sql);
Byte[] photo = ConvertPhotoToByte(pictureBox1.ImageLocation);
ReaderInfo.CommandText = "insert into pt " + " values('"+textBox1.Text.Trim()+"','"+photo+"')";
ReaderInfo.ExecuteNonQuery();
MessageBox.Show("添加成功!", "提示");
}
catch(Exception)
{
MessageBox.Show("数据库连接失败!");
}
sql.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Byte[] imagebytes = null;
SqlConnection sql = new SqlConnection();
sql.ConnectionString = @"Data Source=XXXXX;Initial Catalog=tupian;Integrated Security=SSPI;";
try
{
sql.Open();
SqlCommand cmd = new SqlCommand("", sql);
cmd.CommandText = "select * from pt where num='" + textBox2.Text + "'";
SqlDataAdapter adapter = new SqlDataAdapter(cmd.CommandText, sql);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
MemoryStream mysm = new MemoryStream((byte[])dataset.Tables[0].Rows[0][1]);
Bitmap bmpt = new Bitmap(mysm);
pictureBox2.Image = bmpt;
}
catch (Exception)
{
MessageBox.Show("数据库连接失败!");
}
sql.Close();
}
}
}
这个程序能添加 但是显示不出来,请高手帮我解决 展开
{
public static Byte[] ConvertPhotoToByte(string photoPath)
{
Byte[] bytes = null;
string PicPath = photoPath;
FileStream file = new FileStream(PicPath, FileMode.Open, FileAccess.Read);
bytes = new Byte[file.Length];
file.Read(bytes, 0, bytes.Length);
file.Close();
return bytes;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog PhotoDialog = new OpenFileDialog();
PhotoDialog.Filter = "jpg|*.jpg|bmp|*.bmp";
PhotoDialog.InitialDirectory = @"F:\图片\";
PhotoDialog.ShowDialog();
if (PhotoDialog.FileName != null)
{
pictureBox1.ImageLocation = PhotoDialog.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection();
sql.ConnectionString = @"Data Source=XXXXX;Initial Catalog=tupian;Integrated Security=SSPI;";
try
{
sql.Open();
SqlCommand ReaderInfo = new SqlCommand("", sql);
Byte[] photo = ConvertPhotoToByte(pictureBox1.ImageLocation);
ReaderInfo.CommandText = "insert into pt " + " values('"+textBox1.Text.Trim()+"','"+photo+"')";
ReaderInfo.ExecuteNonQuery();
MessageBox.Show("添加成功!", "提示");
}
catch(Exception)
{
MessageBox.Show("数据库连接失败!");
}
sql.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Byte[] imagebytes = null;
SqlConnection sql = new SqlConnection();
sql.ConnectionString = @"Data Source=XXXXX;Initial Catalog=tupian;Integrated Security=SSPI;";
try
{
sql.Open();
SqlCommand cmd = new SqlCommand("", sql);
cmd.CommandText = "select * from pt where num='" + textBox2.Text + "'";
SqlDataAdapter adapter = new SqlDataAdapter(cmd.CommandText, sql);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
MemoryStream mysm = new MemoryStream((byte[])dataset.Tables[0].Rows[0][1]);
Bitmap bmpt = new Bitmap(mysm);
pictureBox2.Image = bmpt;
}
catch (Exception)
{
MessageBox.Show("数据库连接失败!");
}
sql.Close();
}
}
}
这个程序能添加 但是显示不出来,请高手帮我解决 展开
展开全部
我建议你在数据库中只存储图片的路径,把图片存储到本地文件,这样能节约数据库资源提高速度,还方便读取
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Rows[0][1]里有数据吗?设个断点跟踪下这里,看下bmpt的值。再说
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
发给你了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询