title: how to download binary image from database?
body: <p>I have stored image in db as binary format. While click on link button I want to download that image. Help me to find a proper solution. Thank you.</p>
  
  <p>Code:</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
       {
  
        pdfDataSetTableAdapters.tbl_imgTableAdapter td;
        td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
        DataTable dt = new DataTable();
        dt = td.GetId();        //Sql1
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "Id";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Id--", ""));
       }
  
    }
    protected void Button1_Click(object sender, EventArgs e)
     {
       pdfDataSetTableAdapters.tbl_imgTableAdapter td;
       td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
       DataTable dt = new DataTable();
       dt = td.GetImg(int.Parse(DropDownList1.SelectedValue));   //Sql2
       foreach (DataRow row in dt.Rows)
        {
           byte[] img2 = (byte[])row["img"];
           string base2 = Convert.ToBase64String(img2);
           Image1.ImageUrl = "data:image/jpg;base64," + base2;
         }     
     }
  
  protected void LinkButton1_Click(object sender, EventArgs e)
     {
        // image download
     }
  </code></pre>
  
  <p>Sql1:</p>
  
  <pre><code>SELECT Id FROM tbl_img
  </code></pre>
  
  <p>Sql2:</p>
  
  <pre><code>SELECT img FROM tbl_img WHERE (Id = @Id)
  </code></pre>
  
  <p>aspx:</p>
  
  <pre><code>&lt;asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"&gt; &lt;/asp:DropDownList&gt;
  &lt;asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show" /&gt;
  &lt;asp:LinkButton ID="LinkButton1" runat="server"&gt;Download&lt;/asp:LinkButton&gt;
  &lt;asp:Image ID="Image1" runat="server" /&gt;
  </code></pre>
  
  <p>DB:</p>
  
  <p><img src="https://i.stack.imgur.com/wOito.jpg" alt="enter image description here">
  <img src="https://i.stack.imgur.com/JjXeW.jpg" alt="enter image description here"></p>
  
  <p>O/P Screen:</p>
  
  <p><img src="https://i.stack.imgur.com/ff9Fp.jpg" alt="enter image description here"></p>