图片上实现签名 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//画布
private Bitmap bu;
//画笔
private Graphics pen;
//字体
private Font f;
//刷子 线性渐变
private LinearGradientBrush b;
//点
private Point p;
private int i = 1;
public Form1()
{
InitializeComponent();
//适合
//bu = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
////透明
////bu.MakeTransparent();
//pen = Graphics.FromImage(bu);
//看看有多少种字体 哪个好看
//FontDialog a=new FontDialog();
//a.ShowDialog();
//设置字体 大小 样式加粗 倾斜
//System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
//privateFonts.AddFontFile("Font/1.ttf"); //D:/aa/abc.ttf
// f = new Font(privateFonts.Families[0], 30);
//f = new Font("@方正舒体", 30, FontStyle.Bold | FontStyle.Italic);
//设置刷子
//b = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.Black, Color.Black);
}
private void huatu()
{
//得到输入的字符
string s = this.textBox1.Text;
if (s.Length == 0)
return;
int n = s.Length;
for (int i = 0; i < n; i )
{
//设置每个字符的位置 占70 字体大小是60
p = new Point(30 * i, 0);
//字符string 字体font 刷子brush 坐标point
pen.DrawString(s[i].ToString(), f, b, p);
}
}
/// <summary>
/// 显示随输入改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox1_TextChanged(object sender, EventArgs e)
{
pen.Clear(Color.White);
huatu();
this.pictureBox1.Image = bu;
}
/// <summary>
/// 存储图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click_1(object sender, EventArgs e)
{
System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
Random r = new Random();
int fnum = 0;
//FileInfo info = new FileInfo(Application.StartupPath "Font");
foreach (string d in Directory.GetFiles(Application.StartupPath "/Font"))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Extension == ".ttf")
{
fnum ;
}
}
}
privateFonts.AddFontFile(Application.StartupPath "/Font/" r.Next(1, fnum) ".ttf");
//D:/aa/abc.ttf
Font f = new Font(privateFonts.Families[0], 40);
//获取文本
string text = this.textBox1.Text;
this.textBox1.Font = f;
//得到Bitmap(传入Rectangle.Empty自动计算宽高)
//Bitmap bmp = TextToBitmap(text, f, Rectangle.Empty, this.textBox1.ForeColor, this.textBox1.BackColor);
//this.pictureBox2.Image = bmp;
Graphics g = Graphics.FromImage(pictureBox1.Image);
SolidBrush mybrush;
mybrush = new SolidBrush(Color.Black);
g.DrawString(textBox1.Text, f, mybrush, new Rectangle((int)pictureBox1.Image.PhysicalDimension.Width - 300, (int)pictureBox1.Image.PhysicalDimension.Height - 400, 600, 600));
pictureBox1.Refresh();
//g1.DrawImage(pictureBox1.Image, 50, 50);
////保存到桌面save.jpg
//string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
pictureBox1.Image.Save(Application.StartupPath "\\Image\\save.jpg", ImageFormat.Jpeg);
}
/// <summary>
/// 把文字转换才Bitmap
/// </summary>
/// <param name="text"></param>
/// <param name="font"></param>
/// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
/// <param name="fontcolor">字体颜色</param>
/// <param name="backColor">背景颜色</param>
/// <returns></returns>
private Bitmap TextToBitmap(string text, Font font, Rectangle rect, Color fontcolor, Color backColor)
{
Graphics g;
Bitmap bmp;
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
if (rect == Rectangle.Empty)
{
bmp = new Bitmap(1, 1);
g = Graphics.FromImage(bmp);
//计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
int width = (int)(sizef.Width 1);
int height = (int)(sizef.Height 1);
rect = new Rectangle(0, 0, width, height);
bmp.Dispose();
bmp = new Bitmap(width, height);
}
else
{
bmp = new Bitmap(rect.Width, rect.Height);
}
g = Graphics.FromImage(bmp);
//使用ClearType字体功能
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.FillRectangle(new SolidBrush(backColor), rect);
g.DrawString(text, font, Brushes.Black, rect, format);
return bmp;
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "图片文件|*.jpg|BMP图片|*.bmp|Gif图片|*.gif";
if (this.openFileDialog1.ShowDialog() == DialogResult.Cancel) return;
var filePath = openFileDialog1.FileName;
string fileType = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(fileType)) return;
Bitmap b = new Bitmap(filePath);
this.pictureBox1.Image = b;
}
private void button3_Click(object sender, EventArgs e)
{
Bitmap bmp = TextToBitmap(textBox1.Text, this.textBox1.Font, Rectangle.Empty, this.textBox1.ForeColor, this.textBox1.BackColor);
//用PictureBox显示
this.pictureBox1.Image = bmp;
bmp.Save(Application.StartupPath "\\save.jpg", ImageFormat.Jpeg);
}
}
}
评论