欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#實(shí)現(xiàn)在底圖上動態(tài)生成文字和圖片

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文主要記錄在圖片上動態(tài)的生成需要添加的文字和把指定的圖片加到底圖上,直接上代碼

/// <summary>
/// 在底圖上畫指定路徑的圖片
/// </summary>
/// <param name="g">畫板實(shí)例</param>
/// <param name="path">圖片路徑</param>
/// <param name="totalWidth">畫區(qū)總長度</param>
/// <param name="totalHeight">畫區(qū)總高度</param>
/// <param name="px">起點(diǎn)X坐標(biāo)</param>
/// <param name="py">起點(diǎn)Y坐標(biāo)</param>
  private void FontPic(ref Graphics g, string path, int totalWidth, int totalHeight, int px, int py)
  {
   if (File.Exists(path))
   {
    var pImg = Image.FromFile(path);
    //如果圖片大于畫布區(qū)域,則縮小
    if (totalHeight < pImg.Height && totalWidth < pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, totalWidth, totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight < pImg.Height && totalWidth >= pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, pImg.Width, totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight >= pImg.Height && totalWidth < pImg.Width)
    {
     Image newPic = GetReducedImage(pImg, totalWidth, pImg.Height);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else
    {
     DrawPic(ref g, totalWidth, totalHeight, px, py, pImg);
    }
   }
  }
  /// <summary>
  /// 在圖上畫圖片
  /// </summary>
  /// <param name="g">畫板實(shí)例</param>
  /// <param name="totalWidth">畫區(qū)總長度</param>
  /// <param name="totalHeight">畫區(qū)總高度</param>
  /// <param name="px">起點(diǎn)X坐標(biāo)</param>
  /// <param name="py">起點(diǎn)Y坐標(biāo)</param>
  /// <param name="pImg">要畫的圖片實(shí)例</param>
  private void DrawPic(ref Graphics g, int totalWidth, int totalHeight, int px, int py, Image pImg)
  {
   px += GetValue(totalWidth, pImg.Width);
   py += GetValue(totalHeight, pImg.Height);
 
   g.DrawImage(new Bitmap(pImg, new Size(GetSize(totalWidth, pImg.Width), GetSize(totalHeight, pImg.Height))),
    new Rectangle(px, py, totalWidth, totalHeight),
    0, 0, totalWidth, totalHeight, GraphicsUnit.Pixel);
  }
  /// <summary> 
  /// 生成縮略圖重載方法1,返回縮略圖的Image對象 
  /// </summary> 
  /// <param name="width">縮略圖的寬度</param> 
  /// <param name="height">縮略圖的高度</param> 
  /// <returns>縮略圖的Image對象</returns> 
  public Image GetReducedImage(Image resourceImage, int width, int height)
  {
   try
   {
    Image data = null;
    //用指定的大小和格式初始化Bitmap類的新實(shí)例 
    using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
    {
     //從指定的Image對象創(chuàng)建新Graphics對象 
     using (Graphics graphics = Graphics.FromImage(bitmap))
     {
      //清除整個繪圖面并以透明背景色填充 
      //graphics.Clear(Color.Transparent);
      //在指定位置并且按指定大小繪制原圖片對象 
      graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
     }
     data = new Bitmap(bitmap);
    }
    return data;
   }
   catch (Exception e)
   {
    throw e;
   }
  }
  /// <summary>
  /// 比較兩個值,得到給到給定值(判斷是否越界)
  /// </summary>
  /// <param name="total">總長度</param>
  /// <param name="width">指定長度</param>
  /// <returns></returns>
  public int GetSize(int total, int width)
  {
   if (total > width)
   {
    return width;
   }
   else
   {
    return total;
   }
  }
  /// <summary>
  /// 更加傳入的值計算得到新值(計算點(diǎn)坐標(biāo))
  /// </summary>
  /// <param name="total">總長度</param>
  /// <param name="width">指定長度</param>
  /// <returns></returns>
  private int GetValue(int total, int width)
  {
   return (total - width) / 2;
  }
  /// <summary>
  /// 在圖片上畫出文字
  /// </summary>
  /// <param name="g">圖片對象</param>
  /// <param name="pointX">文字x坐標(biāo)</param>
  /// <param name="pointY">文字y坐標(biāo)</param>
  /// <param name="word">文字內(nèi)容</param>
  /// <param name="textWidth">文本寬度</param>
  /// <param name="textHeight">文本高度</param>
  private static void DrawStringWord(Graphics g, int pointX, int pointY, string word, int textWidth, int textHeight, int fontSize = 30)
  {
   Font font = new Font("微軟雅黑", fontSize, (FontStyle.Regular));
   RectangleF textArea = new RectangleF(pointX, pointY, textWidth, textHeight);
   Brush brush = new SolidBrush(Color.Black);
   g.DrawString(word, font, brush, textArea);
  }

希望對需要這方面操作的朋友有所幫助。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C#生成Word文件(圖片、文字)

欄    目:C#教程

下一篇:【C#基礎(chǔ)】Substring截取字符串的方法小結(jié)(推薦)

本文標(biāo)題:C#實(shí)現(xiàn)在底圖上動態(tài)生成文字和圖片

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4751.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有