常用工具类10-上传水印类

public class WaterMark
{
/// <summary>
/// 图片水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkFilename">水印文件相对路径</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
if (!File.Exists(Utils.GetMapPath(imgPath)))
return;
byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
filename = Utils.GetMapPath(filename);

if (watermarkFilename.StartsWith("/") == false)
watermarkFilename = "/" + watermarkFilename;
watermarkFilename = Utils.GetMapPath(watermarkFilename);
if (!File.Exists(watermarkFilename))
return;
Graphics g = Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Image watermark = new Bitmap(watermarkFilename);

if (watermark.Height >= img.Height || watermark.Width >= img.Width)
return;

ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();

colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

float transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
transparency = (watermarkTransparency / 10.0F);

float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

int xpos = 0;
int ypos = 0;

switch (watermarkStatus)
{
case 1:
xpos = (int)(img.Width * (float).01);
ypos = (int)(img.Height * (float).01);
break;
case 2:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).01);
break;
case 3:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)(img.Height * (float).01);
break;
case 4:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 9:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
}

g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
quality = 80;

qualityParam[0] = quality;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;

if (ici != null)
img.Save(filename, ici, encoderParams);
else
img.Save(filename);

g.Dispose();
img.Dispose();
watermark.Dispose();
imageAttributes.Dispose();
}

/// <summary>
/// 文字水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="fontname">字体</param>
/// <param name="fontsize">字体大小</param>
public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
{
byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
filename = Utils.GetMapPath(filename);

Graphics g = Graphics.FromImage(img);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = g.MeasureString(watermarkText, drawFont);

float xpos = 0;
float ypos = 0;

switch (watermarkStatus)
{
case 1:
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
case 2:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = (float)img.Height * (float).01;
break;
case 3:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = (float)img.Height * (float).01;
break;
case 4:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
}

g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
quality = 80;

qualityParam[0] = quality;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;

if (ici != null)
img.Save(filename, ici, encoderParams);
else
img.Save(filename);

g.Dispose();
img.Dispose();
}
}

时间: 2024-11-06 13:50:19

常用工具类10-上传水印类的相关文章

文件上传帮助类

using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls; using System.IO; namespace AIMSCommon { /// <summary> /// 文件上传帮助类 /// </summary> public class UploadHelper { /// <summary> /// 文件上传 /// &

PHP 上传文件类

<?php /** * * @name upload.class.php * 上传文件类 * @author Peter * @createtime 2014-11-20 10:30:29 * */ class upload{ public $max_size; //上传file大小 public $allow_types; //上传文件类型 public $file_name; //文件名 public $errmsg; //错误提示 public $uploaded; //后的文件名 pub

Uploader 文件上传器类

概述 客户端完全基于JavaScript的 浏览器文件上传器,不需要任何浏览器插件,但需要和jQuery框架协同工作,支持超大文件上传,其算法是将一个超大文件切片成N个数据块依次提交给服务 端处理,由服务端存储断点信息实现断点续传的功能:支持文件拖拽上传,直接将文件拖拽到页面元素上方即可自动上传(默认元素是body):服务端采用asp.net 4.0程序开发,包含有处理程序,提供程序和视图控件,当然也可以用java或者php实现服务端程序. 浏览器兼容:IE10+.火狐.谷歌.Opera.win

django-自定义文件上传存储类

文件储存API:https://yiyibooks.cn/xx/django_182/ref/files/storage.html 编写自定义存储系统:https://yiyibooks.cn/xx/django_182/howto/custom-file-storage.html 定义一个自定义的储存类步骤 1.你的自定义储存类必须是django.core.files.storage.Storage的子类 2.Django必须能够不带任何参数来实例化你的储存类.这意味着任何设置都应该从djan

使用工具实现文件上传

开发步骤: 先给大家介绍了要用到的两个类: (1)DIskFileItemFactory对象:用于设置缓存大小和缓存目录 (2)ServletFileUpload对象:来解析文件 1]导两个jar包 commons-fileupload-1.2.2.jar   右键Build Path  -->  add path commons-io-2.0.1.jar 右键Build Path  -->  add path 在E盘下新建一个文件夹为TestData,在文件夹中新建两个文件夹:FileTem

[C#]工具类—FTP上传下载

public class FtpHelper { /// <summary> /// ftp方式上传 /// </summary> public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword) { FileInfo fileInf = new FileInfo(filePath + "\\&quo

android基础知识---重写系统Crash处理类保存上传和完美退出程序的方法

当今市场上android的手机型号和版本太多要做到完全适配几乎是完全不可能的,那么怎么才能获取其他的玩家的出错的信息呢!这里我们就要重新定义系统的Crash处理类了. 首先我们我们新建一个CustomCrashHandler类 实现UncaughtExceptionHandler接口,重写回调方法void uncaughtException(Thread thread, Throwable ex) package com.example.admin.crashchuli; import andr

C# 通用上传文件类

1.Upfile.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upfile.aspx.cs" Inherits="Inc_Upfile" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or

php上传文件类

<?php class Upload { private $_max_size; private $_type_map; private $_allow_ext_list; private $_allow_mime_list; private $_upload_path; private $_prefix; private $_error;//当前的错误信息 public function getError() { return $this->_error; } public function

OGG运维优化脚本(十二)-信息同步类--信息上传

文件: upload.sh 路径:$HOME/ggscript/ggupload 功能:该脚本不会直接使用,为满足其他脚本进行信息上传而设计,在脚本内直接调用上传相应的文件信息 他会读取系统信息配置文件sysinfo内的系统配置信息 范例 [detest#] Ip-MTMyLjEyMS4xMDEuODYK UserName-Z2dzCg== PassWord-Z2dzxxxxxx Port-MjIK path-L2RhdGExL2dncy9nZ3NlcnZlci9kaXJkZWYK [#dete