开发者

datatype image in $.ajax

in page 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();

=============================================================================开发者_如何转开发===== show dialog but does not show image? address cap.aspx is correct!

how get cap.aspx by $.ajax and datatype:image?


I think that the key here is to add the ContentType and the BufferOutput

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

Eg:

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