C#图片加水印(Generate Watermark Picture)
C#图片加水印(Generate Watermark Picture)
扫一扫加作者微信
效果图如下:
C#源码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace CSFramework.BLL
{
public class WatermarkImage
{
/// <summary>
/// 在图片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源图片文件</param>
/// <param name="waterWords">需要添加到图片上的文字</param>
/// <param name="webName">网站名称</param>
/// <param name="alpha">透明度</param>
/// <returns></returns>
public string DrawWords(string sourcePicture, string waterWords, string webName, float alpha)
{
// 判断参数是否有效
if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0)
{
return sourcePicture;
}
string PicturePath = Path.GetDirectoryName(sourcePicture) "\\";
string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower();
// 判断文件是否存在,以及文件名是否正确
if (System.IO.File.Exists(sourcePicture) == false || (
fileExtension != ".gif" &&&
fileExtension != ".jpg" &
fileExtension != ".png"))
{
return sourcePicture;
}
//创建一个图片对象用来装载要被添加水印的图片
Image imgPhoto = Image.FromFile(sourcePicture);
//获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//建立一个bitmap,和我们需要加水印的图片一样大小
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Graphics:封装一个 GDI 绘图图面。
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//设置图形的品质
//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的图片
new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高
0, // X方向从0点开始描绘
0, // Y方向
phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
int m_alpha = Convert.ToInt32(256 * alpha);
SolidBrush alphaBrush = new SolidBrush(Color.FromArgb(m_alpha, 175, 175, 175)); //设置画笔透明度及颜色
DrawMainWords(imgPhoto, grPhoto, alphaBrush, waterWords);
DrawWebTitleWords(imgPhoto, grPhoto, alphaBrush, webName);
imgPhoto.Dispose();
imgPhoto = bmPhoto;//重新设置引用
grPhoto.Dispose();
// 目标图片名称及全路径
string targetImage = Path.GetDirectoryName(sourcePicture) "\\" DateTime.Now.ToString("yyyyMMddhhmmss") ".png";
//将grPhoto保存
imgPhoto.Save(targetImage, ImageFormat.Png);
imgPhoto.Dispose();
File.Delete(sourcePicture);//删除旧的图片
File.Move(targetImage, sourcePicture);//文件改名
return targetImage.Replace(PicturePath, "");
}
private void DrawWebTitleWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
{
Font font = new Font("黑体", 12, FontStyle.Bold);
SolidBrush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(0.3 * 256), 175, 175, 175)); //设置画笔透明度及颜色
g.DrawString(waterWords, font, brush, imgPhoto.Width / 2 - 30, 40); //绘制文字
}
private void DrawMainWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
{
//**********绘制倾斜文字**************
Font font = new Font("Verdana", 26, FontStyle.Bold);
SizeF size = g.MeasureString(waterWords, font);
Single posX = 10;
Single posY = Convert.ToSingle(imgPhoto.Height * 0.8); //高度位置80%处
g.TranslateTransform(posX, posY);
Matrix transform = g.Transform;
//float shearX = -0.230F; //右倾斜文字
float shearX = 0.50F; //左倾斜文字
float shearY = -0.50F;
transform.Shear(shearX, shearY);
g.Transform = transform;
g.DrawString(waterWords, font, alphaBrush, 0, 0); //绘制文字
}
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace CSFramework.BLL
{
public class WatermarkImage
{
/// <summary>
/// 在图片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源图片文件</param>
/// <param name="waterWords">需要添加到图片上的文字</param>
/// <param name="webName">网站名称</param>
/// <param name="alpha">透明度</param>
/// <returns></returns>
public string DrawWords(string sourcePicture, string waterWords, string webName, float alpha)
{
// 判断参数是否有效
if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0)
{
return sourcePicture;
}
string PicturePath = Path.GetDirectoryName(sourcePicture) "\\";
string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower();
// 判断文件是否存在,以及文件名是否正确
if (System.IO.File.Exists(sourcePicture) == false || (
fileExtension != ".gif" &&&
fileExtension != ".jpg" &
fileExtension != ".png"))
{
return sourcePicture;
}
//创建一个图片对象用来装载要被添加水印的图片
Image imgPhoto = Image.FromFile(sourcePicture);
//获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//建立一个bitmap,和我们需要加水印的图片一样大小
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
//SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Graphics:封装一个 GDI 绘图图面。
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//设置图形的品质
//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的图片
new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高
0, // X方向从0点开始描绘
0, // Y方向
phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
int m_alpha = Convert.ToInt32(256 * alpha);
SolidBrush alphaBrush = new SolidBrush(Color.FromArgb(m_alpha, 175, 175, 175)); //设置画笔透明度及颜色
DrawMainWords(imgPhoto, grPhoto, alphaBrush, waterWords);
DrawWebTitleWords(imgPhoto, grPhoto, alphaBrush, webName);
imgPhoto.Dispose();
imgPhoto = bmPhoto;//重新设置引用
grPhoto.Dispose();
// 目标图片名称及全路径
string targetImage = Path.GetDirectoryName(sourcePicture) "\\" DateTime.Now.ToString("yyyyMMddhhmmss") ".png";
//将grPhoto保存
imgPhoto.Save(targetImage, ImageFormat.Png);
imgPhoto.Dispose();
File.Delete(sourcePicture);//删除旧的图片
File.Move(targetImage, sourcePicture);//文件改名
return targetImage.Replace(PicturePath, "");
}
private void DrawWebTitleWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
{
Font font = new Font("黑体", 12, FontStyle.Bold);
SolidBrush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(0.3 * 256), 175, 175, 175)); //设置画笔透明度及颜色
g.DrawString(waterWords, font, brush, imgPhoto.Width / 2 - 30, 40); //绘制文字
}
private void DrawMainWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
{
//**********绘制倾斜文字**************
Font font = new Font("Verdana", 26, FontStyle.Bold);
SizeF size = g.MeasureString(waterWords, font);
Single posX = 10;
Single posY = Convert.ToSingle(imgPhoto.Height * 0.8); //高度位置80%处
g.TranslateTransform(posX, posY);
Matrix transform = g.Transform;
//float shearX = -0.230F; //右倾斜文字
float shearX = 0.50F; //左倾斜文字
float shearY = -0.50F;
transform.Shear(shearX, shearY);
g.Transform = transform;
g.DrawString(waterWords, font, alphaBrush, 0, 0); //绘制文字
}
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网