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


    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
  • 相关阅读:
    博客作业01-抽象数据类型
    C语言最后一次作业--总结报告
    C语言博客作业--函数嵌套调用
    C语言博客作业--结构体
    数位DP之奥义
    POJ 3311 Hie with the Pie (状压DP)
    kaungbin_DP S (POJ 3666) Making the Grade
    POJ 3666 Making the Grade
    kuangbin_SegTree M (HDU 4553)
    kuangbin_SegTree I (HDU 1540)
  • 原文地址:https://www.cnblogs.com/wubiyu/p/941311.html
Copyright © 2020-2023  润新知