Files
g.hnyhua.cn/Mtxfw.VipSite/mobile/tg2wm2.aspx.cs
2026-02-07 15:48:27 +08:00

243 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Net;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
using System.Drawing;
using System.Drawing.Imaging;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
namespace Mtxfw.VipSite
{
public partial class tg2wm2 : System.Web.UI.Page
{
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 strRealName = "", EWMPic = "", EWMPic2 = "", SiteTitle = "";
public static string appKey = "", signature = "", timestamp = "", nonceStr = "", strurl = "", IFApp = "0", IFLink = "1";
public int GetCount = 0, GetCount2 = 0, GetCount3 = 0;
public int gtype = 0;
protected void Page_Load(object sender, EventArgs e)
{
SiteTitle = config.webName;
string hostname = Request.Url.Host;
}
// 给图片加水印#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.Black);
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)
{
// 确定水印文字的字体大小
//int[] sizes = new int[] { 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
crFont = new Font("黑体", fontsize, FontStyle.Bold);
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.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
fg.DrawString(_watermarkText,
crFont, new SolidBrush(System.Drawing.Color.White),
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.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);
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;
}
}
}