前几天看到老师展示了一个例子,是把你输入的文字转化成图片,觉得很有趣,所以今天我也做了一个小例子。在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");
}
}
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");
}
}