C#添加水印
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
/// <summary>
///ImgWater 的摘要说明
/// </summary>
public class ImgWater
{
public ImgWater()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 图片水印
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="WaterImg">水印图片地址</param>
/// <param name="sImgPath">水印图片保存地址</param>
/// <param name="Alpha">水印透明度设置</param>
/// <param name="iScale">水印图片在原图上的显示比例</param>
/// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
public bool zzsImgWater(string ImgFile,string WaterImg,string sImgPath,
float Alpha, float iScale,int intDistance)
{
try
{
//装载图片
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
int imgWatermarkWidth = imgWatermark.Width;
int imgWatermarkHeight = imgWatermark.Height;
//计算水印图片尺寸
decimal aScale = Convert.ToDecimal(iScale);
decimal pScale = 0.05M;
decimal MinScale = aScale - pScale;
decimal MaxScale = aScale + pScale;
int imgWatermarkWidthNew = imgWatermarkWidth;
int imgWatermarkHeightNew = imgWatermarkHeight;
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
}
else
)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
else
)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
//将原图画出来
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(, );
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.Clear(Color.White);
gbmPhoto.DrawImage(
imgPhoto
, , , imgPhotoWidth, imgPhotoHeight)
,
,
, imgPhotoWidth
, imgPhotoHeight
, GraphicsUnit.Pixel
);
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics gWatermark = Graphics.FromImage(bmWatermark);
//指定高质量显示水印图片质量
gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
ImageAttributes imageAttributes = new ImageAttributes();
//设置两种颜色,达到合成效果
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(, , , );
colorMap.NewColor = Color.FromArgb(, , , );
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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, Alpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
//确定水印边距
int xPos = imgPhotoWidth - imgWatermarkWidthNew;
int yPos = imgPhotoHeight - imgWatermarkHeightNew;
;
;
if (xPos > intDistance)
xPosOfWm = xPos - intDistance;
else
xPosOfWm = xPos;
if (yPos > intDistance)
yPosOfWm = yPos - intDistance;
else
yPosOfWm = yPos;
gWatermark.DrawImage(
imgWatermark
, new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
,
,
, imgWatermarkWidth
, imgWatermarkHeight
, GraphicsUnit.Pixel
, imageAttributes
);
imgPhoto = bmWatermark;
//以jpg格式保存图片
imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//销毁对象
gbmPhoto.Dispose();
gWatermark.Dispose();
imgPhoto.Dispose();
imgWatermark.Dispose();
return true;
}
catch
{
return false;
}
}
/**//// <summary>
/// 文字水印
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="TextFont">水印文字</param>
/// <param name="sImgPath">文字水印图片保存地址</param>
/// <param name="hScale">高度位置</param>
/// <param name="widthFont">文字块在图片中所占宽度比例</param>
/// <param name="Alpha">文字透明度 其数值的范围在0到255</param>
public bool zzsTextWater(
string ImgFile
, string TextFont
, string sImgPath
, float hScale
, float widthFont
, int Alpha
)
{
try
{
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(, );
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(
imgPhoto
, , , imgPhotoWidth, imgPhotoHeight)
,
,
, imgPhotoWidth
, imgPhotoHeight
, GraphicsUnit.Pixel
);
//建立字体大小的数组,循环找出适合图片的水印字体
//int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
, , , , , , };
System.Drawing.Font crFont = null;
System.Drawing.SizeF crSize = new SizeF();
; i < ; i++)
{
crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
crSize = gbmPhoto.MeasureString(TextFont, crFont);
if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont)
break;
}
//设置水印字体的位置
//int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
//float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
//float xCenterOfImg = (imgPhotoWidth * 1 / 2);
float yPosFromBottom = imgPhotoHeight * 0.85f;
float xCenterOfImg = imgPhotoWidth * 0.8f;
//if (xCenterOfImg<crSize.Height)
// xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth));
System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
StrFormat.Alignment = System.Drawing.StringAlignment.Center;
//
System.Drawing.SolidBrush semiTransBrush2 = , , ));
gbmPhoto.DrawString(
TextFont
, crFont
, semiTransBrush2
, , yPosFromBottom + )
, StrFormat
);
System.Drawing.SolidBrush semiTransBrush = , , ));
//gbmPhoto.FillRectangle(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - crSize.Width / 2, yPosFromBottom - 4), crSize));
gbmPhoto.DrawString(
TextFont
, crFont
, semiTransBrush
, new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
, StrFormat
);
bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
gbmPhoto.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
return true;
}
catch(Exception ex)
{
return false;
}
}
/**//// <summary>
/// 文字和Logo图片水印
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="WaterImg">水印图片地址</param>
/// <param name="TextFont">水印文字信息</param>
/// <param name="sImgPath">生存水印图片后的保存地址</param>
/// <param name="ImgAlpha">水印图片的透明度</param>
/// <param name="imgiScale">水印图片在原图上的显示比例</param>
/// <param name="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
/// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param>
/// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param>
/// <param name="textAlpha">文字透明度 其数值的范围在0到255</param>
public void zzsImgTextWater(
string ImgFile
, string WaterImg
, string TextFont
, string sImgPath
, float ImgAlpha
, float imgiScale
, int intimgDistance
, float texthScale
, float textwidthFont
, int textAlpha
)
{
try
{
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(, );
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(
imgPhoto
, , , imgPhotoWidth, imgPhotoHeight)
,
,
, imgPhotoWidth
, imgPhotoHeight
, GraphicsUnit.Pixel
);
//建立字体大小的数组,循环找出适合图片的水印字体
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
System.Drawing.Font crFont = null;
System.Drawing.SizeF crSize = new SizeF();
; i < ; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
crSize = gbmPhoto.MeasureString(TextFont, crFont);
if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont)
break;
}
//设置水印字体的位置
int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
));
/ );
System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
StrFormat.Alignment = System.Drawing.StringAlignment.Center;
//
System.Drawing.SolidBrush semiTransBrush2 = , , ));
gbmPhoto.DrawString(
TextFont
, crFont
, semiTransBrush2
, , yPosFromBottom + )
, StrFormat
);
System.Drawing.SolidBrush semiTransBrush = , , ));
gbmPhoto.DrawString(
TextFont
, crFont
, semiTransBrush
, new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
, StrFormat
);
System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
int imgWatermarkWidth = imgWatermark.Width;
int imgWatermarkHeight = imgWatermark.Height;
//计算水印图片尺寸
decimal aScale = Convert.ToDecimal(imgiScale);
decimal pScale = 0.05M;
decimal MinScale = aScale - pScale;
decimal MaxScale = aScale + pScale;
int imgWatermarkWidthNew = imgWatermarkWidth;
int imgWatermarkHeightNew = imgWatermarkHeight;
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
}
else
)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
if (imgWatermarkWidth > imgWatermarkHeight)
)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
{
}
else
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
else
)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
{
}
else
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
}
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
{
imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
}
//将原图画出来
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics gWatermark = Graphics.FromImage(bmWatermark);
//指定高质量显示水印图片质量
gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
//设置两种颜色,达到合成效果
System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
colorMap.OldColor = Color.FromArgb(, , , );
colorMap.NewColor = Color.FromArgb(, , , );
System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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, ImgAlpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
//确定水印边距
int xPos = imgPhotoWidth - imgWatermarkWidthNew;
int yPos = imgPhotoHeight - imgWatermarkHeightNew;
;
;
if (xPos > intimgDistance)
xPosOfWm = xPos - intimgDistance;
else
xPosOfWm = xPos;
if (yPos > intimgDistance)
yPosOfWm = yPos - intimgDistance;
else
yPosOfWm = yPos;
gWatermark.DrawImage(
imgWatermark
, new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
,
,
, imgWatermarkWidth
, imgWatermarkHeight
, GraphicsUnit.Pixel
, imageAttributes
);
imgPhoto = bmWatermark;
//以jpg格式保存图片
imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//销毁对象
gbmPhoto.Dispose();
gWatermark.Dispose();
bmPhoto.Dispose();
imgPhoto.Dispose();
imgWatermark.Dispose();
}
catch
{
}
}
/**//// <summary>
/// 缩略图
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="sImgPath">缩略图保存地址</param>
/// <param name="ResizeWidth">缩略图宽度</param>
/// <param name="ResizeHeight">缩略图高度</param>
/// <param name="BgColor">缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等</param>
public bool zzsResizeImg(string ImgFile, string sImgPath, int ResizeWidth, int ResizeHeight, string BgColor)
{
try
{
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
int imgPhotoWidth = imgPhoto.Width;
int imgPhotoHeight = imgPhoto.Height;
;
;
;
;
if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
{
NewWidth = ResizeWidth;
NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), ));
startX = ;
StartY = (ResizeHeight - NewHeight) / ;
}
if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
{
NewHeight = ResizeHeight;
NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), ));
startX = (ResizeWidth - NewWidth) / ;
StartY = ;
}
if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
{
NewWidth = ResizeWidth;
NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), ));
startX = ;
StartY = (ResizeHeight - NewHeight) / ;
}
if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
{
NewWidth = imgPhotoWidth;
NewHeight = imgPhotoHeight;
startX = (ResizeWidth - imgPhotoWidth) / ;
StartY = (ResizeHeight - imgPhotoHeight) / ;
}
//计算缩放图片尺寸
Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(, );
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.Clear(Color.FromName(BgColor));
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(
imgPhoto
, new Rectangle(startX, StartY, NewWidth, NewHeight)
, , , imgPhotoWidth, imgPhotoHeight)
, GraphicsUnit.Pixel
);
bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();
ms.Close();
return true;
}
catch
{
return false;
}
}
/**//// <summary>
/// 图片剪切
/// </summary>
/// <param name="ImgFile">原图文件地址</param>
/// <param name="sImgPath">缩略图保存地址</param>
/// <param name="PointX">剪切起始点 X坐标</param>
/// <param name="PointY">剪切起始点 Y坐标</param>
/// <param name="CutWidth">剪切宽度</param>
/// <param name="CutHeight">剪切高度</param>
public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
{
FileStream fs = new FileStream(ImgFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
try
{
byte[] bytes = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
//此处只能用filestream,用 System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件
Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(, );
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gbmPhoto.DrawImage(
imgPhoto
, , , CutWidth, CutHeight)
, new Rectangle(PointX, PointY, CutHeight, CutHeight)
, GraphicsUnit.Pixel
);
bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();
ms.Close();
return true;
}
catch
{
return false;
}
finally
{
}
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ImgWater img = new ImgWater();
bool f = img.zzsTextWater(@"F:\ConsoleApplication1\LOGO.png",
"水印测试", @"F:\LOGO.png",15.00F,0.25F,100);
}
}
C#添加水印的更多相关文章
- PHP-生成缩略图和添加水印图-学习笔记
1.开始 在网站上传图片过程,经常用到缩略图功能.这里我自己写了一个图片处理的Image类,能生成缩略图,并且可以添加水印图. 2.如何生成缩略图 生成缩略图,关键的是如何计算缩放比率. 这里,我根据 ...
- C#中如何给Excel添加水印
我们知道Microsoft Excel并没有内置的功能直接给Excel表添加水印,但是其实我们可以用其他变通的方式来解决此问题,如通过添加页眉图片或艺术字的方法来模仿水印的外观.所以在这篇文章中,我将 ...
- C# 给word文档添加水印
和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...
- C# 给PDF文件添加水印
水印种类及功能介绍 PDF水印分为两种:文本水印和图片水印.文本水印一般被用在商业领域,提醒读者该文档是受版权保护的,其他人不能抄袭或者免费使用.除了这个特征,水印还可以用来标记这个文档 的一些基 ...
- C#基于两种需求向图片添加水印
使用场景 1.也就是大家经常用的,一般是图片的4个角落,基于横纵坐标来添加. 2.在图片内基于固定位置,文字始终居中.刚开始我基于第一种场景来根据水印汉字的长度来计算坐标,后来发现方法始终不可靠.现在 ...
- PHP 图片处理工具类(添加水印与生成缩略图)
=================ImageTool.class.php================= <?php class ImageTool { private $imagePath; ...
- ios 给图片添加水印
//第一种添加水印方法 -(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name{ NSString* mark = na ...
- Android 图片添加水印图片或者文字
给图片添加水印的基本思路都是载入原图,添加文字或者载入水印图片,保存图片这三个部分 添加水印图片: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- iOS 学习 - 15.添加水印
绘制到位图 下面利用位图图形上下文给一个图片添加水印,在下面的程序中我们首先创建上下文,然后在上下文中绘制图片.直线和文本,最后从当前位图上下文中取得最终形成的新图片显示到界面 - (void)vie ...
- c#图片添加水印
今天讲一个上传图片添加水印的方法,直接上代码吧 protected void Button1_Click(object sender, EventArgs e) { int loc ...
随机推荐
- 洛谷P3383 【模板】线性筛素数(Miller_Rabin)
题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...
- 使用Java语言开发微信公众平台(五)——被关注回复与关键词回复
在上一篇文章中,我们实现了文本消息的接收与响应.可以在用户发送任何内容的时候,回复一段固定的文字.本章节中,我们将对上一章节的代码进行适当的完善,同时实现[被关注回复与关键词回复]功能. 一.微信 ...
- WPF通用管理框架 项目客户端基础结构介绍
介绍 首先, 粗糙的展示一下目前的结构设计理念, 因为这几天一直在忙于工作, 所以跟进有些缓慢, 整体的设计是支持多种服务模式.目前只针对MSSQL做数据库接口, ORM选型则用的是微软的EF(PS: ...
- PatentTips - Enhancing the usability of virtual machines
BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...
- actionmode-ActionMode以及它的menu使用
下图左边效果为Context Menu右边效果为ActionMode. ActionMode 其实就是替换在actionbar的位置上显示的一个控件.它跟actionbar一样,也是一种导航作用.只不 ...
- Qt使用第三方库
简述 在 Qt 中经常会用到第三方库,例如:FFmpeg.OpenCV 等.第三方库的使用比较简单,只需要一些基本的配置就可以搞定,一起来看看吧! 简述 第三方库 源代码 库文件 目标目录 第三方库 ...
- STM32介绍以及与通常ARM的区别
ARM是英国的芯片设计公司,其最成功的莫过于32位嵌入式CPU核----ARM系列,最常用的是ARM7和ARM9,ARM公司主要提供IP核,就是CPU的内核结构,只包括最核心的部分,并不是完整的处理器 ...
- 将vue-cli 2.x的项目升级到3.x
尝试将vue-cli 2.x的项目升级到3.x,记录一下升级过程,和遇到的坑 1. 直接复制替换src文件夹 2. 安装项目需要的依赖 (可以将原来package.json dependencies下 ...
- BZOJ3160: 万径人踪灭(FFT,回文自动机)
BZOJ传送门: 解题思路: FFT在处理卷积时可以将自己与自己卷,在某一种字母上标1其他标0,做字符集次就好了. (回文就是直接对称可以联系偶函数定义理解,根据这个性质就可以将字符串反向实现字符串匹 ...
- C#之菜单控件、主窗体打开子窗体、GroupBox控件使用
一.背景 一年前有学习过C#,但没有在项目中去实际做APP,重新捡起来应用到项目中.我同事本来做好一个CANOPEN设备管理的界面,由于近期搜索了别人的开发的界面,我觉得有很多东西要重新安排,以及我已 ...