首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

ASP.NET的验证码源代码在winform中运行的一点小有关问题

2011-12-14 
ASP.NET的验证码源代码在winform中运行的一点小问题//画图片privatevoidCreateImage(stringcheckCode){Sys

ASP.NET的验证码源代码在winform中运行的一点小问题
//画图片
private   void   CreateImage(string   checkCode)
{        
          System.Drawing.Bitmap   image   =   new                   System.Drawing.Bitmap(checkCode.Length   *   12   +   10,   22);
                        Graphics   g   =   Graphics.FromImage(image);

                        //try
                        //{
                                //生成随机生成器  
                                Random   random   =   new   Random();

                                //清空图片背景色  
                                g.Clear(Color.White);

                                //画图片的背景噪音线  
                                for   (int   i   =   0;   i   <   25;   i++)
                                {
                                        int   x1   =   random.Next(image.Width);
                                        int   x2   =   random.Next(image.Width);
                                        int   y1   =   random.Next(image.Height);
                                        int   y2   =   random.Next(image.Height);

                                        g.DrawLine(new   Pen(Color.Silver),   x1,   y1,   x2,   y2);
                                }

                                Font   font   =   new   System.Drawing.Font( "Arial ",   12,   (System.Drawing.FontStyle.Bold   |   System.Drawing.FontStyle.Italic));
                                System.Drawing.Drawing2D.LinearGradientBrush   brush   =   new   System.Drawing.Drawing2D.LinearGradientBrush(new   Rectangle(0,   0,   image.Width,   image.Height),   Color.Blue,   Color.DarkRed,   1.2f,   true);
                                g.DrawString(checkCode,   font,   brush,   2,   2);



                                //画图片的前景噪音点  
                                for   (int   i   =   0;   i   <   100;   i++)
                                {
                                        int   x   =   random.Next(image.Width);
                                        int   y   =   random.Next(image.Height);

                                        image.SetPixel(x,   y,   Color.FromArgb(random.Next()));
                                }

                                //画图片的边框线  
                                g.DrawRectangle(new   Pen(Color.Silver),   0,   0,   image.Width   -   1,   image.Height   -   1);

                                System.IO.MemoryStream   ms   =   new   System.IO.MemoryStream();
                                image.Save(ms,   System.Drawing.Imaging.ImageFormat.Gif);
                               
                                //Response.ClearContent();
                                //Response.ContentType   =   "image/Gif ";
                                //Response.BinaryWrite(ms.ToArray());
                        //}
                        //finally
                        //{
                        //         g.Dispose();
                        //         image.Dispose();
                        //}
                  }

---------------------------
问题,这是ASP.NET的代码,在WINFROM中运行的时候,由于没有   Response对象的,要怎么改一下呢?
//Response.ClearContent();
//Response.ContentType   =   "image/Gif ";
//Response.BinaryWrite(ms.ToArray());

[解决办法]
你只要得到了System.IO.MemoryStream对象,直接把他转换成Image对象,放在winform的pictutebox中就可以了


[解决办法]
你要什么样的效果?可以显示到picturebox

picturebox1.image=image;

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
画图片的话要在某个控件上画,比如PictureBox或者Form,在它们的Paint事件中传Graphics给你的那个方法。
[解决办法]
Paint事件.楼上正解.

热点排行