0
我想在图像上添加两个水印文字,一个在图像底部左侧,另一个在底部右侧,与图像尺寸无关。以下是我的方法:相同尺寸但不同dpi的图像上的水印文字字体大小
public void AddWaterMark(string leftSideText, string rightSideText, string imagePath)
{
string firstText = leftSideText;
string secondText = rightSideText;
Bitmap bitmap = (Bitmap)Image.FromFile(imagePath);//load the image file
PointF firstLocation = new PointF((float)(bitmap.Width * 0.035), bitmap.Height - (float)(bitmap.Height * 0.06));
PointF secondLocation = new PointF(((float)((bitmap.Width/2) + ((bitmap.Width/2) * 0.6))), bitmap.Height - (float)(bitmap.Height * 0.055));
int opacity = 155, baseFontSize = 50;
int leftTextSize = 0, rightTextSize = 0;
leftTextSize = (bitmap.Width * baseFontSize)/1920;
rightTextSize = leftTextSize - 5;
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font arialFontLeft = new Font(FontFamily.GenericSerif, leftTextSize);
Font arialFontRight = new Font(FontFamily.GenericSerif, rightTextSize);
graphics.DrawString(firstText, arialFontLeft, new SolidBrush(Color.FromArgb(opacity, Color.White)), firstLocation);
graphics.DrawString(secondText, arialFontRight, new SolidBrush(Color.FromArgb(opacity, Color.White)), secondLocation);
}
string fileLocation = HttpContext.Current.Server.MapPath("~/Images/Albums/") + Path.GetFileNameWithoutExtension(imagePath) + "_watermarked" + Path.GetExtension(imagePath);
bitmap.Save(fileLocation);//save the image file
bitmap.Dispose();
if (File.Exists(imagePath))
{
File.Delete(imagePath);
File.Move(fileLocation, fileLocation.Replace("_watermarked", string.Empty));
}
}
我现在面临的问题是与设定水位标记文本font size
正常。假设有两个图像尺寸为1600 x 900
,第一个图像的dpi
的为72
,第二个图像的尺寸为dpi
的为240
。上述方法对于72
dpi的图像工作正常,但对于图像240
dpi
,font size
的水印文本变得太大并溢出图像。如何正确计算font size
与不同dpi
的图像但具有相同的尺寸?
因此,您是否希望字体对于较大的DPI值更小? (字体大小以像素为单位而不是DPI相关) – grek40