/// <summary>
/// 图片水印,图片文字
/// </summary>
/// <param name="WorkingDirectory">图片目录</param>
/// <param name="Copyright">文字</param>
/// <param name="sourceImg">源图片</param>
/// <param name="logBmp">Logo图片</param>
/// <param name="FormatImg">合成后的图片名称</param>
private void drimg(string WorkingDirectory,string Copyright,string sourceImg,string logBmp,string FormatImg)
{
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(WorkingDirectory+sourceImg);
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(phWidth,phHeight,PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72,72);
Graphics grPhoto = Graphics.FromImage(bmPhoto); System.Drawing.Image imgWater = new Bitmap(WorkingDirectory + logBmp);
int wmWidth = imgWater.Width;
int wmHeight = imgWater.Height;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel); int[] sizes = new int[]{16,14,12,10,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("arial", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont); if((ushort)crSize.Width < (ushort)phWidth)
break;
}
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2); StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(153, 0, 0,0)); grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat); SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(153, 255, 255, 255)); grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat); Bitmap bmWater = new Bitmap(bmPhoto);
bmWater.SetResolution(
imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution); Graphics grWater =
Graphics.FromImage(bmWater);
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[][] 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 wmColorMatrix = new
ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10; grWater.DrawImage(imgWater,
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,
wmHeight),
0,
0,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes);
imgPhoto = bmWater;
grPhoto.Dispose();
grWater.Dispose();
imgPhoto.Save(WorkingDirectory + FormatImg);
imgPhoto.Dispose();
imgWater.Dispose();
}
/// 图片水印,图片文字
/// </summary>
/// <param name="WorkingDirectory">图片目录</param>
/// <param name="Copyright">文字</param>
/// <param name="sourceImg">源图片</param>
/// <param name="logBmp">Logo图片</param>
/// <param name="FormatImg">合成后的图片名称</param>
private void drimg(string WorkingDirectory,string Copyright,string sourceImg,string logBmp,string FormatImg)
{
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(WorkingDirectory+sourceImg);
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(phWidth,phHeight,PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72,72);
Graphics grPhoto = Graphics.FromImage(bmPhoto); System.Drawing.Image imgWater = new Bitmap(WorkingDirectory + logBmp);
int wmWidth = imgWater.Width;
int wmHeight = imgWater.Height;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel); int[] sizes = new int[]{16,14,12,10,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("arial", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont); if((ushort)crSize.Width < (ushort)phWidth)
break;
}
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2); StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(153, 0, 0,0)); grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat); SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(153, 255, 255, 255)); grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat); Bitmap bmWater = new Bitmap(bmPhoto);
bmWater.SetResolution(
imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution); Graphics grWater =
Graphics.FromImage(bmWater);
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[][] 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 wmColorMatrix = new
ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10; grWater.DrawImage(imgWater,
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,
wmHeight),
0,
0,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes);
imgPhoto = bmWater;
grPhoto.Dispose();
grWater.Dispose();
imgPhoto.Save(WorkingDirectory + FormatImg);
imgPhoto.Dispose();
imgWater.Dispose();
}
{
this.drimg(@"E:\MyProject\img\","Honsl.com","c1.jpg","logo.bmp","c1111.jpg");
Response.Write("<script language='javascript'>alert('ok')</script>");
Response.End();
}