然后在
winfrom中读取二进制流后转为图片在pictureBox显示
问题二:
pictureBox点击事件中获取图片然后将图片转为二进制数据流
以上都是公用的一个字段
5 个解决方案
#1
将图片存为文件吧,这样简单点,效率高,将你的二进制流写入文件就行。
#2
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";
sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ms.Close();
}
}
SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;
#4
aomanman ,感谢你的代码
#5
有源文件下载吗?
#1
将图片存为文件吧,这样简单点,效率高,将你的二进制流写入文件就行。
#2
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";
sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ms.Close();
}
}
SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;
#3
#4
aomanman ,感谢你的代码
#5
有源文件下载吗?