Skip to main content

show image in asp.net form from database

In order to show image from database to webform first add an image control in webpage.

  <asp:Image ID="imgStudentImage" runat="server" Height="157px" Width="160px" />

Then add new webform to read image from database for example displayimage.aspx
and in code behind model write the following code.

public string str = //your string
    public SqlConnection conn;
    public SqlCommand sql = new SqlCommand();
    public SqlDataReader dr;
    public string strSQL;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(str);
        conn.Open();
        long id = Convert.ToInt64(Request.QueryString["mId"]);
        strSQL = "";
        strSQL = strSQL + " SELECT snap from StudentPicture ";
        strSQL = strSQL + " WHERE IsOdl='true' and (Convert(Varchar(50),AdmissionId) = '" + id + "' or (Convert(Varchar(50),RegisterationNumber)='" + id + "'))";
        /**/
        sql = new SqlCommand(strSQL, conn);
        dr = sql.ExecuteReader(CommandBehavior.CloseConnection);
        if (dr.Read())
        {
            Response.ContentType = "image/gif";
            Response.BinaryWrite((byte[])dr["snap"]);
        }
        else
        {
            System.Drawing.Image img1 = DrawImage();
            byte[] myarray = imageToByteArray(img1);
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "image /png";
            Response.AddHeader("content-disposition", "attachment;filename="
            + "imagenotfound.png");
            Response.BinaryWrite(myarray);
            Response.Flush();
            Response.End();
        }

        conn.Close();
    }
    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }

    public System.Drawing.Image DrawImage()
    {
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(100, 100);
        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
        graphics.DrawString("No Image Found", new System.Drawing.Font("Tahoma", 10), System.Drawing.Brushes.White, new System.Drawing.PointF(0, 0));
        return bitmap;
    }



And now in pageload of webpage where image is to be shown do the following code to showimage

 imgStudentImage.ImageUrl = "DisplayImage.aspx?mId=" + idfromfrmsearchstudent;

Done !! :)

Comments

Popular posts from this blog

Snake Xenzia

Hello friends did you remember the GAME you play on nokia 1100 , SNAKE XENZIA .Yeah the same you are thinking ,a snake eating food and go longer and longer till it dies. Why to play it on mobile when it is available on your pc ??? It is made in .net framework 4.0 .Just install the setup and play SNAKE XENZIA . Have Fun !! DOWNLOAD IT   HERE  . Related articles Rattlesnake Dreams "Classsic Snake Game In C"? Resident Evil 6 gamescom demo takes in a giant snake battle Snakes & ladders

Export Doc,Access,Image,CSV,Excel,Pdf,XML,HTML,Text,Print of Gridview in Asp.net

First import itextsharp.dll in the solution and use three namespces (basically for pdf) using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser;          Then Use the following code :- on each button  click  protected void Page_Load(object sender, EventArgs e)         {             if (!Page.IsPostBack)             {                 BindGridDetails(GridView1);             }         }                 protected DataTable BindGridDetails(GridView GridView1)         {   ...

New Smartphone with 12GB RAM, 1TB storage unveiled

Here is the News -- Turing Robotics Industries (TRI) has unveiled its latest smartphone - the Turing Phone Cadenza. It comes with 'Voice On' technology which enables the smartphone to be switched On/Off using voice commands. Users can also use these voice commands for biometric authentication. The Turing Phone Cadenza features a 5.8-inch Quad HD display of 1440x2560 pixel resolution. It is powered by not one, but two Qualcomm Snapdragon 830 (even though the same hasn't been officially announced) CPUs paired with a an astonishing 12GB of RAM and 1TB of internal storage. The storage can be further expanded up to 500GB by installing a microSD card. The Cadenza runs Turing's own Swordfish OS with deep learning Artificial Intelligence (AI) features a 60MP rear camera with IMAX 6K recording facility and a 20MP dual-front camera. Users can install up to 4 sim cards in the smartphone and it houses a huge 100Wh battery with graphene and hydrogen fuel cells.  In related ne...