How to print out a pointf in winforms c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to programming and I want to print a dot in the windows form application.
What is the code if I already have a PointF that is assigned with the x & y?
Thank you very much

You can try using FillRectangle of a Graphics object to fill the rectangle with dimension of 1x1 (looks like a point), because the Graphics object doesn't have any method like SetPixel:
public void SetPixel(PointF p, Color c, Graphics g){
using(Brush brush = new SolidBrush(c)){
e.Graphics.FillRectangle(brush, new RectangleF(p, new Size(1, 1)));
}
}
//Paint event handler of your form
private void Form1_Paint(object sender, PaintEventArgs e){
SetPixel(yourPoint, Color.Red, e.Graphics);
}

Isaac, not a very detailed question, but from what I got is that you have a windows form that you want to draw a "POINT" (1px X 1px dot)? Well its pretty easy if you are using windows forms.
to go the designer view of your form
right click on the background of the form while the form is selected
click properties
click on the events tab (the lightning bolt) of the properties tab
scroll down until you see paint event for that form
type in a function name for that event
browse to your source file for your form
using code from below. There are a couple ways to draw a point I use either a 1px line or a filled 1px box
private void paintEvent(object sender, PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
// Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, 50, 50,
51, 51);
g.DrawRectangle(System.Drawing.Pens.Red, 50, 50, 1, 1);
}

Related

How to design or somehow get a custom WindowBar and grip [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Like in Visual Studio, let's say the ToolBox, it has a blue draggable WindowBar like this:
or like this:
Is there a DLL to get one, or an easy way to make it?
To render some control to look like some system element, like a a grip, you can use a suitable VisualStyleRenderer
As you can see there is a huge number! - Here is how you would add a VisualStyleElement.Rebar.Gripper to a Panel:
private void panel1_Paint(object sender, PaintEventArgs e)
{
// any other drawing before..
DrawVisualStyleElementRebarGripper1(e);
}
Here is a typical implementation of the method to call:
public void DrawVisualStyleElementRebarGripper1(PaintEventArgs e)
{
if (VisualStyleRenderer.IsElementDefined(
VisualStyleElement.Rebar.Gripper.Normal))
{
VisualStyleRenderer renderer =
new VisualStyleRenderer(VisualStyleElement.Rebar.GripperVertical.Normal);
Rectangle rectangle1 = new Rectangle(0, 0,
20, (int)e.Graphics.VisibleClipBounds.Height);
renderer.DrawBackground(e.Graphics, rectangle1);
}
//else
// e.Graphics.DrawString("This element is not defined in the current visual style.",
// this.Font, Brushes.Black, new Point(10, 10));
}
Result:
Make sure to call the rendering method after any other paint action so it won't get painted over
Note that are two of them: GripperVertical and Gripper; on my system (W10) they look the same but on other systems they may not!
If you actually want a custom grip style you could paint it with a suitable hatchpattern brush; that would look the same across all systems, which may be what you want. But it would also mean that it will not always integrate with the rest of windows; this solution will always use the style of the current machine.
Update:
If you want to allow dragging the control you can use Vanethrane's answer for the basic functionality. For better UX also make sure to consider these points:
Use all three events, MouseDown, -Move and -Up.
Change the Cursor from Default to Hand and SizeAll
Test if you are in the grip area
Before moving bring the control to the top of the z-order with BringToFront to avoid passing under any other control
In the MouseDown store the current position twice; once for moving and once for restoring in case the final location is invalid
often you want to use a grid to control the final position and...
..often you want to have the control align itself 'magnetically' with the closest neighbour. Use MouseUp to change the final position accordingly..
I suggest to bundle all the functionality into a DraggableControl class.
super easy. here:
create the desired control, name it grip. put these in the mouse down and mouse move methods respectively
private Point Mouselocation;
private void grip_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Mouselocation = e.Location;
}
}
private void grip_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
grip.Left = e.X + grip.Left - Mouselocation.X;
grip.Top = e.Y + grip.Top - Mouselocation.Y;
}
}
note, that this will move a single control. to move the whole form you need to implement this on the form itself

C#, Drawing on button click, not before

