62,268
社区成员
发帖
与我相关
我的任务
分享
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (file != null)
{
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);//这里是保存在服务器上,我想就在这句之后给图片加水印
string webFilePath = uploadPath + file.FileName;
string webFilePath_sy = uploadPath + file.FileName;
AddWater(webFilePath, webFilePath_sy);
context.Response.Write("1");
}
else
{
context.Response.Write("0");
}
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
public static void AddWater(string Path, string Path_sy)
{
string addText = "文字水印";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
g.DrawString(addText, f, b, 35, 35);
g.Dispose();
image.Save(Path_sy);
image.Dispose();
}
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
FileStream m_fsSource = File.OpenRead(sourcePicture);
System.Drawing.Image m_imgSource = System.Drawing.Image.FromStream(m_fsSource, true);
m_fsSource.Close();
/// <summary>
/// 截取右边字符串
/// </summary>
/// <param name="value">截取字符串</param>
/// <param name="length">截取长度</param>
/// <returns>截取右边字符串</returns>
public static string Right(string value, int length)
{
if (length <= 0 || length > value.Length)
{
return value;
}
else
{
return value.Substring(value.Length - length);
}
}
/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="sourcePicture">源图片(绝对路径)</param>
/// <param name="targetPicture">生成图片(绝对路径)</param>
/// <param name="watermarkWords">水印文字</param>
/// <param name="fontName">水印字体类型</param>
/// <param name="fontSize">水印字体大小</param>
/// <param name="fontColor">水印字体颜色</param>
/// <param name="fontBond">水印文字是否加粗</param>
/// <param name="watermarkAlpha">水印文字透明度(0.1-1.0数值越小透明度越高)</param>
/// <param name="watermarkPosition">水印图片位置(从1-9,1:左上;2:中上;3:右上;4:左中;5:正中;6:右中;7:左下;8:中下;9:右下)</param>
public static void MakeWordWatemark(string sourcePicture, string targetPicture, string watermarkWords, string fontName, int fontSize, string fontColor, Boolean fontBond, float watermarkAlpha, int watermarkPosition)
{
Color m_colorFontColor = new Color();
try
{
m_colorFontColor = ColorTranslator.FromHtml(fontColor);
}
catch
{
return;
}
if (sourcePicture == string.Empty || targetPicture == string.Empty || watermarkWords == string.Empty || watermarkAlpha <= 0.0 || watermarkAlpha > 1.0 || watermarkPosition < 1 || watermarkPosition > 9)
return;
string m_sSourceExtension = Path.GetExtension(sourcePicture).ToLower();
if (File.Exists(sourcePicture) == false || (m_sSourceExtension != ".gif" && m_sSourceExtension != ".jpg" && m_sSourceExtension != ".png" && m_sSourceExtension != ".bmp"))
return;
FileStream m_fsSource = File.OpenRead(sourcePicture);
System.Drawing.Image m_imgSource = System.Drawing.Image.FromStream(m_fsSource, true);
m_fsSource.Close();
int m_iSourceWidth = m_imgSource.Width;
int m_iSourceHeight = m_imgSource.Height;
Bitmap m_bmSource = new Bitmap(m_imgSource, m_iSourceWidth, m_iSourceHeight);
m_imgSource.Dispose();
m_bmSource.SetResolution(1000f, 1000f);
Graphics m_grSource = Graphics.FromImage(m_bmSource);
m_grSource.SmoothingMode = SmoothingMode.AntiAlias;
m_grSource.DrawImage(m_bmSource, new Rectangle(0, 0, m_iSourceWidth, m_iSourceHeight), 0, 0, m_iSourceWidth, m_iSourceHeight, GraphicsUnit.Pixel);
Font m_fontWords = null;
if (fontBond)
m_fontWords = new Font(fontName, (float)fontSize, FontStyle.Bold);
else
m_fontWords = new Font(fontName, (float)fontSize, FontStyle.Regular);
int m_iWordWidth = (int)(Encoding.Default.GetByteCount(watermarkWords) / 2 * fontSize * 1.6 + 6);
int m_iWordHeight = (int)(fontSize * 1.5 + 2);
System.Drawing.Image m_imgWord = new Bitmap(m_iWordWidth, m_iWordHeight);
System.Drawing.Graphics m_grWord = System.Drawing.Graphics.FromImage(m_imgWord);
SizeF m_sfWord = new SizeF();
m_sfWord = m_grWord.MeasureString(watermarkWords, m_fontWords);
m_imgWord.Dispose();
m_grWord.Dispose();
float m_fWatermarkX;
float m_fWatermarkY;
switch (watermarkPosition)
{
case 1:
m_fWatermarkX = 10;
m_fWatermarkY = 10;
break;
case 2:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = 10;
break;
case 3:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = 10;
break;
case 4:
m_fWatermarkX = 10;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 5:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 6:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 7:
m_fWatermarkX = 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
case 8:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
case 9:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
default:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
}
int m_iWatermarkAlpha = Convert.ToInt32(255 * watermarkAlpha);
SolidBrush m_sbTransBrush = new SolidBrush(Color.FromArgb(m_iWatermarkAlpha, m_colorFontColor));
m_imgWord = new Bitmap((int)m_sfWord.Width + 2, (int)m_sfWord.Height + 2);
m_grWord = Graphics.FromImage(m_imgWord);
m_grWord.DrawString(watermarkWords, m_fontWords, m_sbTransBrush, new PointF(0, 0));
ImageAttributes m_imgAttributes = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red红色
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色
new float[] {0.0f, 0.0f, 0.0f, watermarkAlpha, 0.0f}, //透明度
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//
ColorMatrix m_wmColorMatrix = new ColorMatrix(colorMatrixElements);
m_imgAttributes.SetColorMatrix(m_wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
m_grSource.DrawImage(m_imgWord, new Rectangle((int)m_fWatermarkX, (int)m_fWatermarkY, m_iWordWidth, m_iWordHeight), 0, 0, m_iWordWidth, m_iWordHeight, GraphicsUnit.Pixel);
m_grSource.Dispose();
ImageFormat m_imgformatTarget = ImageFormat.Jpeg;
switch (Right(targetPicture, 4).ToLower())
{
case ".gif":
m_imgformatTarget = ImageFormat.Gif; break;
case ".jpg":
m_imgformatTarget = ImageFormat.Jpeg; break;
case ".png":
m_imgformatTarget = ImageFormat.Png; break;
case ".bmp":
m_imgformatTarget = ImageFormat.Bmp; break;
default:
m_imgformatTarget = ImageFormat.Jpeg; break;
}
m_bmSource.Save(targetPicture, m_imgformatTarget);
m_fontWords.Dispose();
m_sbTransBrush.Dispose();
m_grSource.Dispose();
m_bmSource.Dispose();
}