$ .ajax中的数据类型图像

时间:2011-07-03 15:36:30

标签: asp.net jquery asp.net-ajax captcha

页面cap.aspx

中的

=============================================== ===========================

   string code = GetRandomText();

    Bitmap bitmap = new Bitmap(160,50,System.Drawing.Imaging.PixelFormat.Format32bppArgb);


    Graphics g = Graphics.FromImage(bitmap); 
    Pen pen = new Pen(Color.LavenderBlush); 
    Rectangle rect = new Rectangle(0,0,160,50);

    SolidBrush b = new SolidBrush(Color.LightPink);
    SolidBrush blue = new SolidBrush(Color.White);

    int counter = 0; 

    g.DrawRectangle(pen, rect);
    g.FillRectangle(b, rect);

    for (int i = 0; i < code.Length; i++)
    {
        g.DrawString(code[i].ToString(), new Font("Tahoma", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
        counter += 20;
    }

    DrawRandomLines(g); 

    bitmap.Save(Response.OutputStream,ImageFormat.Gif);

    g.Dispose();
    b.Dispose();
    blue.Dispose();
    bitmap.Dispose();

=============================================== ====================================

<div id="DIVdialog" style="display:none">
    <img src="cap.aspx"/>
</div>

=============================================== ====================================

$("#DIVdialog").dialog();

=============================================== =================================== 显示对话框但不显示图像?地址cap.aspx是正确的!

如何通过$ .ajax获取cap.aspx和数据类型:image?

1 个答案:

答案 0 :(得分:1)

我认为这里的关键是添加ContentType和BufferOutput

context.Response.ContentType = "image/gif";     
context.Response.BufferOutput = false;  

例如:

public void RenderIt(HttpContext context) 
{
    context.Response.ContentType = "image/gif";     
    context.Response.BufferOutput = false;

     string code = GetRandomText();

    using(Bitmap bitmap = new Bitmap(160,50,System.Drawing.Imaging.PixelFormat.Format32bppArgb))
    {
        using(Graphics g = Graphics.FromImage(bitmap))
        {
            using(Pen pen = new Pen(Color.LavenderBlush)
            {   
                using(SolidBrush b = new SolidBrush(Color.LightPink))
                {
                    using(SolidBrush blue = new SolidBrush(Color.White))
                    {

                    Rectangle rect = new Rectangle(0,0,160,50);

                    int counter = 0; 

                    g.DrawRectangle(pen, rect);
                    g.FillRectangle(b, rect);

                    for (int i = 0; i < code.Length; i++)
                    {
                        g.DrawString(code[i].ToString(), new Font("Tahoma", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
                        counter += 20;
                    }

                    DrawRandomLines(g); 

                    g.Dispose();
                    b.Dispose();
                    blue.Dispose();

                    // Return
                    bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);

                    // Dispose
                    bitmap.Dispose();
                    }
                }
            }
        }
    }

    context.Response.End();
}