Friday 24 May 2013

How to Convert Byte Array to Image and Image To Byte Array in asp.net


Description:-

                    In this Example we explain that how we can convert byte[] to image and image to byte[].
there is a sitution when this example is very useful.

  For Example:-

                  when you have to generate a report from gridview and in gridview there is a image of each student when user click on each row of grid the name name of image are displayed in report but image are not displayed for this reason to display student image in report you have to first convert byte[] to image and then bind to the report.


in which we Define two Function one for Covert ByteArray to Image and second is Image to ByteArray. when you Call this Function  First it will Check the Image Extention if it return True the it Will convert otherwise it will Return False.this Function will Read File and Open it and Convert it to the ByteArray. To Covert ByteArray to Image the FileStream Class is used to Return Image. and when Convert From Image to ByteArray BinaryWrite Class is used to Return ByteArray. look as Example


FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);





BinaryReader br = new BinaryReader(fs);

byte[] image = br.ReadBytes((int)fs.Length);

Before using this Example you have to Add NameSpace 


using System.IO;


How to Handle Concurrency in Linq to Sql Handle Concurrency in Linq to Sql

Read/Write Text File in Asp.Net with C# Example of Read/Write File

Validation For Enter only Ahphabet in TextBox Javascript Validation For only allows Ahphabets


Function:-

Byte[] to Image Coversion:-

public static byte[] ReadImageFile(string PostedFileName, string[] filetype)
    {
        bool isAllowedFileType = false;
        try
        {
            FileInfo file = new FileInfo(PostedFileName);

            strFile_Name = file.Name;

            foreach (string strExtensionType in filetype)
            {
                if (strExtensionType == file.Extension)
                {
                    isAllowedFileType = true;
                    break;
                }
            }
            if (isAllowedFileType)
            {
                FileStream fs = new FileStream(PostedFileName, FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);

                byte[] image = br.ReadBytes((int)fs.Length);

                br.Close();

                fs.Close();

                return image;
            }
            return null;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

Image to Byte[] Conversion:-


private static byte[] ReadImage(string p_postedImageFileName, string[] p_fileType)
    {
        bool isValidFileType = false;
        try
        {
            FileInfo file = new FileInfo(p_postedImageFileName);

            foreach (string strExtensionType in p_fileType)
            {
                if (strExtensionType == file.Extension)
                {
                    isValidFileType = true;
                    break;
                }
            }
            if (isValidFileType)
            {
                FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);

                byte[] image = br.ReadBytes((int)fs.Length);

                br.Close();

                fs.Close();

                return image;
            }
            return null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


How to Use:-

 ImageButton btndetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        ReportDocument rd = new ReportDocument();
       rd.Load(Server.MapPath("reportxm.rpt"));

        DataSet2 d1 = new DataSet2();
        Label l1=(Label) row.FindControl("l1");
       Label l2 = (Label)row.FindControl("l2");
       Image l3 = (Image)row.FindControl("Image1");
        DataRow dr = d1.kkkkk.NewRow();
        dr["name"]=l1.Text;
        dr["city"] = l2.Text;
        string s = Server.MapPath(l3.ImageUrl);
        bt = ReadImageFile(s, new string[] { ".GIF", ".gif", ".jpg", ".bmp", ".jpeg" });

        string msg = "<script type='text/javascript'>";
        msg += " alert('Conversion is successful!') ";
        msg += "</script>";
        ClientScript.RegisterClientScriptBlock(typeof(Page), "Test", msg);
     

        dr["photo"] = bt;

        d1.kkkkk.Rows.Add(dr);

        Response.Redirect("reportxml.aspx?l1=" +l1.Text+"&l2="+l2.Text+"&l3="+l3.ImageUrl);

      

0 comments:

Post a Comment