C#实现图片叠加,图片上嵌入文字,文字生成图片的方法

时间:2020-12-03 16:54:08

/// <summary>
    /// 图片叠加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(@"image/20160102.png");
        System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);
        System.Drawing.Image imgWarter = System.Drawing.Image.FromFile(Server.MapPath(@"Image/bear.png"));
        using (Graphics g = Graphics.FromImage(imgSrc))
        {
            g.DrawImage(imgWarter, new Rectangle(imgSrc.Width - imgWarter.Width, imgSrc.Height - imgWarter.Height,  imgWarter.Width,  imgWarter.Height),    0, 0, imgWarter.Width, imgWarter.Height, GraphicsUnit.Pixel);
        }

string newpath = Server.MapPath(@"Image/WaterMark.bmp");
        imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
        this.image_Water.ImageUrl = @"~/Image/WaterMark.bmp";
    }

/// <summary>
    /// 图片上嵌入文字
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(@"image/20160102.png");
        System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);

using (Graphics g = Graphics.FromImage(imgSrc))
        {
            g.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);
            using (Font f = new Font("宋体", 20))
            {
                using (Brush b = new SolidBrush(Color.Red))
                {
                    string addText = "我的地盘我做主";   g.DrawString(addText, f, b, 100, 20);
                }
            }
        }
        string fontpath = Server.MapPath(@"image/FontMark.bmp");
        imgSrc.Save(fontpath, System.Drawing.Imaging.ImageFormat.Bmp);
        this.image_Water.ImageUrl = @"~/image/FontMark.bmp";

}

////////////////////////////////////////以下为文字生成图片//////////////////////////////////////////////////////////

/// <summary>
    /// 把文字转换才Bitmap
    /// </summary>
    /// <param name="text"></param>
    /// <param name="font"></param>
    /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
    /// <param name="fontcolor">字体颜色</param>
    /// <param name="backColor">背景颜色</param>
    /// <returns></returns>
    private Bitmap TextToBitmap(string text, Font font, Rectangle rect, Color fontcolor, Color backColor)
    {
        Graphics g;
        Bitmap bmp;
        StringFormat format = new StringFormat(StringFormatFlags.NoClip);
        if (rect == Rectangle.Empty)
        {
            bmp = new Bitmap(1, 1);
            g = Graphics.FromImage(bmp);
            //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
            SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);

int width = (int)(sizef.Width + 1);
            int height = (int)(sizef.Height + 1);
            rect = new Rectangle(0, 0, width, height);
            bmp.Dispose();

bmp = new Bitmap(width, height);
        }
        else
        {
            bmp = new Bitmap(rect.Width, rect.Height);
        }

g = Graphics.FromImage(bmp);

//使用ClearType字体功能
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        g.FillRectangle(new SolidBrush(backColor), rect);
        g.DrawString(text, font, Brushes.Black, rect, format);
        return bmp;
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        string str = @"开始时间:2016-1-1" + "\r\n" + "结束时间:2017-1-1"+"\r\n"+ "沙尘天气等级:2"+"\r\n" + "PM10日均浓度最大值:2ug/m3"+"\r\n" + "影响范围:济南,青岛";
        //得到Bitmap(传入Rectangle.Empty自动计算宽高)
        Bitmap bmp = TextToBitmap(str, new Font("Arial", 16), Rectangle.Empty,Color.Black,Color.Wheat);

//保存到桌面save.jpg
        string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
        bmp.Save(directory + "\\save.png", ImageFormat.Png);
    }