欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

应用DEV第三方界面控件制作批量添加水印程序

程序员文章站 2022-06-13 17:30:11
本次应用DevExpress和C#语言制作了一个批量添加水印的程序,看界面效果图: 界面中既可以进行文字水印添加,也可以图片水印添加,同时还可以对水印的位置进行设置,比较实用! 文字水印的具体添加情况,看图: 还可以文字的预览: 整个文字水印的预览: 同时图片的水印预览: 最后显示下图片的水印效果: ......

本次应用devexpress和c#语言制作了一个批量添加水印的程序,看界面效果图:

应用DEV第三方界面控件制作批量添加水印程序

界面中既可以进行文字水印添加,也可以图片水印添加,同时还可以对水印的位置进行设置,比较实用!

文字水印的具体添加情况,看图:

应用DEV第三方界面控件制作批量添加水印程序

还可以文字的预览:

应用DEV第三方界面控件制作批量添加水印程序

整个文字水印的预览:

应用DEV第三方界面控件制作批量添加水印程序

同时图片的水印预览:

应用DEV第三方界面控件制作批量添加水印程序

最后显示下图片的水印效果:

应用DEV第三方界面控件制作批量添加水印程序

核心代码部分:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.drawing.imaging;
using system.drawing.text;
using system.linq;
using system.text;
using system.windows.forms;

namespace 添加水印_程序
{
    public partial class form1 : devexpress.xtraeditors.xtraform
    {
        public form1()
        {
            initializecomponent();
        }

        #region 获取系统字体
        private void getsystemfont(toolstripcombobox cb)
        {
            installedfontcollection myfont = new installedfontcollection();
            foreach (fontfamily ff in myfont.families)
            {
                cb.items.add(ff.name);
            }
            cb.selecteditem = "宋体";
        }
        #endregion

        private void form1_load(object sender, eventargs e)
        {
            cbbposition.selectedindex = 1;
        }

        string[] imgarray = null;
        string imgdirectorypath = null;

        private void simplebutton4_click(object sender, eventargs e)
        {
            if (openfiledialog1.showdialog() == dialogresult.ok)
            {
                lbimglist.items.clear();
                imgarray = openfiledialog1.filenames;

                string imgp = imgarray[0].tostring();
                
                imgp = imgp.remove(imgp.lastindexof("\\"));
                
                imgdirectorypath = imgp;
                for (int i = 0; i < imgarray.length; i++)
                {
                    string imgpath = imgarray[i].tostring();
                    
                    string imgname = imgpath.substring(imgpath.lastindexof("\\") + 1, imgpath.length - imgpath.lastindexof("\\") - 1);
                    //messagebox.show(imgname);
                    lbimglist.items.add(imgname);
                }
                tsslstatus.text = "图片总数:" + lbimglist.items.count;
            }
        }

        private void listboxcontrol1_selectedindexchanged(object sender, eventargs e)
        {
            if (lbimglist.selecteditems.count > 0)
            {
               tssltext.text = "图片位置:" + imgdirectorypath + "\\" + lbimglist.selecteditems[0].tostring();
            }
        }
        private void btnpreview_click(object sender, eventargs e)
        {
            if (lbimglist.items.count > 0)
            {
                if (rbtxt.checked)
                {
                    if (txtwatermarkfont.text != "" && txtsavapath.text.trim() != "")
                    {
                        addfontwatermark(txtwatermarkfont.text.trim(), lbimglist.items[0].tostring(), 0);
                        frm_browser frm2 = new frm_browser();
                        frm2.ig = bigbt;
                        frm2.showdialog();
                    }
                }
                else
                {
                    if (txtwatermarkimg.text != "" && txtsavapath.text != "")
                    {
                        changealpha();
                        addfontwatermark(txtwatermarkfont.text.trim(), lbimglist.items[0].tostring(), 2);
                        frm_browser frm2 = new frm_browser();
                        frm2.ig = bigbt;
                        frm2.showdialog();
                    }
                }
            }
        }
        private void trackbar1_enter(object sender, eventargs e)
        {
            lbimglist.focus();
        }
        private void btnselect_click(object sender, eventargs e)
        {

            if (openfiledialog2.showdialog() == dialogresult.ok)
            {
                txtwatermarkimg.text = openfiledialog2.filename;
                //messagebox.show(txtwatermarkimg.text);
               

                if (rbpic.checked == true)
                {
                    changealpha();
                    pbimgpreview.image = image.fromfile(txtwatermarkimg.text.trim());
                }
            }

        }
        private void rbtxt_checkedchanged(object sender, eventargs e)
        {
            trackbar1.enabled = false;
            if (rbpic.checked)
                pbimgpreview.image = null;
        }

