创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_AddStringToImage_keleyi_com。
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace Keleyi.Com
{
public partial class KeleyiImageForm : Form
{
public KeleyiImageForm()
{
InitializeComponent();
}
//按钮点击事件 jihua.cnblogs.com
private void button_AddStringToImage_keleyi_com_Click(object sender, EventArgs e)
{
HttpWebRequest request_keleyi_com = (HttpWebRequest)WebRequest.Create(@"http://keleyi.com/image/a/q9e4nvb3.jpg");
WebResponse response_keleyi = null;
Stream keleyi_com_stream = null;
response_keleyi = request_keleyi_com.GetResponse();
keleyi_com_stream = response_keleyi.GetResponseStream();
Bitmap bmp = new Bitmap(keleyi_com_stream);
Graphics g = Graphics.FromImage(bmp);
String str = "柯乐义 keleyi.com";
Font font = new Font("宋体", 16);
SolidBrush sbrush = new SolidBrush(Color.Black);
g.DrawString(str, font, sbrush, new PointF(10, 30));
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
if (pictureBox_keleyi_com.Name == "pictureBox_ke"+"leyi_com")
pictureBox_keleyi_com.Image = Image.FromStream(ms);
}
}
}
程序实现在网上的图片(http://keleyi.com/image/a/q9e4nvb3.jpg)上加上字符串“柯乐义 keleyi.com”,在窗体上显示出来。