private static Point SetMarkPoint(WaterPositionMode position, Image watermak, Image orImage) { int x = 0; int y = 0; switch (position) { case WaterPositionMode.LeftTop: x = 8; y = 8; break; case WaterPositionMode.LeftBottom: x = 8; y = orImage.Height - watermak.Height; break; case WaterPositionMode.RightTop: x = orImage.Width * 1 - watermak.Width; y = 8; break; case WaterPositionMode.RightBottom: x = orImage.Width - watermak.Width; y = orImage.Height - watermak.Height; break; case WaterPositionMode.Center: x = orImage.Width / 3; y = orImage.Height / 2 - watermak.Height / 2; break; } return(new Point(x, y)); }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="path">原图片绝对地址</param> /// <param name="suiyi">水印文件</param> /// <param name="pos">水印位置</param> private static byte[] addWaterMark(Image image, string suiyi, WaterPositionMode pos) { try { Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); 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, 1.0f, 0.0f },//设置透明度 new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; Point position = SetMarkPoint(pos, watermark, image); xpos = position.X; // ((image.Width - watermark.Width) - 50);//水印位置 ypos = position.Y; //image.Height - watermark.Height - 50;//水印位置 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); MemoryStream ms = new MemoryStream(); b.Save(ms, ImageFormat.Bmp); byte[] byteImage = ms.ToArray(); b.Dispose(); image.Dispose(); g.Dispose(); return(byteImage); } catch (Exception ex) { MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Bmp); return(ms.ToArray()); } }
/// <summary> /// 给图片加水印文字 /// </summary> /// <param name="oldpath">原图文件</param> /// <param name="savepath">新图文件</param> /// <param name="watertext">水印文字</param> /// <param name="position">水印位置</param> /// <param name="color">颜色</param> /// <param name="alpha">数值</param> public static void AddWaterText(string oldPath, string newPath, string waterText, WaterPositionMode position, string color, int alpha) { Image image = Image.FromFile(oldPath); if (File.Exists(newPath)) { File.Delete(newPath); } System.IO.File.Copy(oldPath, newPath); int lucencyPercent = 25; Bitmap bitmap = new Bitmap(image.Width, image.Height); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); Font font = new Font("arial", 12); SizeF ziSizeF = new SizeF(); ziSizeF = graphics.MeasureString(waterText, font); float x = 0f; float y = 0f; switch (position) { case WaterPositionMode.LeftTop: x = ziSizeF.Width / 2f; y = 8f; break; case WaterPositionMode.LeftBottom: x = ziSizeF.Width / 2f; y = image.Height - ziSizeF.Height; break; case WaterPositionMode.RightTop: x = image.Width * 1f - ziSizeF.Width / 2f; y = 8f; break; case WaterPositionMode.RightBottom: x = image.Width - ziSizeF.Width; y = image.Height - ziSizeF.Height; break; case WaterPositionMode.Center: x = image.Width / 2; y = image.Height / 2 - ziSizeF.Height / 2; break; } try { StringFormat stringFormat = new StringFormat { Alignment = StringAlignment.Center }; SolidBrush solidBrush = new SolidBrush(Color.FromArgb(alpha, 0, 0, 0)); graphics.DrawString(waterText, font, solidBrush, x + 1f, y + 1f, stringFormat); SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, ColorTranslator.FromHtml(color))); graphics.DrawString(waterText, font, brush, x, y, stringFormat); solidBrush.Dispose(); brush.Dispose(); bitmap.Save(newPath, ImageFormat.Jpeg); } catch (Exception e) { } finally { bitmap.Dispose(); image.Dispose(); } }