从数据库中读取二进制数据,并显示图片

region 读取数据库中图片到内存.并显示
        
public void LoadToMemoryAndDisable(string serverAdress, string database)
        
{
            
//读取数据库中图片到内存.并显示
            SqlConnection conn = new SqlConnection("server=" + serverAdress + ";integrated security = sspi;database = " + database);
            SqlCommand cmd 
= new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn);
            conn.Open();
            SqlDataReader dr;
            
try
            
{
                dr 
= cmd.ExecuteReader();
                dr.Read();
                System.Data.SqlTypes.SqlBinary sb 
= dr.GetSqlBinary(2);
                
//或byte[] imageData = (byte[])dr[2];
                MemoryStream ms = new MemoryStream(sb.Value);//在内存中操作图片数据
                Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
                
this.pictureBox1.Image = bmp;
                dr.Close();
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

            
finally
            
{
                conn.Close();
            }

        }

        
#endregion
原文地址:https://www.cnblogs.com/wubiyu/p/941311.html