Wednesday, August 3, 2011

load image or captcha image using jquery and asp.net

Demo for show image or catptch image on button click using jquery........


<script src="jquery-1.6.1.min.js" type="text/javascript"></script>
<script language="javascript" >
$(document).ready(function () {
$("#btnload").click(function () {
$.get("data.ashx", function (data) {

$("#imgload").attr("src",data);
});

});
});
</script>

<img id="imgload" border="0" src="" /> <br />
<button id="btnload">Load Image</button>



<%@ WebHandler Language="C#" %>

using System;
using System.Drawing;
using System.Web;
using System.Drawing.Imaging;
using System.IO;
public class data : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Bitmap bmp = new Bitmap(context.Server.MapPath("download.jpg"));
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
bmp.Save(ms, ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
context.Response.Write("data:image/gif;base64," + base64String);
context.Response.End();
}
}

public bool IsReusable
{
get
{
return false;
}
}
}
Share: