PictureBox clearing when dragged out of screen boundries in Visual Studio 2013 - c#

It's me again, with a weird problem. I created a code which reads xml file and uses g.Drawstring function to get my sorted xml text to pictureBox1. So I got advanced and made the pictureBox scrollable and noticed the problem. When the text in pictureBox was off screen (invisible), when I scrolled back to it, it wasn't there anymore. Same happens if I just take my application and drag it off screen, so that text disappears, the text gets deleted, but if I load my program again (browse the xml file again) it refreshes, but that doesn't help, becuase I need to make it so that the text is always there.
Here is a part of my code, I hope it helps:
private void DrawInPictureBox(int index)
{
pictureBox1.Refresh();
using (XmlReader reader = XmlReader.Create(openFileDialog1.FileNames.ElementAt(index)))
{
y = 0;
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.Name == "Something")
{
string Dat = reader.GetAttribute("Dat");
Graphics g = pictureBox1.CreateGraphics();
if (reader.Read())
{
string m = reader.Value.Trim();
g.DrawString(m, new Font("Arial", 10), Brushes.Black, 0, y * 15 + 20);
g.DrawString(Dat + "ok", new Font("Arial", 10), Brushes.Black, 150, y * 15 + 20);
y = y + 1;
}}}}}}
The pictureBox has a panel underneath it, so it is scrollable, but even if there is no panel the text gets deleted when taken off screen.
Thank you!

Some points:
It would be best if you can subscribe to PictureBox.OnPaint event and use the e.Graphics to draw your XML elements
With regards to scroll, there is a possibility that the location of your scrollable control is moving in one direction only which makes it invisible from the screen.
Try to debug the location of the scroll position by writing the values of scroll-X and scroll-Y position in output window via Console.WriteLine() or System.Debug.WriteLine(), chances are there are negative values, or out of boundary values. (In your case the value of y in the g.DrawString())

Related

How to draw in picturebox so drawing doesn't disappear? [duplicate]

I have a Tab Control with two (2) Tabs. The Tab 1 does a drawing in a picture box (the picture box is optional, I can draw directly to the tab) using Graphics Addline. The second tab opens a web browser. Everything is working fine. I can make the drawing in the first tab but when I switch to the second tab and return to the first tab, the drawing disappear and if I return to tab 2 I can see what I was watching in the web browser. I need to kept the drawing in the tab 1 so when I return to it I can see it. Here is the code I'm using to draw in the tab 1:
private void DataLoaded(ref string strFileName) //strFileName has the data
need for the drawing.
{
Graphics g = this.pictureBox1.CreateGraphics();
Pen black = new Pen(Color.Black, 5);
Pen green = new Pen(Color.Green, 5);
List<double> xpoints = new List<double>();
List<double> ypoints = new List<double>();
g.TranslateTransform(350, 350);
g.DrawLine(green, new Point(Convert.ToInt32(X1), Convert.ToInt32(Y1)), new
Point(Convert.ToInt32(X2), Convert.ToInt32(Y2)));
for (int i = 2; i < xpoints.Count(); i++){
g.DrawLine(black, new Point(Convert.ToInt32(X1),
Convert.ToInt32(Y1)), new Point(Convert.ToInt32(X2),
Convert.ToInt32(Y2)));
X1 = X2;
Y1 = Y2;
X2 = xpoints[i];
Y2 = ypoints[i];
}// end of for
}
I even tried to do the drawing using the painteventarg but its not working at all. It helped me a bit because when I change back to the tab 1 and move the mouse over the tab it draws again the lines. Can anyone help me with this?? I even tried using this.picturebox1.Invalidate() but nothing. Like I said, what I need is: Preserve the drawing in tab 1 after switching to tab 2 so when I returned to tab 1 the lines are there. Thanks in advance for the help!!!.
Its done, I just used a Bitmap to draw to it and the set the picturebox image with the bitmap.
The code I used is as follows:
Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(image);
// In between all the code required for extracting the data and do the draw.
pictureBox1.Image = image;
Thanks anyway to whoever saw my question and try to answer it.

