Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends..
i am planning to write something on a picture box.in the page load event i am able to do that.
C#
Graphics g = Graphics.FromImage(pictureBox1.Image);
Font font = new Font("Courier New", 6);

payagainst = "PARADISE TRADING, CONT &  REAL ESTATE";
amount = 3300;
g.DrawString(payagainst, font, new SolidBrush(Color.Black), pagainstX, pagainstY);

My requirement is there is two textbox in the form ie to mention the the X and Y coordinates of the newly drawn label. If the user changes the values in the textbox position of the label has to be changed according to the values given in the text box. in the text box leave event i have written the code as follows. but its not working. whats the correct method to achieve this.
C#
private void txtpaX_Leave(object sender, EventArgs e)
       {
           if (flag != 0)
           {
               Graphics gs = Graphics.FromImage(pictureBox1.Image);
               Font font = new Font("Courier New", 6);
               payagainst = "PARADISE TRADING, CONT &  REAL ESTATE";
               amount = 3300;
               gs.DrawString(payagainst, font, new SolidBrush(Color.Black), float.Parse(txtpaX.Text), float.Parse(txtpaY.Text));
               flag = 1;
           }
       }
Posted
Comments
BillWoodruff 15-Oct-14 0:36am    
Are you drawing text on a PictureBox because the PictureBox hosts an image ?

If you follow Dave K.'s advice below (which I hope you do), and use a Panel, you can set the BackgroundImage of the Panel to the image you want.

1 solution

First, creating that SolidBrush and then not Dispoing it is a good way to run the system out of resources and crash it.

Next, get rid of that PictureBox. You don't need it and you're just complicating your drawing code for no reason. Replace it with a Panel instead.

In the Panels Paint event, put your drawing code. You don't need the Image stuff. The PaintEventArgs will give you a Graphics object to use.

Lastly, create your brush at the class level and hold onto it until your app exits. When that happens, Dispose the Brush.

Oh, and in the Leave event for your textbox, just call the Invalidate method on the Panel and it'll redraw itself with your drawing code.
 
Share this answer
 
Comments
BillWoodruff 15-Oct-14 0:34am    
+5
jinesh sam 15-Oct-14 1:45am    
Sir why i am using picture box is to identify where the string has to be placed. My plan is to print the cheque so i placed a blank cheque copy on the picture box.The date,amount position has to customize according to the user need. so i place two text box to change the cordinates values.
as per your suggestion i placed the code in the paint event of the picture box and the leave event of the textbox i call Invalidate method and its work fine
but now the problem is its not clearing the previous drawn string
Dave Kreskowiak 15-Oct-14 8:11am    
You STILL don't need the PictureBox control. You can paint the image yourself too.
jinesh sam 15-Oct-14 10:06am    
can please show a small demo
jinesh sam 15-Oct-14 10:07am    
Did you mean like this..
Graphics gs = Graphics.FromImage(panel1.BackgroundImage);
Image img = Image.FromFile(Application.StartupPath + "\\chq2.jpg");
Point loc = new Point(10, 10);
gs.DrawImage(img, 100, 100, (float)(panel1.Width), (float)(panel1.Height));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900