2015-05-22 66 views
2

我试图在Windows窗体中的TIFF图像下插入文本水印,并且肯定会感谢任何人的帮助。我有一个打印按钮,用于检索图像,缩小图像,然后基于我的空白,相应地放置图像以打印。我想在图像打印之前添加一个额外的部分,我在文本图像下方添加一个文本水印(在这种情况下为日期戳)。在C图像下方添加水印,但在打印区域内添加水印

我已经尝试调整边距,但只是增加(或减少取决于数字设置)的图像比例,但不添加额外的房间,我想添加水印。下面是我到目前为止的代码:

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.MarginBounds的值:左= 70,右= 90,顶值= 190;底值= 475。所有打印输出将以Letter 8 1/2 by 11尺寸纸张打印成肖像样式。

我能够在图像顶部的任何位置显示水印,但我希望将它放在下面。当我调整y坐标并恰好位于图像下方时,当我打印时,我认为它位于打印区域之外,因此水印不会打印在页面上(它只显示图像)。

我很欣赏任何人在这方面的帮助,因为我在这方面一直在绞尽脑汁,没有运气。

回答

0

您不打印图像下方的文字吗?我想你想在y = Image.Height + e.MarginBounds.Top和x = e.MarginBounds.Left开始打印,并且x = e.MarginBounds.Left

这将在页边距中的图像下面左对齐地打印出您的标签。

更新:这工作:

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); 
+0

是的,我想打印的图像下方的文本。我会尝试你的建议。谢谢。 –

+0

我的意思是在下面(后面)。除非你的图像是透明的,否则不会让文字显示出来。尝试在打印图像后添加文字 –

+0

图像不透明。我尝试了你的建议,但是,这没有奏效(水印没有在打印出来的地方找到)。我不相信我已经尝试在图像打印后添加文本。值得一试... –