首先在window Form 中添加 PictureBox控件,控件名称为:pictureBox1
其次在window form 中添加一个Button,命名为:button2
在该按钮的 private void button2_Click(object sender, EventArgs e)事件中添加如下代码
HttpWebResponse resp; string url = "http://www.my400800.cn/1.jpeg";//换成你自己的验证码生成地址 WebClient wc = new WebClient(); HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create((url)); hwr.Method = "GET"; resp = (HttpWebResponse)hwr.GetResponse(); Stream resStream = resp.GetResponseStream(); Bitmap sourcebm = new Bitmap(resStream); pictureBox2.Image = (Bitmap)sourcebm.Clone(); int iw= sourcebm.Width;//图片宽度 int ih = sourcebm.Height;//图片高度 //图片灰度处理 for (int i = 0; i < iw; i++) { for (int j = 0; j < ih; j++) { Color c = sourcebm.GetPixel(i, j); int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);//转换灰度的算法 sourcebm.SetPixel(i, j, Color.FromArgb(luma, luma, luma)); } } pictureBox1.Image = sourcebm;