Thursday, 26 July 2012

Best way of Show and Save Image in asp.net

  //Convert image in byte and show image...................
protected void Upload_Click(object sender, EventArgs e)
    {
        try
        {
            Session["img"] =(byte []) FileUpload1.FileBytes;
            System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image1.ImageUrl = "data:image/jpg;base64," + base64String;
            Image1.Visible = true;
        }
        catch (Exception ex)
        { }
    }

 //-convert byte array to image and Save in desire location....
protected void Save_Click(object sender, EventArgs e)
    {
        try
        {
            byte[] image = (byte[])Session["img"];

                MemoryStream ms = new MemoryStream(image);

            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

           string path = Server.MapPath("~/image/");
            path += "test.jpg";
            returnImage.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
        }
        catch (Exception ex)
        { }
    }

No comments:

Post a Comment

Table Partitioning in SQL Server

  Table Partitioning in SQL Server – Step by Step Partitioning in SQL Server task is divided into four steps: Create a File Group Add Files ...