• 生成图片水印


    public static void AddWaterText(string oldpath,string savepath,string watertext,WaterPositionMode position,string color,int alpha)
            {
                Image image = Image.FromFile(oldpath);
                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",18);
                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(savepath, ImageFormat.Jpeg);
                }
                catch (Exception e)
                {
    
    
                }
                finally
                {
                    bitmap.Dispose();
                    image.Dispose();
                }
                
            }
    
    public enum WaterPositionMode
        {
            LeftTop,
            LeftBottom,
            RightTop,
            RightBottom,
            Center
        }
  • 相关阅读:
    通过git命令行从github或服务器上克隆、修改和更新项目
    将项目部署到 github上(部署到码云操作一样,前提是有码云账号)
    tp5中代替tp3.2中的一些方法
    关于ThinkPHP下使用Uploadify插件 仅有火狐提示HTTP Error (302)错误的解决办法
    nginx轮询宕机解决
    nginx实现动静分离
    nginx实现负载均衡
    nginx实现反向代理
    解决表单重复提交
    解决跨域问题
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/6925125.html
Copyright © 2020-2023  润新知