请问C#怎么用二进制流存储图片到Sql Server中.?

2025-05-01 09:29:20
推荐回答(4个)
回答1:

正好我手里有这样的源码请你参考一下:
首先..定义一个函数..将图片转化为二进制码
//定义将图片转化为长二进制代码的函数getphoto()
public Byte[] getphoto(string photopath)
{
string str = photopath;
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[file.Length];
file.Read(bytBLOBData, 0, bytBLOBData.Length);
file.Close();
return bytBLOBData;
}//这是定义函数..

然后..就是将转换成二进制码的图片插入数据库中..下面是简单的也是重要的sql语句..
if (this.pictureBox1.Image != null)
{
sql1 = sql1 + ",Photo";
sql2 = sql2 + ",bytBLOBData";
Byte[] bytBLOBData = getphoto(openFileDialog1.FileName);
cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData));
}

接下来..是读取...

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()));//读取长二进制为图片..

回答2:

给你的我VB.net的代码,供你参考,C#不会,但我想你能看懂.

#Region "二进制文件的存储函数"

Public Function BinaryToFile(ByRef TableRowColItem As Object, ByVal FileName As String) As Boolean

Dim data As Byte() = TableRowColItem
Dim myfilestream As New System.IO.FileStream(FileName, IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
Return True
End Function

Public Function BinaryToImage(ByRef TableRowColItem As Object, ByRef image As Image) As Boolean

Dim data As Byte() = TableRowColItem

Dim imgStream As New System.IO.MemoryStream '(data)
imgStream.Write(data, 0, data.Length)
image = System.Drawing.Image.FromStream(imgStream)
imgStream.Close()
imgStream.Dispose()

Return True
End Function

'ok
Public Function BinaryFromFile(ByVal FileName As String, ByRef TableRowColItem As Object) As Boolean

Using myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
TableRowColItem = data

End Using

Return True
End Function

Public Function BinaryFromImage(ByRef Image As Image, ByRef TableRowColItem As Object) As Boolean

Dim imgStream As New MemoryStream
Dim b As New Bitmap(Image)

b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg)

Dim data As Byte() = imgStream.GetBuffer
TableRowColItem = data
data = TableRowColItem
imgStream.Close()
imgStream.Dispose()

Return True
End Function

Public Function BinaryFromImage(ByVal Image As Image, ByRef TableRowColItem As Object, ByVal imgFormat As System.Drawing.Imaging.ImageFormat) As Boolean
Dim imgStream As New MemoryStream

Dim data As Byte() = imgStream.GetBuffer

TableRowColItem = data
data = TableRowColItem
imgStream.Close()
Return True
End Function

#End Region

回答3:

//定义将图片转化为长二进制代码的函数getphoto()
public
Byte[]
getphoto(string
photopath){
string
str
=
photopath;
FileStream
file
=
new
FileStream(str,
FileMode.Open,
FileAccess.Read);
Byte[]
bytBLOBData
=
new
Byte[file.Length];
file.Read(bytBLOBData,
0,
bytBLOBData.Length);
file.Close();
return
bytBLOBData;
if
(this.pictureBox1.Image
!=
null){
sql1
=
sql1
+
",Photo";

回答4:

你要的东西,在这里:

http://www.zu14.cn/2009/01/04/csharp-image-sqlserver/