Problems printing text on top of img

  • Thread starter Thread starter mphanke
  • Start date Start date
M

mphanke

Hi,

I'm desperately trying to print a string on top of an image, but for
whatever reason the images allways stays on top of my canvas! Is there
some trick how to implement things?

I call

ev.graphics.drawimage(img, rect);
ev.graphics.drawstring("mystring", ..., rect, ...);

I can't really deal with this behavoir....


Martin
 
Hi Martin,

The code snipped you refered in your post should work fine. Just that we
need to ensure the printing happens in the right order, like the image
first and the text next in our case.

Try the following code in a sample form button click event, with a valid
image bitmap. The text should appear on top of the image.

private void button1_Click(object sender, System.EventArgs e)
{
Graphics g = CreateGraphics();
Rectangle rect = new Rectangle(10, 10, 100, 100);
Bitmap b= new Bitmap(@"SampleImage.jpg");
g.DrawImage(b, rect);
g.DrawString("TEXT", new Font("Impact", 20), new SolidBrush(Color.Red),
new Point(10, 10));
}

I doubt if some other event causes invalidation of the canvas and the
text is printed before the image, just a thought. Have a relook at the
paint event of the canvas.

HTH,
APG.
 
Hi,

this is exactly what I do! Just one thing is different:

Rectangle rect = new Rectangle(10, 10, 100, 100);
Rectangle rectText = new Rectangle(40, 40, 300, 10);
StringFormat sf = new Stringformat();
Bitmap b= new Bitmap(@"SampleImage.jpg");
g.DrawImage(b, rect);
g.DrawString("TEXT", new Font("Impact", 20), new SolidBrush(Color.Red),
rectText, sf);

This really confuses me! I just don't get the point what happens there.
It is the same with your code....

Could that be some type of a driver problem??
Using latest HP driver for 5100 series, but the same thing happens on
our Lexmark ColorLaser. So I excluded this posibility!

Regards,

Martin
 
Hi Martin,

Based on my understanding, you want to use GDI+ to draw image, then print
them out.

I have tried your code, I think there is not much problem about it. Only
that I think the height you set for your rectText is too small, you'd
better set it to 100, not 10.(Or you can only display part of the text)

How do you print your drawing picture? I think you may use PrintDocument
class. On machine, I have used PrintDocument with your code snippet to
print out the image, the text really on top of the picture. So I do not
know why your problem arise.

I think you may first try to use PrintPreviewDialog to preview the
pre-print document, if the print preview works well, I think the problem
should be your printer. The code snippet like this:

PrintDocument pd=null;
private void button1_Click(object sender, System.EventArgs e)
{
try
{

Font printFont = new Font("Arial", 10);
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Rectangle rect = new Rectangle(10, 10, 100, 100);
Rectangle rectText = new Rectangle(40, 40, 300, 100);
StringFormat sf = new StringFormat();
Bitmap b= new Bitmap("D:\\1_p1.JPG");
ev.Graphics.DrawImage(b, rect);
ev.Graphics.DrawString("TEXT", new Font("Impact", 20), new
SolidBrush(Color.Red), rectText, sf);
}

private void button2_Click(object sender, System.EventArgs e)
{
PrintPreviewDialog dlg = new PrintPreviewDialog() ;
dlg.Document=pd;
dlg.Show();
}

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

this is what bothers me, I don't get the point! In the preview the image
is above the text and it stays there. I guess I have to check my
calculations somewhat more close...

BTW.:The text I use is Arial 5 pt, so 10 is enough for displaying it.

Thanks for your help so far,

Martin
 
Hi Martin,

Oh, I also think it is strange. I suggest you save your drawed memory
bitmap to the disk to see if the problem also existed.

If you still can not figure out the problem, can you find a way to
reproduce this problem? Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Martin,

Is your problem resolved? Do you still have concern on this issue?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top