250 lines
11 KiB
C#
250 lines
11 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Web;
|
||
using System.Web.SessionState;
|
||
using System.Net;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Drawing;
|
||
using System.Drawing.Imaging;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Linq;
|
||
using System.Collections.Specialized;
|
||
using System.Web.Script.Serialization;
|
||
namespace Mtxfw.VipSite
|
||
{
|
||
public partial class tg2wm : Mtxfw.Utility.CheckWeiXinBase
|
||
{
|
||
public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config("");
|
||
public Mtxfw.DAL.user_info daoUser = new Mtxfw.DAL.user_info();
|
||
public Mtxfw.DAL.user_Goods daoGoods = new Mtxfw.DAL.user_Goods();
|
||
public Mtxfw.DAL.Article daoArticle = new Mtxfw.DAL.Article();
|
||
public Mtxfw.Model.Article ModelArticle = new Mtxfw.Model.Article();
|
||
public string EWMPic = "", EWMPic2 = "", SiteTitle = "";
|
||
public static string appKey = "", signature = "", timestamp = "", nonceStr = "", strurl = "", IFApp = "0", IFLink = "1";
|
||
public int gtype = 0;
|
||
protected override void OnLoad(EventArgs e)
|
||
{
|
||
base.OnLoad(e);
|
||
SiteTitle = config.webName;
|
||
string hostname = Request.Url.Host;
|
||
|
||
if (!IsPostBack)
|
||
{
|
||
|
||
|
||
}
|
||
}
|
||
// 给图片加水印#region 给图片加水印
|
||
/// <summary>
|
||
/// 添加水印(分图片水印与文字水印两种)
|
||
/// </summary>
|
||
/// <param name="oldpath">原图片绝对地址</param>
|
||
/// <param name="newpath">新图片放置的绝对地址(不可与原图片名相同)</param>
|
||
/// <param name="wmtType">要添加的水印的类型</param>
|
||
/// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
|
||
/// 若要添加图片水印,此为图片的路径(图片水印暂不可用)</param>
|
||
public void addWaterMark(string oldpath, string newpath, int wmtType, string wmimgpath, string sWaterMarkContent, int fontsize, int _width, int _height, int xpos, int ypos)
|
||
{
|
||
|
||
Image image = Image.FromFile(oldpath);
|
||
Bitmap b = new Bitmap(image.Width, image.Height,
|
||
PixelFormat.Format24bppRgb);
|
||
Graphics g = Graphics.FromImage(b);
|
||
g.Clear(System.Drawing.Color.White);
|
||
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
||
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
|
||
g.DrawImage(image, 0, 0, image.Width, image.Height);
|
||
|
||
switch (wmtType)
|
||
{
|
||
/**/
|
||
case 0:
|
||
//图片水印
|
||
this.addWatermarkImage(g, wmimgpath, _width, _height, xpos, ypos);
|
||
break;
|
||
case 1:
|
||
//文字水印
|
||
this.addWatermarkText(g, sWaterMarkContent, xpos, ypos, fontsize);
|
||
break;
|
||
}
|
||
|
||
|
||
b.Save(newpath);
|
||
b.Dispose();
|
||
image.Dispose();
|
||
|
||
}
|
||
/**/
|
||
/// <summary>
|
||
/// 加水印文字
|
||
/// </summary>
|
||
/// <param name="picture">imge 对象</param>
|
||
/// <param name="_watermarkText">水印文字内容</param>
|
||
/// <param name="_watermarkPosition">水印位置</param>
|
||
/// <param name="_width">被加水印图片的宽</param>
|
||
/// <param name="_height">被加水印图片的高</param>
|
||
private void addWatermarkText(Graphics picture, string _watermarkText, int xpos, int ypos, int fontsize)
|
||
{
|
||
Font crFont = null;
|
||
SizeF crSize = new SizeF();
|
||
crFont = new Font("黑体", fontsize, FontStyle.Regular);
|
||
crSize = picture.MeasureString(_watermarkText, crFont);
|
||
// 生成水印图片(将文字写到图片中)
|
||
Bitmap floatBmp = new Bitmap((int)crSize.Width + 3,
|
||
(int)crSize.Height + 3, PixelFormat.Format64bppArgb);
|
||
Graphics fg = Graphics.FromImage(floatBmp);
|
||
|
||
PointF pt = new PointF(0, 0);
|
||
// 画阴影文字
|
||
/*Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
|
||
Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
|
||
fg.DrawString(_watermarkText, crFont, TransparentBrush0, pt.X, pt.Y + 1);
|
||
fg.DrawString(_watermarkText, crFont, TransparentBrush0, pt.X + 1, pt.Y);
|
||
fg.DrawString(_watermarkText, crFont, TransparentBrush1, pt.X + 1, pt.Y + 1);
|
||
fg.DrawString(_watermarkText, crFont, TransparentBrush1, pt.X, pt.Y + 2);
|
||
fg.DrawString(_watermarkText, crFont, TransparentBrush1, pt.X + 2, pt.Y);
|
||
TransparentBrush0.Dispose();
|
||
TransparentBrush1.Dispose();*/
|
||
// 画文字
|
||
fg.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;//加上这句可以去掉黑色阴影
|
||
fg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||
fg.DrawString(_watermarkText,
|
||
crFont, new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255)),
|
||
pt.X, pt.Y, StringFormat.GenericDefault);
|
||
// 保存刚才的操作
|
||
fg.Save();
|
||
fg.Dispose();
|
||
// floatBmp.Save("d://WebSite//DIGITALKM//ttt.jpg");
|
||
// 将水印图片加到原图中
|
||
this.addWatermarkImage(picture, new Bitmap(floatBmp), (int)crSize.Width + 3, (int)crSize.Height + 3, xpos, ypos);
|
||
}
|
||
/**/
|
||
/// <summary>
|
||
/// 加水印图片
|
||
/// </summary>
|
||
/// <param name="picture">imge 对象</param>
|
||
/// <param name="iTheImage">Image对象(以此图片为水印)</param>
|
||
/// <param name="_watermarkPosition">水印位置</param>
|
||
/// <param name="_width">被加水印图片的宽</param>
|
||
/// <param name="_height">被加水印图片的高</param>
|
||
private void addWatermarkImage(Graphics picture, Image iTheImage, int _width, int _height, int xpos, int ypos)
|
||
{
|
||
Image watermark = new Bitmap(iTheImage);
|
||
|
||
ImageAttributes imageAttributes = new ImageAttributes();
|
||
ColorMap colorMap = new ColorMap();
|
||
|
||
ColorMap[] remapTable = { colorMap };
|
||
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
|
||
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, 0.3f, 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 WatermarkWidth = _width;
|
||
int WatermarkHeight = _height;
|
||
/*int xpos = 0;
|
||
int ypos = 0;
|
||
|
||
|
||
switch (_watermarkPosition)
|
||
{
|
||
case "WM_TOP_LEFT":
|
||
xpos = 10;
|
||
ypos = 10;
|
||
break;
|
||
case "WM_TOP_RIGHT":
|
||
xpos = _width - WatermarkWidth - 10;
|
||
ypos = 10;
|
||
break;
|
||
case "WM_BOTTOM_RIGHT":
|
||
xpos = _width - WatermarkWidth - 10;
|
||
ypos = _height - WatermarkHeight - 10;
|
||
break;
|
||
case "WM_BOTTOM_LEFT":
|
||
xpos = 10;
|
||
ypos = _height - WatermarkHeight - 10;
|
||
break;
|
||
case "WM_M":
|
||
xpos = 0;
|
||
ypos = 20;
|
||
break;
|
||
case "WM_M2":
|
||
xpos = 430;
|
||
ypos = 100;
|
||
break;
|
||
}*/
|
||
picture.DrawImage(
|
||
watermark,
|
||
new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
|
||
0,
|
||
0,
|
||
watermark.Width,
|
||
watermark.Height,
|
||
GraphicsUnit.Pixel,
|
||
imageAttributes);
|
||
watermark.Dispose();
|
||
imageAttributes.Dispose();
|
||
}
|
||
/**/
|
||
/// <summary>
|
||
/// 加水印图片
|
||
/// </summary>
|
||
/// <param name="picture">imge 对象</param>
|
||
/// <param name="WaterMarkPicPath">水印图片的地址</param>
|
||
/// <param name="_watermarkPosition">水印位置</param>
|
||
/// <param name="_width">被加水印图片的宽</param>
|
||
/// <param name="_height">被加水印图片的高</param>
|
||
private void addWatermarkImage(Graphics picture, string WaterMarkPicPath, int _width, int _height, int xpos, int ypos)
|
||
{
|
||
Image watermark = new Bitmap(WaterMarkPicPath);
|
||
|
||
this.addWatermarkImage(picture, watermark, _width,
|
||
_height, xpos, ypos);
|
||
}
|
||
//生成缩略图#region 生成缩略图
|
||
/**/
|
||
/// <summary>
|
||
/// 保存图片
|
||
/// </summary>
|
||
/// <param name="image">Image 对象</param>
|
||
/// <param name="savePath">保存路径</param>
|
||
/// <param name="ici">指定格式的编解码参数</param>
|
||
private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
|
||
{
|
||
//设置 原图片 对象的 EncoderParameters 对象
|
||
EncoderParameters parameters = new EncoderParameters(1);
|
||
parameters.Param[0] = new EncoderParameter(
|
||
System.Drawing.Imaging.Encoder.Quality, ((long)90));
|
||
image.Save(savePath, ici, parameters);
|
||
parameters.Dispose();
|
||
}
|
||
/**/
|
||
/// <summary>
|
||
/// 获取图像编码解码器的所有相关信息
|
||
/// </summary>
|
||
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
|
||
/// <returns>返回图像编码解码器的所有相关信息</returns>
|
||
private ImageCodecInfo GetCodecInfo(string mimeType)
|
||
{
|
||
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
|
||
foreach (ImageCodecInfo ici in CodecInfo)
|
||
{
|
||
if (ici.MimeType == mimeType)
|
||
return ici;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
} |