diff --git a/src/Colder.CommonUtil/Helpers/ImgHelper.cs b/src/Colder.CommonUtil/Helpers/ImgHelper.cs
index 614e4d8..e5e6cfb 100644
--- a/src/Colder.CommonUtil/Helpers/ImgHelper.cs
+++ b/src/Colder.CommonUtil/Helpers/ImgHelper.cs
@@ -11,16 +11,6 @@ namespace Colder.CommonUtil
///
public class ImgHelper
{
- ///
- /// 从文件获取图片
- ///
- /// 文件名
- ///
- public static Image GetImgFromFile(string fileName)
- {
- return Image.FromFile(fileName);
- }
-
///
/// 从base64字符串读入图片
///
@@ -145,74 +135,59 @@ public static string GetBase64String(string base64UrlStr)
}
///
- /// 图片添加文字,文字大小自适应
+ /// 图片添加文字水印,文字大小自适应
///
/// 图片
- /// 左上角定位像素(x1,y1)
- /// 右下角定位像素(x2,y2)
+ /// 文字区域
/// 文字内容
/// 字体颜色
/// 字体名称
/// 添加文字后的Bitmap对象
- public static Image AddText(Image image, (int x, int y) locationLeftTop, (int x, int y) locationRightBottom, string text, Color color, string fontName = "ttf")
+ public static Image AddText(Image image, Rectangle area, string text, Color color, string fontName = "微软雅黑")
{
int width = image.Width;
int height = image.Height;
Bitmap bmp = new Bitmap(width, height);
- using Graphics graph = Graphics.FromImage(bmp);
-
- // 计算文字区域
- // 左上角
- float x1 = locationLeftTop.x;
- float y1 = locationLeftTop.y;
- // 右下角
- float x2 = locationRightBottom.x;
- float y2 = locationRightBottom.y;
- // 区域宽高
- float fontWidth = x2 - x1;
- float fontHeight = y2 - y1;
-
- // 初次估计先用文字区域高度作为文字字体大小,后面再做调整,单位为px
- float fontSize = fontHeight;
-
- Font font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
- SizeF sf = graph.MeasureString(text, font);
-
- int times = 0;
-
- // 调整字体大小以适应文字区域
- if (sf.Width > fontWidth)
+ using (Graphics graph = Graphics.FromImage(bmp))
{
- while (sf.Width > fontWidth)
+ // 计算文字区域
+ // 区域宽高
+ float fontWidth = area.Width;
+ float fontHeight = area.Height;
+ // 初次估计先用文字区域高度作为文字字体大小,后面再做调整,单位为px
+ float fontSize = fontHeight;
+ Font font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
+ SizeF sf = graph.MeasureString(text, font);
+ int times = 0;
+ // 调整字体大小以适应文字区域
+ if (sf.Width > fontWidth)
{
- fontSize -= 0.1f;
- font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
- sf = graph.MeasureString(text, font);
-
- times++;
+ while (sf.Width > fontWidth)
+ {
+ fontSize -= 0.1f;
+ font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
+ sf = graph.MeasureString(text, font);
+ times++;
+ }
}
- }
- else if (sf.Width < fontWidth)
- {
- while (sf.Width < fontWidth)
+ else if (sf.Width < fontWidth)
{
- fontSize += 0.1f;
- font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
- sf = graph.MeasureString(text, font);
-
- times++;
+ while (sf.Width < fontWidth)
+ {
+ fontSize += 0.1f;
+ font = new Font(fontName, fontSize, GraphicsUnit.Pixel);
+ sf = graph.MeasureString(text, font);
+ times++;
+ }
}
+ // 最终的得出的字体所占区域一般不会刚好等于实际区域
+ // 所以根据两个区域的相差之处再把文字开始位置(左上角定位)稍微调整一下
+ var x1 = area.X + ((fontWidth - sf.Width) / 2);
+ var y1 = area.Y + ((fontHeight - sf.Height) / 2);
+ graph.DrawImage(image, 0, 0, width, height);
+ graph.DrawString(text, font, new SolidBrush(color), x1, y1);
+ return bmp;
}
-
- // 最终的得出的字体所占区域一般不会刚好等于实际区域
- // 所以根据两个区域的相差之处再把文字开始位置(左上角定位)稍微调整一下
- x1 += (fontWidth - sf.Width) / 2;
- y1 += (fontHeight - sf.Height) / 2;
-
- graph.DrawImage(image, 0, 0, width, height);
- graph.DrawString(text, font, new SolidBrush(color), x1, y1);
-
- return bmp;
}
}
}