基本信息
源码名称:批量添加水印-根据数据库条件判断添加文字水印
源码大小:0.26M
文件格式:.rar
开发语言:C#
更新时间:2021-03-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

根据数据库查询BOM,查询子项物料数量,汇总后给自动给实际图纸添加数量水印,便于生产计划操作

//数据库连接获取数量

DataTable table = new DataTable(); 
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = "server=.;database=AIS20200312125333;uid=zz;pwd=zz";
            sqlcon.Open();
            SqlCommand com = new SqlCommand();
            com.Connection = sqlcon;
            com.CommandType = CommandType.Text;
            com.CommandText = "";
            //SqlDataReader dr = com.ExecuteReader();//执行SQL语句
            //int KKone = Convert.ToInt32(com.ExecuteScalar());
            //dr.Close();//关闭执行
            //MessageBox.Show(Convert.ToString(1));
            
            sqlcon.Close();//关闭数据库
            
            #region
            StringBuilder sb = new StringBuilder();
            List<jh> jhs = new List<jh>();
            jh csjh = new jh();
            csjh.jhbm = textBox4.Text.Trim();
            csjh.jhcj = "0";
            csjh.jhsl = "1";
            csjh.jhzx = "1";
            jhs.Add(csjh);
            for (int y = 0; y < 10; y )
            {
                List<jh> jhtext = new List<jh>();
                foreach (jh jhobj in jhs)
                {
                    jhtext.Add(jhobj);
                }
                foreach (jh jhobj in jhtext)
                {
                    if (jhobj.jhcj == Convert.ToString(y) && jhobj.jhzx == "1")
                    {
                        sb.AppendFormat(@"/*dialect*/
                          SELECT * FROM T_ENG_BOM where FMATERIALID in
                         (select FMATERIALID from T_BD_MATERIAL where FNUMBER='{0}') and FDocumentStatus='c'
                          order by FCREATEDATE desc
                         ", jhobj.jhbm);
                        sqlcon.Open();//开启数据库
                        table.Clear();
                        com.CommandText = sb.ToString();
                        sb.Remove(0, sb.Length);
                        SqlDataAdapter sqlAdapter = new SqlDataAdapter(com);
                        sqlAdapter.Fill(table);
                        sqlcon.Close();//关闭数据库
                        if (table.Rows.Count == 0)
                            continue;

//水印

private void AddFontWatermark(string txt,string Iname,int i) 
{
       b = new SolidBrush(fontColor);                                                       //创建SolidBrush对象
       bt = new Bitmap(368,75);                                                        //创建Bitmap对象,设置水印图片大小
       BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath "\\" Iname));    //通过图片路径创建Bitmap对象
       Graphics g = Graphics.FromImage(bt);                                            //创建Graphics对象
       Graphics g1 = Graphics.FromImage(BigBt);                              //创建Graphics对象
       g.Clear(Color.Gainsboro);                                                        //设置画布背景颜色
       pbImgPreview.Image = bt;                                                       //设置Image属性
       if (FontF == null)                                                                    //判断是否设置字体
       {
           f = new Font(txt,fontSize);                                                      //创建字体对象
           SizeF XMaxSize = g.MeasureString(txt, f);                                //获取字体的大小
           Fwidth = (int)XMaxSize.Width;                                                //字体宽度
           Fheight = (int)XMaxSize.Height;                                               //字体高度
           g.DrawString(txt,f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2); //绘制文字水印
           if (cbbPosition.SelectedIndex==0)                                            //正中
           {
               g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);
           }
           if (cbbPosition.SelectedIndex == 1)                                                 //左上
           {
               g1.DrawString(txt, f, b, 30,30);
           }
           if (cbbPosition.SelectedIndex ==2)                                           //左下
           {
               g1.DrawString(txt, f, b, 30, (int)(BigBt.Height - Fheight)-30);
           }
           if (cbbPosition.SelectedIndex == 3)                                                 //右上
           {
               g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), 30);
           }
           if (cbbPosition.SelectedIndex == 4)                                                 //右下
           {
               g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
           }
       }
       else                                                                          //如果设置了字体
       {
           f = new Font(FontF, fontSize, fontStyle);                                  //创建字体对象
           SizeF XMaxSize = g.MeasureString(txt, f);                                //获取字体的大小
           Fwidth = (int)XMaxSize.Width;                                                //字体宽度
           Fheight = (int)XMaxSize.Height;                                               //字体高度
              //绘制文字水印
           g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);
           if (cbbPosition.SelectedIndex == 0)                                                 //正中
           {
                    g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth) / 2, 
                        (int)(BigBt.Height - Fheight) / 2);
            }
            if (cbbPosition.SelectedIndex == 1)                                         //左上
            {
                g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30);
            }
            if (cbbPosition.SelectedIndex == 2)                                         //左下
            {
                g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, (int)(BigBt.Height - Fheight)-30);
            }
            if (cbbPosition.SelectedIndex == 3)                                        //右上
            {
                g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), Fheight);
            }
            if (cbbPosition.SelectedIndex == 4)                                         //右下
            {
                g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 
                        (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
        }
    }
    if (i == 1)                                                                             //i为1时,保存绘制了文字谁赢的图片
    {
        string ipath;                                                                          //加水印后图片路径
        if (NewFolderPath.Length == 3)                                                    //判断是不是系统磁盘
//如果是系统磁盘则删除“:”后边的字符
            ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
        else                                                                               //否则
            ipath = NewFolderPath;                                                         //获取路径
//获取图片类型
        string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
        if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")          //如果原图是jpeg格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg);                     //加水印后依然保存成jpeg
        }
        if (imgstype.ToLower() == "png")                                                        //如果原图是png格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Png);               //加水印后依然保存成png格式
        }
        if (imgstype.ToLower() == "bmp")                                                //如果原图是bmp格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp);                     //加水印后依然保存成bmp
        }
        if (imgstype.ToLower() == "gif")                                                   //如果原图是gif格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif);                //加水印后依然保存成gif
        }
        g1.Dispose();
        BigBt.Dispose();
    }
    if (i == 2)                                                                             //加完图片水印后预览
    {
        if (cbbPosition.SelectedIndex == 0)                                                //正中
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
        }
        if (cbbPosition.SelectedIndex == 1)                                                //左上
        {
            g1.DrawImage(effect, 30, 30);
        }
        if (cbbPosition.SelectedIndex == 2)                                                //左下
        {
            g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
        }
        if (cbbPosition.SelectedIndex == 3)                                               //右上
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, 30);
        }
        if (cbbPosition.SelectedIndex == 4)                                                //右下
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, (int)(BigBt.Height - effect.Height) - 30);
        }
    }
    if (i == 3)                                                                             //加完图片水印后保存
    {
        if (cbbPosition.SelectedIndex == 0)                                                //正中
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
        }
        if (cbbPosition.SelectedIndex == 1)                                                //左上
        {
            g1.DrawImage(effect, 30, 30);
        }
        if (cbbPosition.SelectedIndex == 2)                                                //左下
        {
            g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
        }
        if (cbbPosition.SelectedIndex == 3)                                                //右上
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), 30);
        }
        if (cbbPosition.SelectedIndex == 4)                                                //右下
        {
            g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), (int)(BigBt.Height - effect.Height) - 30);
        }
        string ipath;
        if (NewFolderPath.Length == 3)
            ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
        else
            ipath = NewFolderPath;
        string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
        if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")          //如果原图是jpeg格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg);                     //加水印后依然保存成jpeg格式
        }
        if (imgstype.ToLower() == "png")                                                        //如果原图是png格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Png);               //加水印后依然保存成png格式
        }
        if (imgstype.ToLower() == "bmp")                                                //如果原图是bmp格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp);                     //加水印后依然保存成bmp格式
        }
        if (imgstype.ToLower() == "gif")                                                   //如果原图是gif格式
        {
            BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif);                //加水印后依然保存成gif格式
        }
    }
}