当前位置: 移动技术网 > IT编程>开发语言>c# > C#自定义签名章实现方法

C#自定义签名章实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#自定义签名章实现方法。分享给大家供大家参考。具体实现方法如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;
namespace wfpapp
{
 public class drawcachet
 {
  /// <summary>
  /// 自定义椭圆形签名章
  /// </summary>
  /// <param name="width">宽度,画出的签名章只有这宽度的一半</param>
  /// <param name="height">高度,画出的签名章只有这高度的一半</param>
  /// <param name="test">签名章上名字</param>
  /// <param name="isrotate">签名章是否旋转角度</param>
  /// <param name="angle">旋转角度的大小</param>
  /// <returns></returns>
  public static bitmap getdrawcirclecachet(int width, int height, string test, bool isrotate, int angle)
  {
   //记录圆画笔的粗细 
   int circle_brush = 2;
   //画布
   bitmap bitmap = new bitmap(width, height);
   //定义字符串的样式 
   font var_font = new font("arial", 13, fontstyle.bold);
   //定义一个矩形 ,设置圆的绘制区 
   rectangle rect = new rectangle(10, 10, width / 2, height / 2);
   //实例化graphic类 
   graphics g = system.drawing.graphics.fromimage(bitmap);
   //消除绘制图形的锯齿,平滑
   g.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;
   //以白色清空panelcachet画布背景 
   g.clear(color.white);
   //设置画笔的颜色
   pen mypen = new pen(color.red, circle_brush);
   //绘制圆 
   g.drawellipse(mypen, rect);
   //设置文字的字体样式 
   font star_font = new font("arial", 12, fontstyle.regular);
   //对字符串进行宽度和长度测量 
   sizef var_size = g.measurestring(test, star_font);
   float circlezjw = rect.width + 2 * circle_brush;
   float circlezjh = rect.height + 2 * circle_brush;
   float x = (circlezjw - var_size.width) / 2f + rect.x;
   float y = (circlezjh - var_size.height) / 2f + rect.y;
   //在指定的位置绘制文字
   g.drawstring(test, star_font, mypen.brush, new pointf(x, y));
   if (isrotate)
    bitmap = rotate(bitmap, angle);
   return bitmap;
  }
  /// <summary>
  /// 自定义矩形签名章
  /// </summary>
  /// <param name="width">宽度,画出的签名章只有这宽度的一半</param>
  /// <param name="height">高度,画出的签名章只有这高度的一半</param>
  /// <param name="name">签名章上名字</param>
  /// <param name="isrotate">签名章是否旋转角度</param>
  /// <param name="angle">旋转角度的大小</param>
  /// <returns></returns>
  public static bitmap getdrawrectangle(int width, int height, string name, bool isrotate, int angle)
  {
   //记录圆画笔的粗细 
   int circle_brush = 2;
   //画布
   bitmap bitmap = new bitmap(width, height);
   //定义字符串的样式 
   font var_font = new font("arial", 13, fontstyle.bold);
   //定义一个矩形 ,设置圆的绘制区 
   rectangle rect = new rectangle(10, 10, width / 2, height / 2);
   //实例化graphic类 
   graphics g = system.drawing.graphics.fromimage(bitmap);
   //消除绘制图形的锯齿,平滑
   g.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;
   //以白色清空panelcachet画布背景 
   g.clear(color.white);
   //设置画笔的颜色
   pen mypen = new pen(color.red, circle_brush);
   //绘制圆 
   g.drawrectangle(mypen, rect);
   //设置文字的字体样式 
   font star_font = new font("arial", 12, fontstyle.regular);
   //对字符串进行宽度和长度测量 
   sizef var_size = g.measurestring(name, star_font);
   float circlezjw = rect.width + 2 * circle_brush;
   float circlezjh = rect.height + 2 * circle_brush;
   float x = (circlezjw - var_size.width) / 2f + rect.x;
   float y = (circlezjh - var_size.height) / 2f + rect.y;
   //在指定的位置绘制文字
   g.drawstring(name, star_font, mypen.brush, new pointf(x, y));
   if (isrotate)
    bitmap = rotate(bitmap, angle);
   return bitmap;
  }
  /// <summary>
  /// 签名章旋转
  /// </summary>
  /// <param name="b">bitmap图章</param>
  /// <param name="angle">旋转度</param>
  /// <returns></returns>
  static bitmap rotate(bitmap b, int angle)
  {
   angle = angle % 360;
   //弧度转换
   double radian = angle * math.pi / 180.0;
   double cos = math.cos(radian);
   double sin = math.sin(radian);
   //原图的宽和高
   int w = b.width;
   int h = b.height;
   int w = (int)(math.max(math.abs(w * cos - h * sin), math.abs(w * cos + h * sin)));
   int h = (int)(math.max(math.abs(w * sin - h * cos), math.abs(w * sin + h * cos)));
   //目标位图
   bitmap dsimage = new bitmap(w, h);
   system.drawing.graphics g = system.drawing.graphics.fromimage(dsimage);
   g.interpolationmode = system.drawing.drawing2d.interpolationmode.bilinear;
   g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   //计算偏移量
   point offset = new point((w - w) / 2, (h - h) / 2);
   //构造图像显示区域:让图像的中心与窗口的中心点一致
   rectangle rect = new rectangle(offset.x, offset.y, w, h);
   point center = new point(rect.x + rect.width / 2, rect.y + rect.height / 2);
   g.translatetransform(center.x, center.y);
   g.rotatetransform(360 - angle);
   //恢复图像在水平和垂直方向的平移
   g.translatetransform(-center.x, -center.y);
   g.drawimage(b, rect);
   //重至绘图的所有变换
   g.resettransform();
   g.save();
   g.dispose();
   //dsimage.save("yuancd.jpg", system.drawing.imaging.imageformat.jpeg);
   return dsimage;
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网