C#實現圖片疊加,圖片上嵌入文字,文字生成圖片的方法

/// <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";字體

    }this

 

////////////////////////////////////////如下爲文字生成圖片//////////////////////////////////////////////////////////orm

  /// <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);
    }ip

相關文章
相關標籤/搜索