SoFunction
Updated on 2025-03-07

C# implements dynamically generating text and pictures on the base map

This article mainly records the text that needs to be added to dynamically generate pictures and adds the specified pictures to the bottom picture, and directly uploads the code.

/// <summary>
/// Draw a picture of the specified path on the base map/// </summary>
/// <param name="g">Paintboard example</param>/// <param name="path">Picture path</param>/// <param name="totalWidth">Total length of the painting area</param>/// <param name="totalHeight">Total Height of Painting Area</param>/// <param name="px">First point X coordinate</param>/// <param name="py">First point Y coordinate</param>  private void FontPic(ref Graphics g, string path, int totalWidth, int totalHeight, int px, int py)
  {
   if ((path))
   {
    var pImg = (path);
    // If the picture is larger than the canvas area, it will be reduced    if (totalHeight &lt;  &amp;&amp; totalWidth &lt; )
    {
     Image newPic = GetReducedImage(pImg, totalWidth, totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight &lt;  &amp;&amp; totalWidth &gt;= )
    {
     Image newPic = GetReducedImage(pImg, , totalHeight);
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else if (totalHeight &gt;=  &amp;&amp; totalWidth &lt; )
    {
     Image newPic = GetReducedImage(pImg, totalWidth, );
     if (newPic != null)
     {
      DrawPic(ref g, totalWidth, totalHeight, px, py, newPic);
     }
    }
    else
    {
     DrawPic(ref g, totalWidth, totalHeight, px, py, pImg);
    }
   }
  }
  /// &lt;summary&gt;
  /// Draw pictures on the picture  /// &lt;/summary&gt;
  /// <param name="g">Paintboard example</param>  /// <param name="totalWidth">Total length of the painting area</param>  /// <param name="totalHeight">Total Height of Painting Area</param>  /// <param name="px">First point X coordinate</param>  /// <param name="py">First point Y coordinate</param>  /// <param name="pImg">Example of the picture to be drawn</param>  private void DrawPic(ref Graphics g, int totalWidth, int totalHeight, int px, int py, Image pImg)
  {
   px += GetValue(totalWidth, );
   py += GetValue(totalHeight, );
 
   (new Bitmap(pImg, new Size(GetSize(totalWidth, ), GetSize(totalHeight, ))),
    new Rectangle(px, py, totalWidth, totalHeight),
    0, 0, totalWidth, totalHeight, );
  }
  /// &lt;summary&gt; 
  /// Generate thumbnail overload method 1, return the image object of the thumbnail  /// &lt;/summary&gt; 
  /// <param name="width">Thread of thumbnail</param>  /// <param name="height">Height of thumbnail</param>  /// <returns> Image object for thumbnail</returns>  public Image GetReducedImage(Image resourceImage, int width, int height)
  {
   try
   {
    Image data = null;
    //Initialize a new instance of Bitmap class with the specified size and format    using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
    {
     //Create a new Graphics object from the specified Image object     using (Graphics graphics = (bitmap))
     {
      //Clear the entire drawing surface and fill it with transparent background color      //();
      //Draw the original image object at the specified position and according to the specified size      (resourceImage, new Rectangle(0, 0, width, height));
     }
     data = new Bitmap(bitmap);
    }
    return data;
   }
   catch (Exception e)
   {
    throw e;
   }
  }
  /// &lt;summary&gt;
  /// Compare two values ​​and get the given value (judgment whether it is out of bounds)  /// &lt;/summary&gt;
  /// <param name="total">Total length</param>  /// <param name="width">Specify length</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public int GetSize(int total, int width)
  {
   if (total &gt; width)
   {
    return width;
   }
   else
   {
    return total;
   }
  }
  /// &lt;summary&gt;
  /// More passed value is calculated to obtain a new value (calculate point coordinates)  /// &lt;/summary&gt;
  /// <param name="total">Total length</param>  /// <param name="width">Specify length</param>  /// &lt;returns&gt;&lt;/returns&gt;
  private int GetValue(int total, int width)
  {
   return (total - width) / 2;
  }
  /// &lt;summary&gt;
  /// Draw text on the picture  /// &lt;/summary&gt;
  /// <param name="g">Image object</param>  /// <param name="pointX">Text x-coordinates</param>  /// <param name="pointY">text y coordinates</param>  /// <param name="word">Text content</param>  /// <param name="textWidth">Text width</param>  /// <param name="textHeight">text height</param>  private static void DrawStringWord(Graphics g, int pointX, int pointY, string word, int textWidth, int textHeight, int fontSize = 30)
  {
   Font font = new Font("Microsoft Yahei", fontSize, ());
   RectangleF textArea = new RectangleF(pointX, pointY, textWidth, textHeight);
   Brush brush = new SolidBrush();
   (word, font, brush, textArea);
  }

I hope it will be helpful to friends who need this.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.