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

asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧

程序员文章站 2022-10-13 08:12:57
public class mygdi {     public static void&nb...
public class mygdi
{
    public static void createwatermark(string ssrcfilepath, string sdstfilepath, string stext1, string scolor1, string ssize1, string sfont1, string stext2, string scolor2, string ssize2, string sfont2, string sbgcolor, string stransparence)
    {
        system.drawing.image image = system.drawing.image.fromfile(ssrcfilepath);
        graphics g = graphics.fromimage(image);
        g.smoothingmode = smoothingmode.antialias;
        g.interpolationmode = interpolationmode.highqualitybicubic;
        g.compositingquality = compositingquality.highquality;
        g.textrenderinghint = system.drawing.text.textrenderinghint.antialias; //文字抗锯齿
        g.drawimage(image, 0, 0, image.width, image.height);
        font f1 = new font(sfont1, float.parse(ssize1));
        font f2 = new font(sfont2, float.parse(ssize2));
        brush brushfortext1 = new solidbrush(colortranslator.fromhtml(scolor1));
        brush brushfortext2 = new solidbrush(colortranslator.fromhtml(scolor2));
        brush brushforbg = new solidbrush(color.fromargb(convert.toint16(255 * float.parse(stransparence)), colortranslator.fromhtml(sbgcolor)));
        g.rotatetransform(-20);
        rectangle rect = new rectangle(-image.width/2-50, image.height - 50, image.width * 2, 40);
        g.drawrectangle(new pen(brushforbg), rect);
        g.fillrectangle(brushforbg, rect);
        rectangle rectfortext1 = new rectangle(-image.width/2 + image.width / 5, image.height - 45, image.width * 2, 60);
        for (int i = 0; i < 10; i++)
            g.drawstring(stext1, f1, brushfortext1, rectfortext1);
        rectangle rectfortext2 = new rectangle(-image.width / 2 + image.width / 5, image.height -25, image.width * 2, 60);
        for (int i = 0; i < 10; i++)
            g.drawstring(stext2, f2, brushfortext2, rectfortext2);
        image.save(sdstfilepath, imageformat.jpeg);
        image.dispose();
    }
    public static void createroundedcorner(string ssrcfilepath, string sdstfilepath, string scornerlocation)
    {
        system.drawing.image image = system.drawing.image.fromfile(ssrcfilepath);
        graphics g = graphics.fromimage(image);
        g.smoothingmode = smoothingmode.highquality;
        g.interpolationmode = interpolationmode.highqualitybicubic;
        g.compositingquality = compositingquality.highquality;
        rectangle rect = new rectangle(0, 0, image.width, image.height);
        graphicspath rectpath = createroundrectanglepath(rect, image.width / 10, scornerlocation); //构建圆角外部路径
        brush b = new solidbrush(color.white);//圆角背景白色
        g.drawpath(new pen(b), rectpath);
        g.fillpath(b, rectpath);
        g.dispose();
        image.save(sdstfilepath, imageformat.jpeg);
        image.dispose();
    }
    public static void createplaintext(string ssrcfilepath, string sdstfilepath,string stext, string scolor, string ssize, string sfont)
    {
        system.drawing.image image = system.drawing.image.fromfile(ssrcfilepath);
        graphics g = graphics.fromimage(image);
        g.smoothingmode = smoothingmode.antialias;
        g.interpolationmode = interpolationmode.highqualitybicubic;
        g.compositingquality = compositingquality.highquality;
        g.drawimage(image, 0, 0, image.width, image.height);
        g.textrenderinghint = system.drawing.text.textrenderinghint.antialias; //文字抗锯齿
        font f = new font(sfont,float.parse(ssize));
        brush b = new solidbrush(colortranslator.fromhtml(scolor));
        rectangle rect = new rectangle(10, 5, image.width, image.height); //适当空开一段距离        
        for (int i = 0; i < 30; i++) //加强亮度
            g.drawstring(stext, f, b, rect);
        image.save(sdstfilepath, imageformat.jpeg);
        image.dispose();
    }
    private static graphicspath createroundrectanglepath(rectangle rect, int radius, string sposition)
    {
        graphicspath rectpath = new graphicspath();
        switch (sposition)
        {
            case "topleft":
                {
                    rectpath.addarc(rect.left, rect.top, radius * 2, radius * 2, 180, 90);
                    rectpath.addline(rect.left, rect.top, rect.left, rect.top + radius);
                    break;
                }
            case "topright":
                {
                    rectpath.addarc(rect.right - radius * 2, rect.top, radius * 2, radius * 2, 270, 90);
                    rectpath.addline(rect.right, rect.top, rect.right - radius, rect.top);
                    break;
                }
            case "bottomleft":
                {
                    rectpath.addarc(rect.left, rect.bottom - radius * 2, radius * 2, radius * 2, 90, 90);
                    rectpath.addline(rect.left, rect.bottom - radius, rect.left, rect.bottom);
                    break;
                }
            case "bottomright":
                {
                    rectpath.addarc(rect.right - radius * 2, rect.bottom - radius * 2, radius * 2, radius * 2, 0, 90);
                    rectpath.addline(rect.right - radius, rect.bottom, rect.right, rect.bottom);
                    break;
                }
        }
        return rectpath;
    }
}