im trying to load some rectangles as "layers" of the form, i loaded an image as also a layer of the form, but the problem with those rectangles is that they "overlay" the image, erasing portions of it, i want them to be seen as boxes of the image to display info, and i also want to be able to overlay a rectangle onto another rectangle without erasing eachother.
here is the class of the rectangle
namespace Imagen_capas
{
class rectangulotransp : UserControl
{
public Pen pen11;
private Rectangle Myrectangle;
public rectangulotransp(int x,int y,int alto, int ancho, Rectangle tamacontrol)
{
Size = tamacontrol.Size;
Location = tamacontrol.Location;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
Myrectangle = new Rectangle(x, y, alto, ancho);
pen11 = nuevopen();
}
private Pen nuevopen()
{
Pen mypen1 = new Pen(Color.Red);
return mypen1;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawRectangle(pen11,Myrectangle);
base.OnPaint(e);
}
}
}
And the code of the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Imagen_capas
{
public partial class Form1 : Form
{
rectangulotransp rect;
rectangulotransp rect2;
public Form1()
{
InitializeComponent();
rect = new rectangulotransp(50, 14, 500, 100,new Rectangle(0,0,Width+400,Height));
// this.Controls.Add(rect);
rect2 = new rectangulotransp(0, 50, 20, 100,new Rectangle(20,50,Width,Height));
this.Controls.Add(rect2);
this.Controls.Add(rect);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I think the problem is that when you set Transparent background, the rectangle is using the Form Backcolor and it seems to you that is erasing the other rectangle.
Maybe you could try painting your transparent rectangle as 4 lines instead of a rectangle.
Hope it helps you
my friends, i solved it in this way.
I created a class for the rectangle, and then another class for a control, in which i painted all the rectangles just like you told me, then the image, and then i added it as a layer of the form using the controls.add, in that way i was also able to put an image under it without being erased by the rectangles.
thanks!!
Related
I have two winforms in my application. One of the forms has a picturebox with a jpg loaded of our building plan. The main form has code that does facial recognition identifying people coming into certain areas. I have been asked to modify this program to show an identified individual's location on the building plan. I have a database that has all the X,Y coordinates of the locations that should map to the building plan image. I have looked around and tried to find some code that will draw a circle on the map at the X,Y coordinates as the person progresses through areas of the building by erasing all the existing circles and updating this new one. So on the map form I put in the following code:
public void DrawCircle(int x, int y)
{
Graphics gf = pictureBox1.CreateGraphics();
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 400, 400));
pictureBox1.Refresh();
}
Then from the update method (right now a button click for testing) on the main form I call this method on the map form. The method gets called, but the circle doesn't show up on the form. I have tried both Refresh and Invalidate and neither method seems to draw the circle on the image.
I haven't done winforms development for years, so I'm sure I am missing some plumbing somewhere. Here is the code on the mainform:
LocationMap map = new LocationMap();
public Form1()
{
InitializeComponent();
//set up signalR
UserName = "MovementHub1";
ConnectAsync();
//show the map screen
map.Show();
map.WindowState = FormWindowState.Maximized;
...
Then in a click event (for testing right now) I have this code:
private void button2_Click(object sender, EventArgs e)
{
map.DrawCircle(340, 258);
}
Once I get the circle drawn on the other form, then I will remove the code from the click event and move it another event that does the updating on the location. If it's possible, I would like to put a label by the circle that has the person's name. Right now this is a proof of concept, I just need help getting the circle on the form to start with.
Thanks.
I tried It out by myself and came up with that:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StackoverflowHelp
{
public partial class Form1 : Form
{
Form2 form = new Form2();
public Form1()
{
InitializeComponent();
form.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
form.DrawCircle(100, 100);
}
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StackoverflowHelp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
DrawCircle(10, 10);
}
public void DrawCircle(int x, int y)
{
Graphics gf = Graphics.FromImage(pictureBox1.Image);
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 20, 20));
gf.Dispose();
pictureBox1.Refresh();
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
Instead of calling CreateGraphics() on the picturebox I created the graphics object using the current image.
I want to draw a cross into a picturebox. In the next step I want to move the cross based on my Data.
Which is the Thumbsticks position. I want it to appear similar to the figure below.
Here is my Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SlimDX;
using SlimDX.XInput;
namespace Controller_Test
{
public partial class Form1 : Form
{
GamepadState Controller = new GamepadState(UserIndex.One);
Graphics drawArea;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
drawArea = pictureBox1.CreateGraphics();
}
private void timer1_Tick(object sender, EventArgs e)
{
Controller.Update();
var LeftStick = Controller.LeftStick;
float LeftStickX = LeftStick.Position.X;
float LeftStickY = LeftStick.Position.Y;
Pen blackPen = new Pen(Color.Black);
// horizontal Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
// vertical Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
}
}
public class GamepadState
{Get Postiton......}
}
My problem now is, if I move the Thumbstick every time a new cross is drawn but the old one is still in the
Picturebox like in the figure below. If i use the pictureBox1.Invalidate():/pictureBox1.Refresh(); Funtcion
the cross becomes invisible and is not displayed anymore. The interval of the timer is on 1.
What could i change in order to get the code to work?
I’m trying to fit a lot of rectangles into a Bitmap, which will be displayed in a picturebox. In my real code, I figure out the total width and height of a rectangle that can encompass all them, and then I divide that by the size of the Bitmap, to get my scaling factor. The problem is I can’t figure out how to perform the scaling. The code below is a simple version of what I need to do.
Please keep in mind that I cannot rely on the picturebox’s scaling abilities (stretch), and I don’t want to simply apply the scale to the width and height of all the rectangles, because in my real code it wouldn’t work very well. I need a way to shrink it down in Graphics. It is important the Bitmap stays the same size that it is (300 X 300). Thanks. The below code is what I've gotten so far, but nothing changes with the size.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap BM = new System.Drawing.Bitmap(300, 300);
Pen PenTest = new System.Drawing.Pen(Brushes.Red);
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics GR = Graphics.FromImage(BM))
{
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
// I need a scale of 0.60 in this example, because 300/500 = .6
GR.ScaleTransform(.6F, .6F);//Doesn't Work. No Change at all in the size of the rectangle.
}
pictureBox1.Image = BM;
}
}
}
Graphics.ScaleTransform performs the transformation but it does not draw anything.
You would need to then draw a rectangle after performing the transform on the graphics object:
using (Graphics GR = Graphics.FromImage(BM))
{
// ....
GR.ScaleTransform(.6F, .6F);
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
}
Question: How do i draw a rectangle on a Panel, rather than a form. Here is what my code looks like:
/*
* based on a some flags i determine which shape i want to draw.
* All shapes are stored in a list. I loop through the list
* and call each shape specific draw method - as shown below:.
*
*/
namespace myDrawProgram
{
private void panelArea_Paint(object sender, PaintEventArgs e)
{
if (drawWithPaint == true)
{
Pen p = new Pen(Color.Blue);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (IsShapeRectangle == true)
{
e.Graphics.DrawRectangle(p, rect);
}
else if (IsShapeCircle == true)
{
e.Graphics.DrawEllipse(p, rect);
}
}
foreach (Shapes shape in listOfShapes)
{
shape.Draw(e.Graphics);
}
}
}
/*
* In another file i have my class which deals with
* drawing rectangles. It is as follows:
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SETPaint;
namespace myDrawProgram
{
class TheRectangles : Shapes
{
public Rectangle MyRect { set; get; }
public TheRectangles(Rectangle rect, Color colour, Color boarderColour, Int32 brushThickness)
: base(colour, boarderColour, brushThickness)
{
MyRect = rect;
}
public override void Draw(Graphics g)
{
base.Draw(g);
g.FillRectangle(new SolidBrush(Shapes.c), MyRect);
g.DrawRectangle(new Pen(bc, MyBrushThickness), MyRect);
}
}
}
i'm assuming i need to do something like this:
using (Graphics g = this.panel1.CreateGraphics()) {}
I'm just not sure how to implement this with regards to my code...
It sounds like you haven't hooked up the paint event of the panel:
panelArea.Paint += new PaintEventHandler(panelArea_Paint);
If panelArea is the name of your form, then just change it to your panel:
panel1.Paint += new PaintEventHandler(panel1_Paint);
and then move your painting logic to that method:
private void panel1_Paint(object sender, PaintEventArgs e) {
// the rest of your drawing
}
It looks like the parent (form?) is responsible for drawing each of its controls.
I would not do it that way.
If you just need a control that draws shapes (and doesn't necessarily need other behavior of a Panel), I would just create a User Control that has a property indicating what shape to draw and make it responsible for its own rendering.
If you do need the behavior of a panel, you can subclass Panel and implement the drawing behavior in your subclassed control. Again, that makes the control responsible for its own rendering.
For info on user drawn controls see
http://msdn.microsoft.com/en-us/library/b818z6z6(v=vs.71).aspx
I was looking about some GDI tutorial but everything I have found so far works with OnPaint method, which passes Paintarguments to Graphics. I have not found how to start from scratch, I mean how to use Graphics class itself?
This is the whole code I have treid that just doesnt work for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Pen pen= new Pen(Color.Red, 3);
Graphics g;
g = this.CreateGraphics();
g.DrawEllipse(pen, 150, 150, 100, 100);
}
}
}
It just doesnt do anything. I tried it in new form, nothing.
Thank you in advance!
The code is probably OK, it is drawing a ellipse as you are hoping for. However, after the Load event, there will be a PaintBackground event and a PaintEvent as the form is displayed. The PaintBackground will, by default, erase the contents of the control, effectively removing the ellipse you've just drawn.
Painting is a two stage process:
for each (region in set of regions that need updating)
{
PaintBackground (region)
Paint (region)
}
The window manager only redraws the parts of the control that require updating, if the contents of the control haven't changed or no user action has altered the control's visibility then no painting is done.
So, why do you want to draw the ellipse in the Load method? Usually, you only want to draw something when something needs to be drawn, and your form is told when something needs drawing in the PaintBackground and Paint events.
Are you worried about flickering? Or is it a speed issue? Ellipses are quick to draw. Flickering, however, is harder to fix. You need to create a bitmap, draw to the bitmap and blit the bitmap to the control during the Paint event. Also, make the PaintBackground event do nothing - no erasing the control, it's the erasing that causes the flicker.
EDIT: An example, I'm using DevStudio 2005 here.
Create a new C# winform application.
In Form1.cs add the following:
protected override void OnPaintBackground (PaintEventArgs e)
{
// do nothing! prevents flicker
}
protected override void OnPaint (PaintEventArgs e)
{
e.Graphics.FillRectangle (new SolidBrush (BackColor), e.ClipRectangle);
Point
mouse = PointToClient (MousePosition);
e.Graphics.DrawEllipse (new Pen (ForeColor), new Rectangle (mouse.X - 20, mouse.Y - 10, 40, 20));
}
protected override void OnMouseMove (MouseEventArgs e)
{
base.OnMouseMove (e);
Invalidate ();
}
Compile and run.