How can i calculate the distance from the top of pictureBox1 and almost the top of form1?

I have a label with text inside i can change the label size or the label font size each time and check many times but maybe there is a way to calculate it:
label18.Text = "מכם מזג האוויר איננו פעיל כרגע";
This is how i see the text now:
The text in red is in hebrew this is the text i want to change it's size and also to put it in the middle according to the picturebox1 top not on the left like it is now.
And i did a black circle just to show what i mean by " the distance from the top of pictureBox1 and almost the top of form1 ".
I mean this gray area from the above the pictureBox1 and the form1 white area on the top only this gray area i want to make the text in this height and in the middle.
How can i calculate this two values ?
I tried this but it's not in the exact middle:
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
You don't need graphics or to measure anything. Just set in designer text align = middlecenter and autosize = true
label18.Location = new Point(pictureBox1.Location.X + (pictureBox1.Width / 2 - label18.Width / 2,
pictureBox1.Location.Y - label18.Height);
To center a label you need it get it actual size, then to center it using another control use some simple math to get the coordinate for the control (see below Example 1). I don't know what control the grey bar is but you could center in that by using the size.Width property and doing the same type of calculation.
If you want to fill the grey bar I have added Example 2.
Example 1:
private void CenterLabel()
{
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(label18.Text, label18.Font);
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = pictureBox1.Top - 20;
}
Example 2
private void CenterLabel()
{
int fontHeightPixels = (int)(greyBar.Height * .85);
Font font = new System.Drawing.Font("Arial", fontHeightPixels, FontStyle.Regular, GraphicsUnit.Pixel);
string text = "I am centered";
//get the size of the text (you could do this before hand if needed)
SizeF size = label18.CreateGraphics().MeasureString(text, font);
label18.Font = font;
label18.Text = text;
//center over picture box control and slightly above
label18.Left = (pictureBox1.Width / 2) - (((int)size.Width) / 2) + pictureBox1.Left;
label18.Top = (greyBar.Height / 2) - (((int)size.Height) / 2) + greyBar.Top;
}
This is relatively simple with Windows forms:
Dock your label to the top of the form by setting the appropriate property in the Forms designer. The property you want to set is Dock and it should be set to Top.
Change the label's AutoSize property to false.
Change the label's height as desired.
Change the label's TextAlign property to MiddleCentre.
That should do it.
There's more then one way to achieve this goal.
I would suggest the following:
First calculate the width of the picturebox (picturebox.Width)
Find the coordinates on the form where the picturebox resides (picturebox.Location) property of the picturebox)
Then you change the location of your label control --> to Label.Location.X = (picturebox.Width /2) and Label.Location.Y = picturebox.Location.Y ==> now you have the label correctly placed .
Next Set the Height of the Label Control to the Top(distance between the edge of the form and picturebox) value of the Picturebox.
No visual studion from where i am typing so cannot do full code example.
You're done.

How to convert my Graphics.Drawline drawing on panel, to saved image?

I have a panel called "canvas". It is transparent. So the background is from the form image, which is dark blue. This shows in the panel or canvas.
When I save the canvas to image, it saves the background, but not what I have drawn thereon, my drawline pen is yellow. And I can see it drawing on the panel. But when I save it... there are not yellow lines in the image.
What am I missing? Where are my yellow lines?
I am running this with my timer tick... to get the view to update.
This tracks the position of a CNC type machine. Gives a visual of where
the machine is in relation to Zero.
My ultimate goal, is to have a "viewport" that is zoomable, thus getting it
into a image, for easy resizing, and displaying in a pictureBox, which will
handle the stretched image and center it automatically?
I have read some complex solutions, but I am after the simple ones.
Any help would be appreciated. Sincerely,
private void VMoveNow()//Draw on panel called "canvas".
{
double a = GV.MYa * -1; //Change Direction of Y Drawing.
xc = Convert.ToInt32(GV.MXa) + (canvas.Width / 2);
yc = Convert.ToInt32(a) + (canvas.Height / 2);
g = canvas.CreateGraphics();
g.DrawLine(p, x, y, xc, yc);
x = xc;
y = yc;
g.Dispose();
}
private void SaveBMP()
{
try
{
Bitmap mybmp = new Bitmap(canvas.Width, canvas.Height);
canvas.DrawToBitmap(mybmp, canvas.Bounds);
mybmp.Save("C:\\myimage.bmp");
}
catch
{
return;
}
}
Thanks for looking.
I answered my own problem, after several attempts... I have figured out that I can scale my var's used for this drawings... and the size of the Drawline will be scale as a result.
So I now have scaling of the Drawline drawing, in a panel, with no picture or picture box needed. Does what I wished.
Setting the Pen width to -1 takes care of it resizing.

