C#自定義簽名章實(shí)現(xiàn)方法
更新時(shí)間:2015年08月22日 16:25:08 作者:我心依舊
這篇文章主要介紹了C#自定義簽名章實(shí)現(xiàn)方法,涉及C#圖形繪制的相關(guān)實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#自定義簽名章實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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">簽名章是否旋轉(zhuǎn)角度</param>
/// <param name="angle">旋轉(zhuǎn)角度的大小</param>
/// <returns></returns>
public static Bitmap GetDrawCircleCachet(int Width, int Height, string test, bool IsRotate, int angle)
{
//記錄圓畫筆的粗細(xì)
int Circle_Brush = 2;
//畫布
Bitmap bitmap = new Bitmap(Width, Height);
//定義字符串的樣式
Font var_Font = new Font("Arial", 13, FontStyle.Bold);
//定義一個(gè)矩形 ,設(shè)置圓的繪制區(qū)
Rectangle rect = new Rectangle(10, 10, Width / 2, Height / 2);
//實(shí)例化Graphic類
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//消除繪制圖形的鋸齒,平滑
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//以白色清空panelCachet畫布背景
g.Clear(Color.White);
//設(shè)置畫筆的顏色
Pen mypen = new Pen(Color.Red, Circle_Brush);
//繪制圓
g.DrawEllipse(mypen, rect);
//設(shè)置文字的字體樣式
Font star_Font = new Font("Arial", 12, FontStyle.Regular);
//對(duì)字符串進(jìn)行寬度和長(zhǎng)度測(cè)量
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">簽名章是否旋轉(zhuǎn)角度</param>
/// <param name="angle">旋轉(zhuǎn)角度的大小</param>
/// <returns></returns>
public static Bitmap GetDrawRectangle(int width, int height, string name, bool IsRotate, int angle)
{
//記錄圓畫筆的粗細(xì)
int Circle_Brush = 2;
//畫布
Bitmap bitmap = new Bitmap(width, height);
//定義字符串的樣式
Font var_Font = new Font("Arial", 13, FontStyle.Bold);
//定義一個(gè)矩形 ,設(shè)置圓的繪制區(qū)
Rectangle rect = new Rectangle(10, 10, width / 2, height / 2);
//實(shí)例化Graphic類
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//消除繪制圖形的鋸齒,平滑
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//以白色清空panelCachet畫布背景
g.Clear(Color.White);
//設(shè)置畫筆的顏色
Pen mypen = new Pen(Color.Red, Circle_Brush);
//繪制圓
g.DrawRectangle(mypen, rect);
//設(shè)置文字的字體樣式
Font star_Font = new Font("Arial", 12, FontStyle.Regular);
//對(duì)字符串進(jìn)行寬度和長(zhǎng)度測(cè)量
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>
/// 簽名章旋轉(zhuǎn)
/// </summary>
/// <param name="b">Bitmap圖章</param>
/// <param name="angle">旋轉(zhuǎn)度</param>
/// <returns></returns>
static Bitmap Rotate(Bitmap b, int angle)
{
angle = angle % 360;
//弧度轉(zhuǎn)換
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)));
//目標(biāo)位圖
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;
//計(jì)算偏移量
Point Offset = new Point((W - w) / 2, (H - h) / 2);
//構(gòu)造圖像顯示區(qū)域:讓圖像的中心與窗口的中心點(diǎn)一致
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);
//恢復(fù)圖像在水平和垂直方向的平移
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;
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
c# 調(diào)用.bat文件的實(shí)現(xiàn)代碼
c# 調(diào)用.bat文件主要利用了using System.Diagnostics;命名空間,大家可以參考下。2009-06-06
使用C#開發(fā)OPC?Server服務(wù)器源碼解析
OPC?Server服務(wù)器服務(wù)器的開發(fā)比較繁瑣,本示例采用C#提供了一種簡(jiǎn)單快速實(shí)現(xiàn)OPCServer的方法,已經(jīng)在工程項(xiàng)目中應(yīng)用,本文對(duì)C#開發(fā)OPC?Server服務(wù)器相關(guān)知識(shí)給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?
這篇文章主要介紹了C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開介紹,需要的朋友可以參考一下2022-08-08
c# 應(yīng)用事務(wù)的簡(jiǎn)單實(shí)例
這篇文章介紹了c# 應(yīng)用事務(wù)的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-09-09
Unity shader實(shí)現(xiàn)頂點(diǎn)動(dòng)畫波動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)頂點(diǎn)動(dòng)畫波動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

