在c#的打印区域中,在图像下方添加水印
本文关键字:添加 方添加 图像 打印 区域 | 更新日期: 2023-09-27 17:50:36
我正试图在我的windows窗体中插入TIFF图像下方的文本水印,并且肯定会感谢任何人的帮助。我有一个打印按钮,检索图像,缩小它,然后根据我的空白,放置相应的图像打印。我想添加一个额外的部分,就在图像打印之前,我添加了一个文本水印(在这种情况下是一个日期戳),就在图像下方。
我试过调整边距,但这只是增加(或减少取决于数字设置)的图像规模,但不添加额外的空间,我想添加水印。下面是我到目前为止的代码:
protected void PrintPage(object sender, PrintPageEventArgs e)
{
if (this.Image == null)
{
e.Cancel = true;
return;
}
//ADD TIME STAMP WATERMARK
string watermark = "DATE ISSUED: " + String.Format("{0:MM/dd/yyyy}", System.DateTime.Now.Date);
System.Drawing.Graphics gpr = Graphics.FromImage(Image);
System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);
Font font = new System.Drawing.Font("Arial", 55, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
SizeF size = e.Graphics.MeasureString(watermark, font);
float x = 0;
float y = Image.Height-size.Height;
RectangleF printArea = new RectangleF(x, y, size.Width, size.Height);
gpr.DrawString(watermark, font, brush, printArea);
e.Graphics.DrawImage(this.Image, e.MarginBounds);
}
我在App.config中设置了e.p marginbounds的值,包括以下值:Left=70, Right=90, Top=190;底= 475。所有的打印结果都将在8号半乘11号纸上打印人像样式。
我能够在图像顶部的任何地方显示水印,但我希望把它放在下面。当我调整y坐标时,它恰好在图像下方,当我打印时,我假设它在打印区域之外,因此,水印不会打印在页面上(它只显示图像)。
我感谢大家的帮助,因为我一直在绞尽脑汁,但没有运气。
你不是在图片下面打印文字吗?我想你应该从y=Image开始打印。高度+ e。和x=e.MarginBounds.Left
这将在图像下方的空白处打印一个左对齐的标签。
更新:This works:
y=-size.Height + e.MarginBounds.Bottom;
x = e.MarginBounds.Left;
e.Graphics.DrawImage(Image, e.MarginBounds);
// note the change. Using e.graphics instead of gpr below
e.Graphics.DrawString(watermark, font, brush, printArea);