I'm fairly new to using C#. I started on monday to be honest and prior to this only had basic MATLAB level of experience, so I'm still some kind of newb scrub. The question I will ask has already been asked, according to my googling skills, but I have yet to find a working solution.
I'm currently writing a program to make the image acquisition from a Vimba camera, and the subsequent image tratment, and that program includes a UI, so a form1. On this form, there is a pictureBoxLiveCamera that shows the live recording from the camera.
What I am trying to do is :
draw two rectangles on top of the pictureBoxLiveCamera, indicating specific zones of the image (THIS WORKS LIKE A CHARM)
have those two rectangles NOT drawn when I launch the program, but when I click a specific drawRectanglesButton (AND HERE I HAVE A PROBLEM)
have that same button hide the rectangles if they are drawn, and then draw them if they are hidden (I'M NOT THERE YET)
I have already explored a lot of similar threads, and this one particularly shaped the way I wrote the code, though it is not working as intended yet.
Here are the protions of my code that are relevant here :
form loading :
// Form loading
private void Form1_Load(object sender, EventArgs e)
{
pictureBoxLiveCamera.Paint += pictureBoxLiveCamera_Paint;
... //Vimba API startup and so on
}
button code :
private void drawRectanglesButton_Click(object sender, EventArgs e)
{
this.Invalidate(); // force Redraw the form
}
rectangles drawing code :
public void pictureBoxLiveCamera_Paint(object sender, PaintEventArgs e)
{
Rectangle ee = new Rectangle(-5, 120, 790, 100);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, ee);
}
Rectangle eee = new Rectangle(-5, 364, 790, 100);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, eee);
}
}
My problem is the following : as it is, when I start the program, the rectangles are already drawn, so the button does nothing.
I'm pretty sure the mistake will be obvious to most of you, but this has been driving me crazy all day.
Thanks a lot !
Trion

Image Drawing with Graphics C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to draw a image in a picture box, and the image is not drawing what is wrong with my code? Thanks
Bitmap b;
private void Form1_Load(object sender, EventArgs e)
{
b = new Bitmap(1000, 100);
pictureBox1.Image = b;
Graphics g = pictureBox1.CreateGraphics();
Rectangle rect = new Rectangle(20, 20, 70, 70);
Image earth = Image.FromFile("" + System.IO.Directory.GetCurrentDirectory() + #"\earth.png");
g.DrawImage(earth,rect);
}
Form.Load fires before anything is drawn to the screen. When the Form is actually displayed for the first time, PictureBox.OnPaint will be called and the control will render itself with its default appearance. To properly draw to a PictureBox, handle its Paint event and do your drawing in the event handler using the supplied Graphics instance. That event will be fired every time any part of the control is redrawn, which will allow you to draw graphics that persist when the control is invalidated. See here for more information.

How to change the origin in Windows Forms Bitmap? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a Bitmap object in C# Windows Forms. The origin (where axis start) in this Bitmap object is located in the top-left corner. How can I change the origin to the bottom-left corner, like in usual graphs? Thank you!
You can flip the Y coordinates in a picture box by using the code in the following answer;
flip coordinates when drawing to control
private void ScaleTransformFloat(PaintEventArgs e)
{
// Begin graphics container
GraphicsContainer containerState = e.Graphics.BeginContainer();
// Flip the Y-Axis
e.Graphics.ScaleTransform(1.0F, -1.0F);
// Translate the drawing area accordingly
e.Graphics.TranslateTransform(0.0F, -(float)Height);
// Whatever you draw now (using this graphics context) will appear as
// though (0,0) were at the bottom left corner
e.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);
// End graphics container
e.Graphics.EndContainer(containerState);
// Other drawing actions here...
}

Winforms multi-layer graphics [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Problem example:
I have a control do draw some geometrical shapes (I use panel).
I have drawn a line.
Now when I move cursor to one of the line tips I want that point to be "highlighted" (a solid circle drawn around it).
And when I move cursor off the point, I want the "highlighting" circle to be erased, but I do not want to redraw the line.
So, technically, I need two layers:
1-st layer to draw my line(s).
2-nd layer to draw/erase highlights.
I do not want the first layer to be redrawn every time when something is drawn/erased in the second layer.
Any suggestions?
Option 1: Nest another Panel in the bottom one. This is good for overlaying graphics including semi-transparency. (Nesting means: panel2.Parent = panel1) You can nest many layers if you want to.
Option 2: Draw into the BackgroundImage of the Panel and use drawing onto the surface for the interactivce stuff.
I noted that you 'don't want to draw the line again'. This is not what you would usually say/do/try when doing graphics. If you are serious about this go for option 2!
See here for the difference of drawing onto a control and into a Bitmap and here for another example of using option 2 to display a cross as a cursor above a Bitmap.
Btw, the two options are not mutually exclusive: You could nest Panels with BackgroundImages and draw interactive stuff on the topmost one..
#TaW pretty much answered it. I set an example:
For static drawing:
private Bitmap myStaticImage;
public Form2()
{
InitializeComponent();
this.myStaticImage = new Bitmap(200, 100);
Graphics g = Graphics.FromImage(this.myStaticImage);
g.DrawLine(Pens.Red, new Point(0, 0), new Point(200, 0));
this.panel1.BackgroundImage = this.myStaticImage;
}
For dynamic drawing:
private void panel1_Paint(object sender, PaintEventArgs e)
{
// Stuff
}
I recommend using a PictureBox or a UserControl with DoubleBuffered property set to true. Panel control is not double buffered and will flicker.

Categories

Resources