Monday, June 1, 2009

ASP.Net to generate and distribute Surface tags

Once the Microsoft Surface evolves into more real time applications, there may be so many occasions which demand an ASP.Net application to generate and distribute the Surface IdentityTags .For example when some one buys something related to surface in online the distributer can give him required surface tag through ASP.Net.
Let’s discuss how can we address that scenario.As discussed in one of my previous post there are 3 ways to generate or visualize Surface Identity tags.Among them 2 Methods are applicable here.One is using GenTag.exe utility and another is using the IdentityTagGenerator class.

Using GenTag.exe in ASP.Net
We can run this exe from our ASP.Net application.It will produce the tag image as a png file.Later we can send or distribute this image through our ASP.Net application.

Using IdentityTagGenerator class
I suggest this method which is based on the class IdentityTagGenerator.As you know, there are 2 methods for generating the tag.One is RenderTag and another is GenerateTag.We can use any of these methods in order to generate tags in ASP.Net

Basic idea of this is to have a page which accepts the series and value through it’s query string and generate the tag image according to that and returns the same though output stream.

GenerateTag method

private void ProcessRequest_Generate()
{
string series = Request.QueryString["series"];
string value = Request.QueryString["value"];
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(series))
{
System.Drawing.Image img=IdentityTagGenerator.GenerateTag(Convert.ToInt64(series),
Convert.ToInt64(value));
img.Save(Request.PhysicalApplicationPath+"\\"+ series+value+".png",ImageFormat.Png);
Response.Clear();
Response.TransmitFile("
~/"+series + value + ".png");
}
}


This code is written in a file called IdentityTag.aspx and this file is referred in the other aspx files, which need to display the tag image.How to use IdentityTag.aspx is explained below.

RenderTag method



private void ProcessRequest_Render()
{
string series = Request.QueryString["series"];
string value = Request.QueryString["value"];
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(series))
{
Bitmap bmp = new Bitmap(140, 140);
Graphics g = Graphics.FromImage(bmp);
IdentityTagGenerator.RenderTag(Convert.ToInt64(series),
Convert.ToInt64(value), new PointF(0, 0), g);

Response.Clear();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}


The logic is same as GenerateTag.But here I used the render tag method

IdentityTag.aspx

This is the file which generate the tag image and serve as jpg or png to the calling pages.This doesn’t have any aspx code in it.For more details how to write images directly into stream see my previous post.The whole IdentityTag.aspx.cs looks like below.




public partial class IdentityTag : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected override void Render(HtmlTextWriter writer)
{
ProcessRequest_Render();
}

private void ProcessRequest_Render()
{
string series = Request.QueryString["series"];
string value = Request.QueryString["value"];
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(series))
{
Bitmap bmp = new Bitmap(140, 140);
Graphics g = Graphics.FromImage(bmp);
IdentityTagGenerator.RenderTag(Convert.ToInt64(series),
Convert.ToInt64(value), new PointF(0, 0), g);

Response.Clear();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private void ProcessRequest_Generate()
{
string series = Request.QueryString["series"];
string value = Request.QueryString["value"];
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(series))
{
System.Drawing.Image img=IdentityTagGenerator.GenerateTag(Convert.ToInt64(series),
Convert.ToInt64(value));
img.Save(Request.PhysicalApplicationPath+"\\"+ series+value+".png",ImageFormat.Png);
Response.Clear();
Response.TransmitFile("
~/"+series + value + ".png");
}
}
public bool ThumbnailCallback()
{
return false;
}
}



Using IdentityTag.aspx in default.aspx


This contains 2 textboxes to get values for series and value which are used to generate the IdentityTag.The button here sets the url of the image to IdentityTag.aspx with corresponding query string parameters.




<form id="form1" runat="server">
<div style="width:100%;">
Series <asp:TextBox ID="tbSeries" runat="server"/> <br />
Value <asp:TextBox ID="tbValue" runat="server"/> <br />
<asp:Button ID="Button1" runat="server" Text="Show tag" onclick="Button1_Click" />
<asp:Button ID="btnDown" runat="server" Text="Download tag as image"
onclick="btnDown_Click" />
<br />
<asp:Image ID="imgTag" runat="server" Height="200" Width="200" />
</div>
</form>



protected void Button1_Click(object sender, EventArgs e)
{
imgTag.ImageUrl = string.Format("~/IdentityTag.aspx?series={0}&value={1}",
tbSeries.Text,tbValue.Text);
}

protected void btnDown_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("~/IdentityTag.aspx?series={0}&value={1}",
tbSeries.Text,tbValue.Text));

}


Sample here.

No comments: