aynehband
یک شنبه 18 دی 1390, 19:34 عصر
سلام
من یک captcha نوشتم ، که یک usercontrol یک هندلر را فراخوانی کنه که تصویر را تولید کنه، با querystring کار میکنه، با session نه.
این کد با session گیر میده. با httpcontext.current.user.session هم گیر میده.
ضمنا :D اون پروپرتی (_SecCode) را هم نمیشه بخونم. مقدارش همیشه خالیه. مجبورم با session چک کنم.
کد web user control
private string _SecCode ;
public string SecCode
{
get { return _SecCode; }
set { _SecCode = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this.PreRender+=new EventHandler(Controls_site_secimage_PreRender);
}
protected void Controls_site_secimage_PreRender(object sender, EventArgs e)
{
Button1_Click(null, null);
}
protected void Button1_Click(object sender, EventArgs e)
{
String ABC = "QWERTYUI89PZSXDCFGBJHN15687420";
string last_text="";
Random rand = new Random();
int i;
for (i = 0; i <= 3; i++)
{
last_text +=ABC.Substring( Convert.ToInt32(rand.Next(1,30)),1);
}
Session["seccode"] = last_text;
_SecCode = last_text;
Image1.ImageUrl = "ShowSecImage.ashx" ;
}
کد فایل ShowSecImage.ashx
public void ProcessRequest(HttpContext context)
{
string file="";
HttpResponse r = context.Response;
r.ContentType = "image/png";
file =context.Session["seccode"].ToString();
string filename = "back_Sec.png";
int height = 100;// Convert.ToInt32(context.Request["height"]);
int width = 100; //Convert.ToInt32(context.Request["width"]);
try
{
if ((context.Request["height"] != null) )
{
if( (Convert.ToInt32(context.Request["height"]) > 20) && (Convert.ToInt32(context.Request["height"])< 600))
height = Convert.ToInt32(context.Request["height"]);
else height = 50;
}
else{height = 50;}
if ((context.Request["width"] != null))
{if( (Convert.ToInt32(context.Request["width"]) > 20) && (Convert.ToInt32(context.Request["width"])< 800))
width = Convert.ToInt32(context.Request["width"]);
else width = 150;
}
else { width = 150; }
}
catch {
}
using (Image original = Image.FromFile(HttpContext.Current.Server.
MapPath( filename)))
{
using (Bitmap resized = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(resized))
{
Image logg=Image.FromFile( HttpContext.Current.Server.MapPath(System.Configur ation.ConfigurationManager.AppSettings["Paths_doc_image"].ToString() + "logo.png"));
Image FrameImage = Image.FromFile(HttpContext.Current.Server.MapPath( System.Configuration.ConfigurationManager.AppSetti ngs["Paths_doc_image"].ToString() + "frame.png"));
int width_border, heightborder;
width_border = Convert.ToInt32(width/14);
heightborder = Convert.ToInt32(height / 14);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(original, new Rectangle(new Point(0, 0), new Size(width, height)));
g.DrawString(file, new Font("tahoma", 25, FontStyle.Regular), Brushes.Black, new Point(0 , 0));
var resizedStream = new MemoryStream();
resized.Save(resizedStream, ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(resizedStream.GetBuff er());
context.Response.End();
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
من یک captcha نوشتم ، که یک usercontrol یک هندلر را فراخوانی کنه که تصویر را تولید کنه، با querystring کار میکنه، با session نه.
این کد با session گیر میده. با httpcontext.current.user.session هم گیر میده.
ضمنا :D اون پروپرتی (_SecCode) را هم نمیشه بخونم. مقدارش همیشه خالیه. مجبورم با session چک کنم.
کد web user control
private string _SecCode ;
public string SecCode
{
get { return _SecCode; }
set { _SecCode = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this.PreRender+=new EventHandler(Controls_site_secimage_PreRender);
}
protected void Controls_site_secimage_PreRender(object sender, EventArgs e)
{
Button1_Click(null, null);
}
protected void Button1_Click(object sender, EventArgs e)
{
String ABC = "QWERTYUI89PZSXDCFGBJHN15687420";
string last_text="";
Random rand = new Random();
int i;
for (i = 0; i <= 3; i++)
{
last_text +=ABC.Substring( Convert.ToInt32(rand.Next(1,30)),1);
}
Session["seccode"] = last_text;
_SecCode = last_text;
Image1.ImageUrl = "ShowSecImage.ashx" ;
}
کد فایل ShowSecImage.ashx
public void ProcessRequest(HttpContext context)
{
string file="";
HttpResponse r = context.Response;
r.ContentType = "image/png";
file =context.Session["seccode"].ToString();
string filename = "back_Sec.png";
int height = 100;// Convert.ToInt32(context.Request["height"]);
int width = 100; //Convert.ToInt32(context.Request["width"]);
try
{
if ((context.Request["height"] != null) )
{
if( (Convert.ToInt32(context.Request["height"]) > 20) && (Convert.ToInt32(context.Request["height"])< 600))
height = Convert.ToInt32(context.Request["height"]);
else height = 50;
}
else{height = 50;}
if ((context.Request["width"] != null))
{if( (Convert.ToInt32(context.Request["width"]) > 20) && (Convert.ToInt32(context.Request["width"])< 800))
width = Convert.ToInt32(context.Request["width"]);
else width = 150;
}
else { width = 150; }
}
catch {
}
using (Image original = Image.FromFile(HttpContext.Current.Server.
MapPath( filename)))
{
using (Bitmap resized = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(resized))
{
Image logg=Image.FromFile( HttpContext.Current.Server.MapPath(System.Configur ation.ConfigurationManager.AppSettings["Paths_doc_image"].ToString() + "logo.png"));
Image FrameImage = Image.FromFile(HttpContext.Current.Server.MapPath( System.Configuration.ConfigurationManager.AppSetti ngs["Paths_doc_image"].ToString() + "frame.png"));
int width_border, heightborder;
width_border = Convert.ToInt32(width/14);
heightborder = Convert.ToInt32(height / 14);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(original, new Rectangle(new Point(0, 0), new Size(width, height)));
g.DrawString(file, new Font("tahoma", 25, FontStyle.Regular), Brushes.Black, new Point(0 , 0));
var resizedStream = new MemoryStream();
resized.Save(resizedStream, ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(resizedStream.GetBuff er());
context.Response.End();
}
}
}
}
public bool IsReusable {
get {
return false;
}
}