C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#自定义签名章

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

作者:我心依旧

这篇文章主要介绍了C#自定义签名章实现方法,涉及C#图形绘制的相关实现技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

您可能感兴趣的文章:
阅读全文