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.
Related
UPDATE: Since originally asking this question, I have changed my approach slightly. Rather than drawing using System.Drawing.Graphics, I am hosting a WPF user control with an InkCanvas. That does everything I need it to do. The problem still is that I cannot get the background of the ElementHost to be transparent. I see the same black square I was seeing before.
ORIGINAL QUESTION: I have a C# WinForms application that renders a 3D scene using Ogre3D to a panel in the form using that panel's handle. I am trying to add the ability to draw on top of that scene (imagine Madden drawing over the TV screen) using C#'s System.Drawing.Graphics.
I'm using the BufferedGraphics class to do this. As a test, I'm trying to simply draw a rectangle on top of the 3D scene. Below is a snippet of the code I'm using to set everything up.
namespace TestApp
{
public partial class TestForm
{
private BufferedGraphics graphicsBuffer;
private BufferedGraphicsContext bufferContext = BufferedGraphicsManager.Current;
public TestForm()
{
InitializeComponent();
UpdateGraphicsBuffer();
}
private void UpdateGraphicsBuffer()
{
bufferContext.MaximumBuffer = new Size(panelRender.Width + 1, panelRender.Height + 1);
graphicsBuffer = bufferContext.Allocate(Graphics.FromHwnd(panelRender.Handle), new Rectangle(49, 49, 100, 100));
graphicsBuffer.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}
private void TestForm_Load(object sender, EventArgs e)
{
graphicsBuffer.Graphics.DrawRectangle(new Pen(Color.Red), 50, 50, 50, 50);
}
}
}
I've left out a lot of the proprietary code (there is a call to graphicsBuffer.Render(); in part of that proprietary code) and renamed some stuff but hopefully what I have provided will give you the gist. Also, the 3D scene is also using panelRender.Handle to draw into that panel, and the panelRender.BackColor is black.
In a nutshell, what I am seeing is a chunk of my 3D scene missing (specifically a 100x100 chunk) with the 50x50 red rectangle drawn inside it, as pictured here:
Obviously I don't want to lose the scene that I'm trying to draw on top of. Right now, I'm at a loss as to why this is happening. Is what I'm trying to do just not possible? If any additional information/code is needed, I will be happy to provide it, if possible.
EDIT:
To try and simplify matters, I created a really simple WinForms app that has a single panel and used the code above to recreate the issue. The code-behind for that is here:
using System.Drawing;
using System.Windows.Forms;
namespace DoubleBufferTest
{
public partial class Form1 : Form
{
private BufferedGraphics graphicsBuffer = null;
private BufferedGraphicsContext bufferContext = BufferedGraphicsManager.Current;
public Form1()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
this.UpdateStyles();
InitializeComponent();
UpdateGraphicsBuffer();
}
private void UpdateGraphicsBuffer()
{
bufferContext.MaximumBuffer = new Size(panel1.Width + 1, panel1.Height + 1);
graphicsBuffer = bufferContext.Allocate(Graphics.FromHwnd(panel1.Handle), new Rectangle(10, 10, 50, 50));
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
graphicsBuffer.Graphics.DrawRectangle(new Pen(Color.Red, 3.0f), 20, 20, 10, 10);
graphicsBuffer.Render();
}
}
}
The panel's backcolor is set to transparent. Here is the result:
That black square corresponds to the graphics buffer that is getting allocated by the context. Why it always shows up as black is basically what is confusing me now...
Thank you to #Ron Beyer for answering this for me in a different question I posted.
So I was on the right track with the ElementHost control, but because of the transparency issues, Ron suggested that I try using a new WPF window that I could overlay on top of the application (link). The WPF window's background is set to transparent, so drawing on the InkCanvas results in the desired effect.
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!!
I simulate map navigation and draw generated part of the map on a panel. Since image is flickering I have to use double buffering.
Here's my Panel code:
public class MapPanel : System.Windows.Forms.Panel
{
public MapPanel()
{
DoubleBuffered = true;
ResizeRedraw = true;
}
}
And I have the following method:
public void panelMap_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
g.DrawImage(mapController.GetCurrentMap(), 0, 0, panelMap.Width, panelMap.Height);
}
}
I'm not calling this method. I have the following code in .Designer.cs:
this.panelMap.Paint += new PaintEventHandler(this.panelMap_Paint);
And call Invalidate() in MouseMove. I'm sure that this event occurs, I've checked it. Everything seems to be correct.
And then the image is not drawing. I mean, the panel is empty and seems to be transparent or colored in default control color. However, if I turn double buffering off, the image is properly drawn, but, obviously, it's flickering. Could you help me?
Remove the using statement. You are disposing of the Graphics object before it is used to draw to the screen.
As mentioned, you should also remove the Invalidate() call from the paint method.
public void panelMap_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.DrawImage(mapController.GetCurrentMap(), 0, 0, panelMap.Width, panelMap.Height);
}
You should comment out the following code
//panelMap.Invalidate();
According to MSDN
Invalidates the entire surface of the control and causes the control to be
redrawn.
The control below draws a string in a rectangle. On mouse move there is a hit test on the string rectangle, and the string is redrawn via CreateGraphics. The irritating problem is that the text is not drawn the same as in the Paint handler; it appears to be displaced by about 1 pixel, and the effect is like a bold font. How can I create a graphics object exactly like the one in the Paint handler so the text is drawn the same way? Ordinarily you would invalidate and redraw everything in the Paint event, but I have potentially hundreds of of other drawing items and only want to draw the string. Should I try to do any drawing outside of the Paint event or is this a mistake?
Example control:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test.TestModes
{
public partial class ExampleControl: UserControl
{
private const string testString = "0123456789";
private RectangleF stringRect = new RectangleF(10, 10, 100, 20);
public ExampleControl()
{
InitializeComponent();
}
private void ExampleControl_Paint(object sender, PaintEventArgs e)
{
Font font = new Font("Arial", 12, FontStyle.Regular);
e.Graphics.DrawString(testString, font, Brushes.Black, stringRect);
font.Dispose();
}
private void DrawString(bool hit)
{
Font font = new Font("Arial", 12, FontStyle.Regular);
using(Graphics g = CreateGraphics())
{
g.SetClip(ClientRectangle);
if(hit)
g.DrawString(testString, font, Brushes.Red, stringRect);
else
g.DrawString(testString, font, Brushes.Black, stringRect);
}
font.Dispose();
}
private void ExampleControl_MouseMove(object sender, MouseEventArgs e)
{
if(stringRect.Contains(e.Location))
DrawString(true);
else
DrawString(false);
}
private void button1_Click(object sender, EventArgs e)
{
Invalidate();
}
}
}
It is the CreateGraphics() call that is getting you in trouble, indirectly. The problem is anti-aliasing of the text. A normal painting cycle erases the background before drawing something on top. That doesn't happen in your case, your draw text on top of existing text. The side effect is that the pixels uses to create the aliasing get darker each time your draw. The end result is bold looking, and noticeably jagged text outlines.
The fix is easy: start with a clean slate before you draw:
using (Graphics g = CreateGraphics()) {
g.Clear(this.BackColor); <=== added
g.SetClip(ClientRectangle);
// etc..
}
You'll now also get to encounter a problem in drawing known as "flicker". It might not yet be that noticeable yet, but it will when you do more drawing. Flicker is suppressed with double-buffering. A feature supported by Windows Forms, but only if you use standard drawing techniques. In other words: no CreateGraphics().
I want to show some graphics in a Winform app, it will be a stock's chart drawing tool. I think (but I am not sure...) I have to use a PictureBox, and using the System.Drawing.Graphics class' drawing primitives to draw the chart. I have started coding it, now it works more-or-less, but I have a problem with the resizing feature, as follows: when I resize the entire form, I see that the program shows the graphics then inmediately clear it. When I stop the mouse movement (without to release the mouse button) the graphics disappears!?!?
I made a small test environment to demo the bug:
Using VS2005, creating a new C# Windows Forms app, adding only a PictureBox to the form.
Setting the PictureBox's anchor to left, top, right and bottom. Add two event handler, the Resize to the PictureBox, and the Paint to the Form.
namespace PictureBox_Resize {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
Ellipse_Area = this.pictureBox1.Size;
}
private Pen penBlack = new Pen(Color.Black, 1.0f);
private Size Ellipse_Area;
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = this.pictureBox1.CreateGraphics();
g.DrawEllipse(penBlack, 0, 0, Ellipse_Area.Width, Ellipse_Area.Height);
}
private void pictureBox1_Resize(object sender, EventArgs e) {
Control control = (Control)sender;
Ellipse_Area = control.Size;
this.pictureBox1.Invalidate();
}
}
}
This small app shows the problem. It only draws an ellipse, but of course my drawing code is much more complicated one...
Any idea why the ellipse disappears when I resize the Form????
Why are you using a PictureBox? I would create a UserControl for your chart and draw the ellipse in its Paint method, just using its current size. In its constructor, set it up for double buffering and all painting in the paint method.
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
As far as I remember from my C++ days - where I did loads of such image-stuff - you need to call the repaint method - or override it to fit it for your behaviour.