欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年05月29日 16:31:12   作者:寒冰屋  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

/// <summary>
/// 在底圖上畫指定路徑的圖片
/// </summary>
/// <param name="g">畫板實(shí)例</param>
/// <param name="path">圖片路徑</param>
/// <param name="totalWidth">畫區(qū)總長(zhǎng)度</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ū)總長(zhǎng)度</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對(duì)象 
  /// </summary> 
  /// <param name="width">縮略圖的寬度</param> 
  /// <param name="height">縮略圖的高度</param> 
  /// <returns>縮略圖的Image對(duì)象</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對(duì)象創(chuàng)建新Graphics對(duì)象 
     using (Graphics graphics = Graphics.FromImage(bitmap))
     {
      //清除整個(gè)繪圖面并以透明背景色填充 
      //graphics.Clear(Color.Transparent);
      //在指定位置并且按指定大小繪制原圖片對(duì)象 
      graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
     }
     data = new Bitmap(bitmap);
    }
    return data;
   }
   catch (Exception e)
   {
    throw e;
   }
  }
  /// <summary>
  /// 比較兩個(gè)值,得到給到給定值(判斷是否越界)
  /// </summary>
  /// <param name="total">總長(zhǎng)度</param>
  /// <param name="width">指定長(zhǎng)度</param>
  /// <returns></returns>
  public int GetSize(int total, int width)
  {
   if (total > width)
   {
    return width;
   }
   else
   {
    return total;
   }
  }
  /// <summary>
  /// 更加傳入的值計(jì)算得到新值(計(jì)算點(diǎn)坐標(biāo))
  /// </summary>
  /// <param name="total">總長(zhǎng)度</param>
  /// <param name="width">指定長(zhǎng)度</param>
  /// <returns></returns>
  private int GetValue(int total, int width)
  {
   return (total - width) / 2;
  }
  /// <summary>
  /// 在圖片上畫出文字
  /// </summary>
  /// <param name="g">圖片對(duì)象</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);
  }

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity 如何設(shè)定 Animator分割播放

    Unity 如何設(shè)定 Animator分割播放

    這篇文章主要介紹了Unity 設(shè)定 Animator分割播放的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C# PC版微信消息監(jiān)聽自動(dòng)回復(fù)的實(shí)現(xiàn)方法

    C# PC版微信消息監(jiān)聽自動(dòng)回復(fù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了C# PC版微信消息監(jiān)聽自動(dòng)回復(fù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C#?wpf?無(wú)邊框窗口添加陰影效果的實(shí)現(xiàn)

    C#?wpf?無(wú)邊框窗口添加陰影效果的實(shí)現(xiàn)

    在本篇內(nèi)容中小編給大家整理了一篇關(guān)于C#?wpf?無(wú)邊框窗口添加陰影效果的具體方法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下
    2022-11-11
  • C# 字符串的連接(實(shí)例講解)

    C# 字符串的連接(實(shí)例講解)

    下面小編就為大家分享一篇C# 字符串的連接實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • C#合并BitMap圖像生成超大bitmap

    C#合并BitMap圖像生成超大bitmap

    當(dāng)兩個(gè)圖像合并的時(shí)候,以簡(jiǎn)單的使用gdi+,當(dāng)需要將許多bitmap合并時(shí)就會(huì)造成寬度過(guò)大,那么怎么實(shí)現(xiàn)C#合并BitMap圖像,本文就詳細(xì)的介紹一下
    2021-11-11
  • C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā)

    C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā)

    這篇文章介紹了C#對(duì)接阿里云IOT平臺(tái)進(jìn)行設(shè)備開發(fā),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • c# DataView.ToTable()方法 去除表的重復(fù)項(xiàng)問(wèn)題

    c# DataView.ToTable()方法 去除表的重復(fù)項(xiàng)問(wèn)題

    這篇文章主要介紹了c# DataView.ToTable()方法 去除表的重復(fù)項(xiàng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • C# FileStream簡(jiǎn)單介紹和使用

    C# FileStream簡(jiǎn)單介紹和使用

    這篇文章主要為大家詳細(xì)介紹了C# FileStream基本功能和使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • WPF仿微信實(shí)現(xiàn)截圖功能的方法詳解

    WPF仿微信實(shí)現(xiàn)截圖功能的方法詳解

    這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)截圖功能(仿微信),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-07-07
  • 詳解C#編程中構(gòu)造函數(shù)的使用

    詳解C#編程中構(gòu)造函數(shù)的使用

    這篇文章主要介紹了詳解C#編程中構(gòu)造函數(shù)的使用,是C#入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01

最新評(píng)論