OpenFileDialog FDup = new OpenFileDialog ();
FDup.Filter = "Image(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.png)|*.png";
if (FDup.ShowDialog() == DialogResult.OK)
{
FilePath = FDup.FileName;
this.txbPath.Text = FilePath;
}
//把图片转换为数据流
FileName = this.txbPath.Text;
FileStream Fstream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
Byte[] mybtye = new Byte[Fstream.Length];
int bt = Fstream.Read(mybtye, 0, Convert.ToInt32(Fstream.Length));//必需的.把数据流转换为int
Fstream.Close();
F_str_str = "insert into Document_Image (Document_ID,Image) values('" + this.txbDocument_ID.Text.ToString().Trim() + "',@Image)";
SqlConnection sqlcon = basoprate.GetCon();
sqlcon.Open();
SqlCommand mycmd = new SqlCommand(F_str_str, sqlcon);
//设置变量@Image值
SqlParameter sqlptr = new SqlParameter("@Image", SqlDbType.Image);
sqlptr.Value = mybtye;
mycmd.Parameters.Add(sqlptr);
mycmd.ExecuteNonQuery();
************************************************************************************
string F_str_str = "Select Image from Document_Image";
byte[] mybyte = new byte[0];
SqlDataReader myread = basOprt.GetRead(F_str_str);
while (myread.Read())
{
mybyte = (byte[])myread["Image"];//转换为进制数
MemoryStream Mstream = new MemoryStream(mybyte);//把数据转换为数据流
Image img = Image.FromStream(Mstream, true);//把数据流转换为图片
//设置图片显示格式
Bitmap map = new Bitmap(img);
Point pot = new Point(map.Size);
if (pot.X > pictureBox.Width || pot.Y > pictureBox.Height)
{
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
pictureBox.Image = img;
}