Chinaunix首页 | 论坛 | 博客
  • 博客访问: 735175
  • 博文数量: 769
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 4985
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 16:37
文章分类

全部博文(769)

文章存档

2011年(1)

2008年(768)

我的朋友

分类:

2008-10-15 16:39:27

  前几天看到老师展示了一个例子,是把你输入的文字转化成图片,觉得很有趣,所以今天我也做了一个小例子。在Doc下输入文字,然后会在你要求的目录下生成一张图片。当然字体颜色,图片大小都是可以自己设定的,代码比较少。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

public class Drawing
{   
    public void CreateImage(string name,string filePath)
    {
        int wid=400;
        int high=200;
        Font font=new Font("Arial",48,FontStyle.Bold);
        //绘笔颜色
        SolidBrush brush=new SolidBrush(Color.Black);
       
        Bitmap image=new Bitmap(wid,high);
        Graphics g=Graphics.FromImage(image);
        g.Clear(ColorTranslator.FromHtml("#f0f0f0"));
        RectangleF rect=new RectangleF(5,2,wid,high);
        //绘制图片
        g.DrawString(name,font,brush,rect);
        //保存图片
        image.Save(filePath,ImageFormat.Jpeg);
        //释放对象
        g.Dispose();
        image.Dispose();
    }
}
public class Program
{
    public static void Main()
    {
        Drawing dh=new Drawing();
        Console.WriteLine("输入你的名字:");
        string name=Console.ReadLine();
        dh.CreateImage(name,@"D:\test\c#\advanced\Name.jpg");       
    }
}

【责编:Luzi】

--------------------next---------------------

阅读(341) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~