C
C#8mo ago
ʙʀʊʍɨ

✅ How can I draw a line between labels?

I have to draw lines between city labels. But when the form loads, the labels disappear and there is no line. What can I do?
No description
No description
66 Replies
TheRanger
TheRanger8mo ago
I think you need to get the image's graphics, not create a new one
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(......
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(......
}
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
i just noticed this error
No description
TheRanger
TheRanger8mo ago
Those are warnings, not errors!
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
oh yea warning sorry :when: either way, im thinking that maybe i have to make the image some kind of grid so that i can give x,y,a,b some coordinates but idrk how to do that
TheRanger
TheRanger8mo ago
a,b ?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
for the two points i was thinking i couldn't use X and Y cuz they are the coordinates for the first point
TheRanger
TheRanger8mo ago
What do you see now in your program?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
in the form?
TheRanger
TheRanger8mo ago
yes
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
its just the map the picture the labels disappeared and there's no line
TheRanger
TheRanger8mo ago
disappeared? so they appeared at some point before they disappeared?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
in the form they are there but once it runs they disappear
TheRanger
TheRanger8mo ago
you mean the form builder?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
its the first time im working with graphics so idk what to do the designer yea
TheRanger
TheRanger8mo ago
the image is probably hiding the labels try calling .BringToFront() Method on the labels
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
I'll try
TheRanger
TheRanger8mo ago
Graphics also has a method called DrawString You can draw text directly into the image also it looks to me like you're resetting the label's location to 0, 0 which is the top left coordinate of the form assuming x, y, a, b values are 0
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
ooh yea that makes sense the warnings do say they assign the value 0 yup they're in the top left corner so the first problem would be the location on the axis... alright so i managed to make them appear in the desired positions just copied their coordinates from the designer but the line still isn't there
TheRanger
TheRanger8mo ago
what code are you using to try to draw a line?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
i have to make it so the line appears when i press the button
TheRanger
TheRanger8mo ago
do you know what Form2_Paint does? or when does it get called?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
last time i used it it made a drawing when the form loaded
TheRanger
TheRanger8mo ago
well, put this code in the button1_click then
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(......
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(......
}
also according to this code ur trying to draw the line on the form itself not on the image
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
ooh well not even with this change it doesnt show but i dont get any errors or warnings
TheRanger
TheRanger8mo ago
show your new code
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
im still thinking it might be sum related to those integers
TheRanger
TheRanger8mo ago
whats ur pictureBox called, is it pictureBox1 ?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
yea
TheRanger
TheRanger8mo ago
you sure you clicked the correct button?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
yea well now i used left and top for the labels i had used .Location.X and .Location.Y before
TheRanger
TheRanger8mo ago
i think you need to call pictureBox1.Invalidate(); to redraw the picturebox or .Refresh()
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
yup invalidate worked but the lines is not where it should be
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
it should be between these two labels
TheRanger
TheRanger8mo ago
Yeah the labels are using the coordinates of the form while the line is using the coordinates of the image itself 1 minute
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
maybe if i make the form background the image?
TheRanger
TheRanger8mo ago
there is a better way
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
lets hear it
TheRanger
TheRanger8mo ago
PictureBox has a method called PointToClient where it translates a coordinate in the image into the form's coordinate you could try using that
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
how do i use it :when: i genuinely never heard of this method
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
like this?
TheRanger
TheRanger8mo ago
use pictureBox1 instead of this
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
this refers to the form right?
TheRanger
TheRanger8mo ago
yes
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
didnt work its even worse now
TheRanger
TheRanger8mo ago
what do u mean?
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
TheRanger
TheRanger8mo ago
yeah well i was gonna tell you to actually use the image coordinates for the labels
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
so... no more x and y? or what
TheRanger
TheRanger8mo ago
Point contains x and y you can actually change x and y into Point to be stored into a single variable this code might work
private Point ConvertLabelPositionToImageCoordinates(Label label)
{
// Ensure the PictureBox has an image
if (pictureBox1.Image == null)
return Point.Empty; // or throw an exception, depending on your requirements

// Calculate the scaling factors for X and Y dimensions
float scaleX = (float)pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float scaleY = (float)pictureBox1.Image.Height / pictureBox1.ClientSize.Height;

// Translate the label's position from the form coordinates to the PictureBox coordinates
Point labelLocationPictureBox = pictureBox1.PointToClient(label.Parent.PointToScreen(label.Location));

// Translate the PictureBox coordinates to the image coordinates
int imageX = (int)(labelLocationPictureBox.X * scaleX);
int imageY = (int)(labelLocationPictureBox.Y * scaleY);

return new Point(imageX, imageY);
}
private Point ConvertLabelPositionToImageCoordinates(Label label)
{
// Ensure the PictureBox has an image
if (pictureBox1.Image == null)
return Point.Empty; // or throw an exception, depending on your requirements

// Calculate the scaling factors for X and Y dimensions
float scaleX = (float)pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float scaleY = (float)pictureBox1.Image.Height / pictureBox1.ClientSize.Height;

// Translate the label's position from the form coordinates to the PictureBox coordinates
Point labelLocationPictureBox = pictureBox1.PointToClient(label.Parent.PointToScreen(label.Location));

// Translate the PictureBox coordinates to the image coordinates
int imageX = (int)(labelLocationPictureBox.X * scaleX);
int imageY = (int)(labelLocationPictureBox.Y * scaleY);

return new Point(imageX, imageY);
}
Note i can't test, so i have to rely on your tests
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
ill see what i can do
TheRanger
TheRanger8mo ago
code updated.. your label positions should be as it was as it was in this image and use this method to convert the label's position into the position in the image
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
alright lemme try
TheRanger
TheRanger8mo ago
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Pen p = new Pen(Color.Black, 3);
Point from = ConvertLabelPositionToImageCoordinates(constLbl);
Point to = ConvertLabelPositionToImageCoordinates(sochiLbl);
g.DrawLine(p, from, to);
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Pen p = new Pen(Color.Black, 3);
Point from = ConvertLabelPositionToImageCoordinates(constLbl);
Point to = ConvertLabelPositionToImageCoordinates(sochiLbl);
g.DrawLine(p, from, to);
}
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
IT WORKED gad dayum
TheRanger
TheRanger8mo ago
show 😄
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
No description
TheRanger
TheRanger8mo ago
Nice
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
thing is this saturday im gonna go to some kind of contest and idrk what subjects imma have but im gonna have to make an app like this not necesasrily like this one exactly but how can I remember all this
TheRanger
TheRanger8mo ago
$practice
ʙʀʊʍɨ
ʙʀʊʍɨOP8mo ago
true either way thank you so so much
Want results from more Discord servers?
Add your server