111,131
社区成员
发帖
与我相关
我的任务
分享public Image ReadImage(int empid)
{
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;
}
MemoryStream buf=new MemoryStream((byte[])reader[0]); //reader是datareader,是你那个存图片的字段
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image; private void ShowPic()
{
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();
}
}
System.IO.MemoryStream mStream = new MemoryStream();
GetFile( fileName,mStream ); //把文件转换为Stream,你要是数据库里已经转完了不不用了.
mStream.Position = 0;
System.IO.Stream stream = (System.IO.Stream)mStream;
picPreView.Image = Image.FromStream( stream );
Image MyImage = Image.FromStream(new MemoryStream((byte[])DT.Rows[0]["ImageCol"]);
picturebox1.Image = MyImage; 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 Function
Public 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