Public Shared Function GetImgBySQL(ByVal FileldName As String, ByVal TableName As String, ByVal sql As String) As Image Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("select top 1 " & FileldName) & " from " & TableName), Interaction.IIf((sql <> ""), (" where " & sql), ""))) Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True)) Dim image2 As Image = Nothing Try Dim command As New SqlCommand(cmdText, connection) command.CommandType = CommandType.Text connection.Open Dim buffer As Byte() = DirectCast(command.ExecuteScalar, Byte()) command.Dispose If (buffer Is Nothing) Then Return image2 End If If (buffer.Length > 0) Then Dim stream As New MemoryStream(buffer) image2 = Image.FromStream(stream) stream.Close stream = Nothing End If Catch exception1 As Exception ProjectData.SetProjectError(exception1) Dim exception As Exception = exception1 Throw New NotSupportedException(exception.Message) ProjectData.ClearProjectError Finally connection.Close End Try Return image2 End FunctionPublic Shared Function UpdateImgBySQL(ByVal TableName As String, ByVal FieldName As String, ByVal ImgFieldValue As String, ByVal ParaSQL As String) As Integer If (ImgFieldValue = "") Then Return 0 End If Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("update " & TableName & " set ") & FieldName & " = @img "), Interaction.IIf((ParaSQL <> ""), (" where " & ParaSQL), ""))) Dim num As Integer = 0 Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True)) Try Dim command As New SqlCommand(cmdText, connection) connection.Open Dim stream As New FileStream(ImgFieldValue, FileMode.Open, FileAccess.Read) Dim array As Byte() = New Byte((CInt(stream.Length) + 1) - 1) {} stream.Read(array, 0, CInt(stream.Length)) stream.Close stream = Nothing command.Parameters.Add("@img", SqlDbType.Image).Value = array num = command.ExecuteNonQuery command.Dispose Catch exception1 As Exception ProjectData.SetProjectError(exception1) Dim exception As Exception = exception1 Throw New Exception(exception.Message) ProjectData.ClearProjectError Finally connection.Close End Try Return num End Function
Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("select top 1 " & FileldName) & " from " & TableName), Interaction.IIf((sql <> ""), (" where " & sql), "")))
Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True))
Dim image2 As Image = Nothing
Try
Dim command As New SqlCommand(cmdText, connection)
command.CommandType = CommandType.Text
connection.Open
Dim buffer As Byte() = DirectCast(command.ExecuteScalar, Byte())
command.Dispose
If (buffer Is Nothing) Then
Return image2
End If
If (buffer.Length > 0) Then
Dim stream As New MemoryStream(buffer)
image2 = Image.FromStream(stream)
stream.Close
stream = Nothing
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Throw New NotSupportedException(exception.Message)
ProjectData.ClearProjectError
Finally
connection.Close
End Try
Return image2
End FunctionPublic Shared Function UpdateImgBySQL(ByVal TableName As String, ByVal FieldName As String, ByVal ImgFieldValue As String, ByVal ParaSQL As String) As Integer
If (ImgFieldValue = "") Then
Return 0
End If
Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("update " & TableName & " set ") & FieldName & " = @img "), Interaction.IIf((ParaSQL <> ""), (" where " & ParaSQL), "")))
Dim num As Integer = 0
Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True))
Try
Dim command As New SqlCommand(cmdText, connection)
connection.Open
Dim stream As New FileStream(ImgFieldValue, FileMode.Open, FileAccess.Read)
Dim array As Byte() = New Byte((CInt(stream.Length) + 1) - 1) {}
stream.Read(array, 0, CInt(stream.Length))
stream.Close
stream = Nothing
command.Parameters.Add("@img", SqlDbType.Image).Value = array
num = command.ExecuteNonQuery
command.Dispose
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Throw New Exception(exception.Message)
ProjectData.ClearProjectError
Finally
connection.Close
End Try
Return num
End Function
byte[] buffer = *****;
System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes));
picturebox1.Image = image;注意代码没有经过调试,可能有问题。
picturebox1.Image = _MyImage;
WEBFORM
Response.ContentType = "image/jpeg";
Response.Clear();
Response.BufferOutput = true;
Response.BinaryWrite((byte[])_Table.Rows[0]["ImageData"]);
Response.Flush();
public void AddImageToSQLDB()
{
//将需要存储的图片读取为数据流
FileStream fs = new FileStream("这里写你图片的路径及文件名", FileMode.Open, FileAccess.Read);
Byte[] btye = new byte[fs.Length];
fs.Read(btye, 0, Convert.ToInt32(fs.Length));
fs.Close();
string updateCommand = "UPDATE 数据表 SET image字段=@imageData WHERE 条件字段= '条件值'"; SqlConnection connection = new SqlConnection("这里写你的数据库连接");
SqlCommand command = new SqlCommand(updateCommand, connection);
SqlParameter parameter = new SqlParameter("@imageData", SqlDbType.Image);
parameter.Value = btye;
command.Parameters.Add(parameter); try
{
connection.Open();
command.ExecuteNonQuery();
connection.Close(); }
catch
{
connection.Close();
}
}记得给分哦 ^_^
picturebox1.Image = MyImage;
GetFile( fileName,mStream ); //把文件转换为Stream,你要是数据库里已经转完了不不用了.
mStream.Position = 0;
System.IO.Stream stream = (System.IO.Stream)mStream;
picPreView.Image = Image.FromStream( stream );
private void ShowImage()
{
this.pictureBox1.Image = LoadImageFromDB();
}
private Image LoadImageFromDB()
{
string selectCommannd = "SELECT 图片字段 FROM 数据表 WHERE 条件字段= '条件值'";
DataTable dt = SQLDataAccess.GetDataBySelectCommand(selectCommannd); try
{
byte[] MyData = (byte[])dt.Rows[0]["MenuIcon"]; using (MemoryStream ms = new MemoryStream(MyData))
{
using (Bitmap Origninal = new Bitmap(ms))
{
Bitmap returnBmp = new Bitmap(Origninal.Width, Origninal.Height);
Graphics g = Graphics.FromImage(returnBmp);
g.DrawImage(Origninal, 0, 0, Origninal.Width, Origninal.Height);
g.Dispose();
ms.Close(); return (Image)returnBmp;
}
} }
catch
{
return null;
}
}
{
Bitmap returnBmp = new Bitmap(Origninal.Width, Origninal.Height);
Graphics g = Graphics.FromImage(returnBmp);
g.DrawImage(Origninal, 0, 0, Origninal.Width, Origninal.Height);
g.Dispose();
ms.Close(); return (Image)returnBmp;
}
可以换为 return new Bitmap(ms);
因为当时为有其它用途,直接拷贝了,呵呵 -_-
{ SqlConnection cn = new SqlConnection(DbHelperSQL.connectionString);
try
{
cn.Open();
string sql = "SELECT User_ImgContent FROM tbl_FriendsUser where User_Id=2";
//Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand(sql, cn);
Object objimg = cmd.ExecuteScalar();
if (objimg != null)
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])objimg;
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
System.Windows.Forms.PictureBox picbox = new PictureBox();
picbox.Image = Image.FromStream(stmBLOBData);
//picbox.Width = 50;
//picbox.Height = 50;
picbox.SizeMode = PictureBoxSizeMode.AutoSize;
gbDateView.Controls.Add(picbox);
}
else
{
MessageBox.Show("没有图片数据");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{
cn.Close();
}}
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
{
Image myImage = null;
byte[] photo = new byte[0];
string sql = "SELECT EmpImage FROM T_HR_Employee Where EmpId = " + empid + ""; try
{
using (SqlConnection con = new SqlConnection(connection))
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
photo = (byte[])cmd.ExecuteScalar();
if (photo.Length > 0)
{
using (MemoryStream myStream = new MemoryStream(photo, true))
{
//myStream.Write(photo, 0, photo.Length);
Bitmap bmp = new Bitmap(myStream);
Bitmap bmp1 = new Bitmap(bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(bmp1);
g.DrawImage(bmp, 0, 0);
myImage = (Image)bmp1;
g.Dispose();
bmp.Dispose();
}
}
}
}
catch
{ }
return myImage;
}
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;
}
}
仅供参考!
无数据,直接赋值null给控件