How to kept a graphic drawing in a picturebox on a tab control after switching to tab 2 and return to tab 1?

I have a Tab Control with two (2) Tabs. The Tab 1 does a drawing in a picture box (the picture box is optional, I can draw directly to the tab) using Graphics Addline. The second tab opens a web browser. Everything is working fine. I can make the drawing in the first tab but when I switch to the second tab and return to the first tab, the drawing disappear and if I return to tab 2 I can see what I was watching in the web browser. I need to kept the drawing in the tab 1 so when I return to it I can see it. Here is the code I'm using to draw in the tab 1:
private void DataLoaded(ref string strFileName) //strFileName has the data
need for the drawing.
{
Graphics g = this.pictureBox1.CreateGraphics();
Pen black = new Pen(Color.Black, 5);
Pen green = new Pen(Color.Green, 5);
List<double> xpoints = new List<double>();
List<double> ypoints = new List<double>();
g.TranslateTransform(350, 350);
g.DrawLine(green, new Point(Convert.ToInt32(X1), Convert.ToInt32(Y1)), new
Point(Convert.ToInt32(X2), Convert.ToInt32(Y2)));
for (int i = 2; i < xpoints.Count(); i++){
g.DrawLine(black, new Point(Convert.ToInt32(X1),
Convert.ToInt32(Y1)), new Point(Convert.ToInt32(X2),
Convert.ToInt32(Y2)));
X1 = X2;
Y1 = Y2;
X2 = xpoints[i];
Y2 = ypoints[i];
}// end of for
}
I even tried to do the drawing using the painteventarg but its not working at all. It helped me a bit because when I change back to the tab 1 and move the mouse over the tab it draws again the lines. Can anyone help me with this?? I even tried using this.picturebox1.Invalidate() but nothing. Like I said, what I need is: Preserve the drawing in tab 1 after switching to tab 2 so when I returned to tab 1 the lines are there. Thanks in advance for the help!!!.
Its done, I just used a Bitmap to draw to it and the set the picturebox image with the bitmap.
The code I used is as follows:
Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(image);
// In between all the code required for extracting the data and do the draw.
pictureBox1.Image = image;
Thanks anyway to whoever saw my question and try to answer it.

How can I move a PictureBox from one panel to another?

I have a Picture Box in a panel and I need to see it move from this panel to another one upon user click. To give you a better picture, I have a connect four game where I have a chip or whatever you call it moving with the mouse on top of the connect four grid and I need it to go in the grid and obviously stay in its place. This is the code I have right now. The chip seems to go down but never stay there
Graphics g = grid.CreateGraphics();
grid.Controls.Add(picBox);
for (int i = 0; i < newYloc; i++)
{
picBox.Location = new Point(newXloc, picBox.Top + 1);
// moves the chip by 1 down each iteration
picBox.Show();
}
if (playerNo == 1) g.DrawImage(red, newXloc, newYloc, 65, 65);
else g.DrawImage(gold, newXloc, newYloc, 65, 65);
where grid is the panel where I have the connect four grid, picBox is the Picture Box which before this piece of code resides in another panel.

Categories