Webservice return the Image of the Customer in ASP.NET 2.0


Webservice returning image of the customer in ASP.NET 2.0

 

Now I will explain how to get the images from Oracle database and how to display it on the ASP.NET 2.0 web page.

 

In oracle images are saved as blob data type.

 

In my application I have written one webservice, it will send the signature image of the customer to the other application which is saved in my application database Oracle database.

 

Webservice will provide security for database when one application requires the data from other application. The application which requires the data will send a request to the webservice so the webservice will give the response depending on the customer input. In this way it will provide the security to out database.

 

The webservice code is looking like this:

 

The below webservice will take the Customer ID and return the signature of the customer to the other application which send the request to webservice.

 

This is the working code.

 

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data.OracleClient;

using System.IO;

using System.Data;

using Microsoft.ApplicationBlocks.Data;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service()

    {

 

    }

 

    [WebMethod]

    public string Image(int CustID)

    {

        OracleConnection con = new OracleConnection("Connection String");

        OracleCommand cmd = new OracleCommand();

        string imageString = "";

        int id = 0;

        OracleDataReader dr;

        cmd.Connection = con;

        cmd.CommandType = System.Data.CommandType.Text;

        cmd.CommandText = "select * from Customer_Table where CLIENT_ID= " + id;

        con.Open();

        dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

        int Cust_ID = CustID;

        byte[] arrpicture = new byte[0];

        string imageType = "";

        if (dr.Read())

        {

            arrpicture = (byte[])dr[2];

            imageType = dr[4].ToString();

        }

        con.Close();

 

        imageString = Convert.ToBase64String(arrpicture);

        string xml_String = "";

        xml_String = "<Signatures><Customer><CustID>" + Cust_ID + "</CustID><Signature>" + imageString + "</Signature><imageType>" + imageType + "</imageType></Customer></Signatures>";

        return xml_String;

    }

}

 

 

Now I will show how to display the signature image in ASP.NET 2.0 application.

 

Enter the customer ID of the customer and click on the signature button.

 

  a1.bmp
The out put will be looking like this.

a2.bmp
The code written under button click event is

 

protected void b1_Click(object sender, EventArgs e)

    {

        string res = "";

        int i = 0;

        string image = "";

        int CustID = Convert.ToInt32(t1.Text);

 

        Service a = new Service();

        res = a.Image(CustID);

 

        XmlDocument xDoc = new XmlDocument();

 

        xDoc.LoadXml(res);

 

        foreach (System.Xml.XmlNode x in xDoc.ChildNodes[0].ChildNodes)

        {

 

            XmlNodeList Signature = xDoc.GetElementsByTagName("Signature");

 

            image = Signature[i].InnerText;

 

            i++;

        }

 

        byte[] arrpicture = System.Convert.FromBase64String(image);

 

        Response.ContentType = "Image/BMP";

 

        Response.BinaryWrite(arrpicture);

    }

 

 

But it will override the data on the data on the webform.

 

Now I will explain how to display the blobe image on image control in ASP.NET 2.0.

 

Place the Image control on the webform Default.aspx as shown below.

 

<tr>

     <td>

          <asp:image ToolTip = "ASP Image Control" ID="Image4" runat="server" ImageUrl ="ImageCSharp.aspx?ImageID=1" Height="156px" Width="174px"></asp:image>

     </td>

</tr>

 

The imageulr property of the Image control we can call webform ImageCSharp.aspx as shown above.

 

protected void Page_Load(object sender, EventArgs e)

    {

        if (Request.QueryString["ImageID"] != null)

        {

            

            OracleConnection con = new OracleConnection("Connection_String");

            OracleCommand cmd = new OracleCommand();

 

            int id = 0;

     

            OracleDataReader dr;

 

            cmd.Connection = con;

            cmd.CommandType = System.Data.CommandType.Text;

            cmd.CommandText = "select * from Signature where CLIENT_ID= " + id;

 

            con.Open();

            dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

 

            byte[] arrpicture = new byte[0];

           

 

            if (dr.Read())

            {

                arrpicture = (byte[])dr[2];

                               Response.Buffer = true;

                Response.Charset = "";

                Response.Cache.SetCacheability(HttpCacheability.NoCache);                    
                Response.AddHeader("content-disposition", "attachment;filename=" + dr[2].ToString());

                Response.BinaryWrite(arrpicture);

                Response.Flush();

                Response.End();

 

            }

            con.Close();

        }

    }

 
 

 

This will provide the image to Image control in Default.aspx page.

 

 

 

 

 

 
 

Up Next
    Ebook Download
    View all
    Learn
    View all