日期:2014-05-17 浏览次数:21360 次
public static class BitmapHelper { public static Bitmap BytesToBitmap(byte[] Bytes) { MemoryStream stream = null; try { stream = new MemoryStream(Bytes); return new Bitmap((Image)new Bitmap(stream)); } catch (ArgumentNullException ex) { throw ex; } catch (ArgumentException ex) { throw ex; } finally { stream.Close(); } } public static byte[] BitmapToBytes(Bitmap Bitmap) { MemoryStream ms = null; try { ms = new MemoryStream(); Bitmap.Save(ms, Bitmap.RawFormat); byte[] byteImage = new Byte[ms.Length]; byteImage = ms.ToArray(); return byteImage; } catch (ArgumentNullException ex) { throw ex; } finally { ms.Close(); } } }
------解决方案--------------------
//保存图片: SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master"); conn.Open(); SqlCommand cmd = new SqlCommand("insert into image values(@i)", conn); byte[] ib = new byte[60000]; FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read); fs.Read(ib, 0, 60000); cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length); cmd.Parameters["@i"].Value = ib; cmd.ExecuteNonQuery(); conn.Close(); MessageBox.Show("保存成功"); //显示图片: SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master"); conn.Open(); SqlCommand cmd = new SqlCommand("select image1 from image", conn); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { MemoryStream buf = new MemoryStream((byte[])reader[i]); Image image = Image.FromStream(buf,true); this.pictureBox1.Image = image; } }
------解决方案--------------------
1.任何文件以二进制存入Access数据库:
……
if (opFlDlg.ShowDialog() == DialogResult.OK)
{
Stream fl = null;