读取数据库image字段显示在picturebox,找了很多帖子,都是保存成临时文件急求不保存成文件,直接显示的代码解决问题的100分奉上在线等回复

解决方案 »

  1.   

    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
      

  2.   

    假设你的数据保存在buffer中
    byte[] buffer  = *****;
    System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes));
    picturebox1.Image = image;注意代码没有经过调试,可能有问题。
      

  3.   

    例如你获取TABLE里WINFORMImage _MyImage = Image.FromStream(new MemoryStream((byte[])_Table.Rows[0]["ImageData"]); 
    picturebox1.Image = _MyImage;
    WEBFORM
    Response.ContentType = "image/jpeg"; 
    Response.Clear(); 
    Response.BufferOutput = true; 
    Response.BinaryWrite((byte[])_Table.Rows[0]["ImageData"]); 
    Response.Flush(); 
     
      

  4.   

     
    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();
                }
            }记得给分哦 ^_^
      

  5.   

    DT为含有IMAGE字段的表:Image MyImage = Image.FromStream(new MemoryStream((byte[])DT.Rows[0]["ImageCol"]); 
    picturebox1.Image = MyImage; 
      

  6.   

    用Image.FromStream (Stream) 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     );
      

  7.   


    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;
                }
            }
      

  8.   

    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; 
                        } 
    可以换为 return new Bitmap(ms);
    因为当时为有其它用途,直接拷贝了,呵呵 -_-
      

  9.   

    可以直接把字节流转为图片:pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])dataTable.Rows[0]["PicData"]);
      

  10.   

    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();
        }}
      

  11.   

    MemoryStream  buf=new  MemoryStream((byte[])reader[0]);  //reader是datareader,是你那个存图片的字段
    Image  image=Image.FromStream(buf,true);  
    pictureBox1.Image=image;  
      

  12.   

     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;
            }
      

  13.   

    保存图片: 
    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;  
                    }  
                }  
    仅供参考!
      

  14.   

    有数据就用流处理,再赋值给控件
    无数据,直接赋值null给控件