问题描述
- 求一个C#生成印章的代码
-
我在做一个模拟真实印章效果的小程序,需要能看起来类似真实的印章效果。哪位有这方面的代码求赏赐。
要求:1、印章的颜色要跟真实印章效果类似,能模拟真实印章的颜色不均匀。
2、背景要能透明,即要像真实的盖在纸上的印章,能看到底下的文字。
解决方案
很简单,创建一个带透明颜色的位图,画上圆圈、文字和五角星,颜色这个不好说,不同的显示器,不同人的判断都不同,你选一个差不多就可以。然后随机将某些地方设置为透明色,模仿不均匀的效果
最后将这个图层和你的背景文档合成下,用drawimage绘图。
解决方案二:
估计网上生成的电子印章,都不能满足你的要求。
特别是颜色要跟真实印章效果类似!可行方法就是实物拍照,获取印章的底色。然后再完成其它元素增加到底色上的过程。
解决方案三:
页面:
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void uploadFileBtn_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string contentType = FileUpload1.PostedFile.ContentType;
if (contentType.StartsWith("image/"))
{
string filePath = Server.MapPath(string.Format("~/{0}", FileUpload1.PostedFile.FileName));
///保存文件
FileUpload1.SaveAs(filePath);
///加水印
this.AddWatermark(filePath, this.watermarkTextBox.Text);
image.ImageUrl = string.Format("~/{0}", FileUpload1.PostedFile.FileName);
}
}
}
/**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/// <summary>
/// 添加水印
/// </summary>
/// <param name="filePath"></param>
/// <param name="watermaterText"></param>
private void AddWatermark(string filePath, string watermaterText)
{
using (TextWatermarkPainter painter = new TextWatermarkPainter(filePath, 80, watermaterText))
{
painter.FontColor = System.Drawing.Color.Gray;
painter.PaintWaterMark();
}
}
}
解决方案四:
写文字水印
using System.Drawing;
/// <summary>
/// 添加文字水印
/// </summary>
public sealed class TextWatermarkPainter : WatermarkPainter
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="rawImagePath"></param>
/// <param name="alpha"></param>
/// <param name="text"></param>
public TextWatermarkPainter(string rawImagePath, int alpha, string text)
: base(rawImagePath, alpha)
{
WatermarkText = text;
}
private Font m_Font;
/// <summary>
/// 水印文字的字体
/// </summary>
public Font Font
{
get
{
if (null == m_Font)
m_Font = new Font("Arial Black", 30, FontStyle.Bold, GraphicsUnit.Pixel);
return m_Font;
}
set { m_Font = value; }
}
private Color m_FontColor;
/// <summary>
/// 字体颜色
/// </summary>
public Color FontColor
{
get
{
if (m_FontColor == null)
m_FontColor = Color.DarkSeaGreen;
return m_FontColor;
}
set { m_FontColor = value; }
}
private string m_WatermarkText;
/// <summary>
/// 水印的文字
/// </summary>
public string WatermarkText
{
get { return m_WatermarkText; }
set { m_WatermarkText = value; }
}
/// <summary>
/// 实现添加水印的方法
/// </summary>
/// <param name="graphics"></param>
/// <param name="newImage"></param>
/// <returns></returns>
protected override bool AddWatermark(Graphics graphics, Bitmap newImage)
{
if (string.IsNullOrEmpty(WatermarkText))
return true;
using (SolidBrush brush = new SolidBrush(Color.FromArgb(WatermarkAlpha, FontColor.R, FontColor.G, FontColor.B)))
{
Font font;
///添加文字位置
PointF f = new PointF();
float totalWidth = Font.Size * WatermarkText.Length;
f.X = newImage.Width - totalWidth - 100;
f.Y = newImage.Height - Font.Height;
if (f.X < 0)
{
///自动调整字体的大小
float width = newImage.Width / WatermarkText.Length;
font = new Font(Font.FontFamily, width, GraphicsUnit.Pixel);
}
else
font = Font;
graphics.DrawString(WatermarkText, font, brush, f);
}
return true;
}
#region IDisposable 成员
public override void Dispose()
{
Font.Dispose();
base.Dispose();
}
#endregion
}
解决方案五:
图标水印
using System;
using System.Drawing;
using System.IO;
/// <summary>
/// 图片水印绘制器
/// </summary>
public abstract class WatermarkPainter : IDisposable
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="rawImagePath"></param>
/// <param name="alpha"></param>
/// <param name="replaceRawImage"></param>
protected WatermarkPainter(string rawImagePath, int alpha, bool replaceRawImage)
{
ImageToBePainted = rawImagePath;
WatermarkAlpha = alpha;
ReplaceRawImage = replaceRawImage;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="rawImagePath"></param>
/// <param name="alpha"></param>
protected WatermarkPainter(string rawImagePath, int alpha)
: this(rawImagePath, alpha, true)
{ }
/// <summary>
/// 为临时存储文件添加的后缀
/// </summary>
private string m_TempFileSuffix = ".juice.watermark.temp";
/// <summary>
/// 水印的透明度
/// </summary>
private int m_WatermarkAlpha;
/// <summary>
/// 水印透明度
/// </summary>
public int WatermarkAlpha
{
get
{
if (m_WatermarkAlpha > 255)
m_WatermarkAlpha = 255;
else if (m_WatermarkAlpha < 0)
m_WatermarkAlpha = 0;
return m_WatermarkAlpha;
}
set { m_WatermarkAlpha = value; }
}
private string m_ImageToBePainted;
/// <summary>
/// 添加水印的图片的物理路径
/// </summary>
public string ImageToBePainted
{
get { return m_ImageToBePainted; }
set { m_ImageToBePainted = value; }
}
private bool m_ReplaceRawImage;
/// <summary>
/// 是否用加了水印的图片替换原有的图片
/// </summary>
public bool ReplaceRawImage
{
get { return m_ReplaceRawImage; }
set { m_ReplaceRawImage = value; }
}
private string m_TempImagePath;
/// <summary>
/// 加了水印后的临时储存名称
/// </summary>
protected string TempImagePath
{
get
{
if (string.IsNullOrEmpty(this.m_TempImagePath))
{
System.Text.StringBuilder s = new System.Text.StringBuilder(ImageToBePainted);
s.Append(this.m_TempFileSuffix);
this.m_TempImagePath = s.ToString();
}
return m_TempImagePath;
}
set { m_TempImagePath = value; }
}
/// <summary>
/// 原始图像
/// </summary>
private Image m_RawImage;
/// <summary>
/// 为图片添加水印
/// </summary>
public void PaintWaterMark()
{
if (string.IsNullOrEmpty(ImageToBePainted))
return;
if (!File.Exists(ImageToBePainted))
throw new FileNotFoundException(string.Format("file {0} not found.", ImageToBePainted));
bool succeed;
if (m_RawImage == null)
m_RawImage = Image.FromFile(ImageToBePainted);
///原始的图片
using (Bitmap newImage = new Bitmap(m_RawImage.Width, m_RawImage.Height))
{
///创建绘制图片的图面
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(m_RawImage, 0, 0, m_RawImage.Width, m_RawImage.Height);
///开始绘制水印
succeed = AddWatermark(g, newImage);
}
if (succeed)
///将图片保存到临时的文件
newImage.Save(TempImagePath);
if (m_RawImage != null)
m_RawImage.Dispose();
}
if (succeed && ReplaceRawImage)
{
//删除原来的文件
if (File.Exists(ImageToBePainted))
File.Delete(ImageToBePainted);
FileInfo info = new FileInfo(TempImagePath);
if (info.Exists)
{
info.MoveTo(ImageToBePainted);
}
}
}
/// <summary>
/// 添加水印,继承类可以用来添加自己的水印
/// </summary>
/// <param name="graphics"></param>
/// <param name="newImage"></param>
/// <returns></returns>
protected abstract bool AddWatermark(Graphics graphics, Bitmap newImage);
#region IDisposable 成员
public virtual void Dispose()
{
}
#endregion
}
时间: 2024-11-02 01:21:26