        private void rbpic_checkedchanged(object sender, eventargs e)
        {
            trackbar1.enabled = true;
            if (rbtxt.checked)
            {

                pbimgpreview.image = null;

            }
        }
     private void txtwatermarkfont_textchanged(object sender, eventargs e)
        {
            if (lbimglist.items.count <= 0)
            {
                messagebox.show("请加载图片", "警告", messageboxbuttons.ok, messageboxicon.error);
                return;
            }
            else
            {
                addfontwatermark(txtwatermarkfont.text.trim(), lbimglist.items[0].tostring(), 0);
                pbimgpreview.image = bt;
            }

        }

        private void simplebutton7_click(object sender, eventargs e)
        {
            application.exit();
        }

        bitmap effect;
        bitmap source;
        image new_img;

        private void changealpha()
        {
            pbimgpreview.refresh();
            source = new bitmap(image.fromfile(txtwatermarkimg.text.trim()));
            if (source.width <= 368)
                effect = new bitmap(368, 75);
            else
            {
                image.getthumbnailimageabort callb = null;
                //对水印图片生成缩略图,缩小到原图得1/4
                new_img = source.getthumbnailimage(source.width / 4, source.width / 4, callb, new system.intptr());
                effect = new bitmap(this.new_img.width, this.new_img.height);
            }
            graphics _effect = graphics.fromimage(effect);
            float[][] matrixitems ={
                                      new float[]{1,0,0,0,0},
                                      new float [] {0,1,0,0,0},
                                      new float []{0,0,1,0,0},
                                      new float []{0,0,0,0,0},
                                      new float[]{0,0,0,trackbar1.value/255f,1}
                                   };

            colormatrix imgmatrix = new colormatrix(matrixitems);
            imageattributes imgeffect = new imageattributes();
            imgeffect.setcolormatrix(imgmatrix, colormatrixflag.default, coloradjusttype.bitmap);
            if (source.width <= 368)
            {
                _effect.drawimage(source, new rectangle(0, 0, 368, 75), 0, 0, 368, 75, graphicsunit.pixel, imgeffect);
            }
            else
            {
                _effect.drawimage(new_img, new rectangle(0, 0, new_img.width, new_img.height), 0, 0, new_img.width, new_img.height, graphicsunit.pixel, imgeffect);
            }
            pbimgpreview.image = effect;
        }



        private void trackbar1_valuechanged(object sender, eventargs e)
        {
            if (rbpic.checked && txtwatermarkimg.text.trim() != "")
                changealpha();
        }
        private void btnperform_click(object sender, eventargs e)
        {
            if (rbtxt.checked && txtsavapath.text != "" && txtwatermarkfont.text != "")
            {
                for (int i = 0; i < lbimglist.items.count; i++)
                {
                    addfontwatermark(txtwatermarkfont.text.trim(), lbimglist.items[i].tostring(), 1);
                }
                messagebox.show("添加水印成功", "提示", messageboxbuttons.ok, messageboxicon.exclamation);
            }
            if (rbpic.checked && txtsavapath.text != "" && pbimgpreview.image != null)
            {
                for (int i = 0; i < lbimglist.items.count; i++)
                {
                    addfontwatermark(txtwatermarkfont.text.trim(), lbimglist.items[i].tostring(), 3);
                }
                messagebox.show("添加水印成功", "提示", messageboxbuttons.ok, messageboxicon.exclamation);
            }

        }
        string newfolderpath;

        private void button2_click(object sender, eventargs e)
        {

            if (folderbrowserdialog1.showdialog() == dialogresult.ok)
            {
                txtsavapath.text = folderbrowserdialog1.selectedpath;
                newfolderpath = txtsavapath.text.trim();
            }
        }

 

有兴趣的可自己尝试下!!!