C#利用原图和水印图的重叠简单实现水印的方法
作者:且行且思 发布时间:2023-04-21 01:41:24
标签:C#,水印
本文实例讲述了C#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:
图片操作类
/// <summary>
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxWidth">需要缩小到的宽度</param>
/// <param name="maxHeight">需要缩小到的高度</param>
/// <param name="imageOriginalWidth">图片的原始宽度</param>
/// <param name="imageOriginalHeight">图片的原始高度</param>
/// <returns>返回图片按等比例缩小后的实际大小</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
/// </summary>
/// <param name="originalImage">原始图片</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
/// <returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size.Empty;
System.Drawing.Image newImage = originalImage;
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
graphics = Graphics.FromImage(newImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
}
catch { }
finally
{
if (graphics != null)
{
graphics.Dispose();
graphics = null;
}
}
return newImage;
}
/// <summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图。
/// </summary>
/// <param name="originalImage">图片的物理文件地址</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
/// <returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image originalImage = null;
System.Drawing.Image newImage = null;
try
{
originalImage = System.Drawing.Image.FromFile(imageFile);
newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
}
catch { }
finally
{
if (originalImage != null)
{
originalImage.Dispose();
originalImage = null;
}
}
return newImage;
}
/// <summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
/// </summary>
/// <param name="originalImageFile">图片的物理文件地址</param>
/// <param name="thumbNailImageFile">缩略图的物理文件地址</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
{
System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
try
{
newImage.Save(thumbNailImageFile, ImageFormat.Jpeg);
}
catch
{ }
finally
{
newImage.Dispose();
newImage = null;
}
}
/// <summary>
/// 将一个图片的内存流调整为指定大小,并返回调整后的内存流。
/// </summary>
/// <param name="originalImageStream">原始图片的内存流</param>
/// <param name="newWidth">新图片的宽度</param>
/// <param name="newHeight">新图片的高度</param>
/// <returns>返回调整后的图片的内存流</returns>
public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight)
{
MemoryStream newImageStream = null;
System.Drawing.Image newImage = Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream), newWidth, newHeight);
if (newImage != null)
{
newImageStream = new MemoryStream();
newImage.Save(newImageStream, ImageFormat.Jpeg);
}
return newImageStream;
}
/// <summary>
/// 将一个内存流保存为磁盘文件。
/// </summary>
/// <param name="stream">内存流</param>
/// <param name="newFile">目标磁盘文件地址</param>
public static void SaveStreamToFile(Stream stream, string newFile)
{
if (stream == null || stream.Length == 0 || string.IsNullOrEmpty(newFile))
{
return;
}
byte[] buffer = new byte[stream.Length];
stream.Position = 0;
stream.Read(buffer, 0, buffer.Length);
FileStream fileStream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Flush();
fileStream.Close();
fileStream.Dispose();
}
/// <summary>
/// 对一个指定的图片加上图片水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterImage">水印图片(Image对象)</param>
public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
{
if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
{
return;
}
Graphics graphics = Graphics.FromImage(originalImage);
int x = originalImage.Width - waterImage.Width - 10;
int y = originalImage.Height - waterImage.Height - 10;
int width = waterImage.Width;
int height = waterImage.Height;
graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
graphics.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 对一个指定的图片加上文字水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterText">水印文字内容</param>
public static void CreateTextWaterMark(string imageFile, string waterText)
{
if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
Graphics graphics = Graphics.FromImage(originalImage);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);
float x = (float)originalImage.Width - waterTextSize.Width - 10F;
float y = (float)originalImage.Height - waterTextSize.Height - 10F;
graphics.DrawString(waterText, waterTextFont, brush, x, y);
graphics.Dispose();
brush.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 判断上传组件是否包含内容。
/// </summary>
/// <param name="fileUpload">ASP.NET 2.0标准上传组件</param>
/// <returns>如果数据有效,则返回True,否则返回False</returns>
public static bool IsAttachmentValid(FileUpload fileUpload)
{
if (fileUpload != null &&
fileUpload.PostedFile != null &&
!string.IsNullOrEmpty(fileUpload.PostedFile.FileName) &&
fileUpload.PostedFile.ContentLength > 0)
{
return true;
}
return false;
}
public class ImageHelper
{
#region " 水印存放的相对路径 "
public static string GetLogoPath()
{
return "/images/logo.png"; ///水印图路径
}
#endregion
#region " 图片水印 "
// <summary>
// 在图片上生成图片水印,此方法不支持Gif类型的图片
// </summary>
// <param name="Path">原服务器图片路径</param>
// <param name="Path_syp">生成的带图片水印的图片路径</param>
// <param name="Path_sypf">水印图片路径</param>
public static void MarkImage(Stream InUploadImagePath, string inLogoImagePath, string InSavePath)
{
System.Drawing.Image Image = System.Drawing.Image.FromStream(InUploadImagePath);
System.Drawing.Image newimage = Image.FromFile(Current.Server.MapPath(inLogoImagePath));
Graphics g = Graphics.FromImage(Image);
g.DrawImage(newimage, new Rectangle(Image.Width - newimage.Width, Image.Height - newimage.Height, newimage.Width, newimage.Height), 0, 0, newimage.Width, newimage.Height, GraphicsUnit.Pixel);
try {
Image.Save(Current.Server.MapPath(InSavePath));
}
catch (Exception ex) {
}
finally {
g.Dispose();
Image.Dispose();
newimage.Dispose();
}
}
#endregion
}
希望本文所述对大家C#程序设计有所帮助。


猜你喜欢
- ContentProvider是内容提供者,可以跨进程提供数据。大家都知道,ContentProvider的启动,是在Application
- 我们经常在项目开放中需要进行很多配置, 那么这些配置基本上都是动态的, 如果我直接写在代码中, 修改起来很麻烦, 如果该配置在多处进行引用啦
- 1、 题外话 相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getV
- 废话不多说,直接给大家贴C#代码了。/// <summary>/// 执行存储过程,返回" 返回值"///
- java Hibernate多对多映射前言:一、单向多对多 单向多对多的例子用人和职位来举例,一个人可以有多个职位
- 1. 项目背景最近参加了Compose挑战赛的终极挑战,使用Compose完成了一个天气app。之前几轮挑战也都有参与,每次都学到不少新东西
- 1.先定义一个Java对象Person:public class Person{ String name; int age; int num
- 之前看到过一个数字进度条,一直想写,今天就把这个实现下,想起来也是很简单的,先看下实现的效果:思路:绘制2根线 绘制进度条的文字,不断的改变
- 本文实例讲述了C#获取每个年,月,周的起始日期和结束日期的方法。分享给大家供大家参考,具体如下:我们在写程序的时候往往要计算出年,月,周的开
- Android 自动获取验证码的两种方式分别是BroadcastReceiver及ContentObserver,两种方式都需要进行注册、取
- 目录卡顿原理卡顿监控ANR原理卡顿原理主线程有耗时操作会导致卡顿,卡顿超过阀值,触发ANR。 应用进程启动时候,Zygote会反射调用Act
- string str="aaa|||bbb|||ccc"; string[] sArray=str.Split(new[
- 正文:相关术语翻译说明:Mark,标记;Sweep,清除;Compact,整理; 也有人翻译为压缩,译者认为GC时不存在压缩这回事。Copy
- 本文实例讲述了Android编程实现禁止系统锁屏与解锁亮屏的方法。分享给大家供大家参考,具体如下:需求:某个时刻任务执行完毕,关闭屏幕,某时
- 大家好,记得上次我带着大家一起实现了一个类似与淘宝客户端中带有的图片滚动播放器的效果,但是在做完了之后,发现忘了加入图片自动播放的功能(或许
- 首先我们来理解下 * 的机制。Android的事件处理机制有两种:监听和回调。A基于监听的事件处理主要涉及三类对象:EventSource(
- 本文实例为大家分享了java实现简单年龄计算器的具体代码,供大家参考,具体内容如下制作一个如下图年龄计算器根据题目,我做了一个由Calend
- java中如何表示圆周率设计一个Shape接口和它的两个实现类Square和Circle。 要求如下(1) Shape接口中有一个抽象方法a
- Android权限Android安全架构规定:默认情况下,任何应用都没有权限执行对其他应用、操作系统或用户有不利影响的任何操作。这包括读写用
- 前言列表是移动应用中用得最多的组件了,我们也会经常对列表元素进行增加或删除操作,最简单的方法是列表数据变动后,直接 setStat