Drawing rectangle doesn't work - c#

Can you explain me what's wrong with this code? because it is not drawing anything.
doesn't it suppose to draw a rectangle in my form? thanks!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Graphics g = this.CreateGraphics();
Rectangle r = new Rectangle(0, 0, 150, 150);
g.DrawRectangle(System.Drawing.Pens.Black, r);
}
}

Make your painting in the OnPaint method:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle r = new Rectangle(0, 0, 150, 150);
g.DrawRectangle(System.Drawing.Pens.Black, r);
}

Related

How can I draw from another class?

I have MainForm class. Here I can make somthing like this.
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
And I can see result in my Form.
But I have another class Image. And I want to draw from here. How can I do it?
Send the PaintEventArgs (the below came from one i have been using)
class Draw
{
public void Paint(PaintEventArgs e)
{
e.Graphics.DrawRectangles(Pens.Blue, GetRectangle());
}
}
where GetRectangle would be another method to define the rectangle
you should also be able to just send your object (in your case the instance of MainForm)
class Draw
{
public void Paint(MainForm main)
{
Graphics graphics = main.CreateGraphics();
}
}
or the graphics object
class Draw
{
public void Paint(Graphics graphics)
{
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
}
you still need the event handler for the PictureBox, so you would do something like
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Draw image = new Draw();
image.Paint(graphics);
}

Bufferedgraphics change background to black c#

I try to do double buffer using BufferedGraphics. When i use BufferedGraphics.Render method background of my image changes to black. Here the simple code, that illustrate my issue
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e) {
Paint += new PaintEventHandler(Form1_Paint);
}
private void print(Bitmap image, PaintEventArgs e) {
Graphics graphicsObj = e.Graphics;
graphicsObj.DrawImage(image, 60, 10);
graphicsObj.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Rectangle rect = Screen.PrimaryScreen.Bounds;
PixelFormat pf;
pf = PixelFormat.Format32bppArgb;
Bitmap image = new Bitmap(rect.Width, rect.Height, pf);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Orange);
BufferedGraphicsContext context = new BufferedGraphicsContext();
BufferedGraphics buffer = context.Allocate(g, new Rectangle(0, 0, rect.Width + 20, rect.Height + 20));
buffer.Render(g);
print(image, e);
}
}
I expect to see orange rectangle on my screen, but it's black. I can't understand why this happen. Help me please :)
buffer.Render(g) renders the contents of the buffer to the graphics object. This means that the orange color is being overwritten by the empty buffer.
You'll have to choose between using BufferedGraphicsContext or creating a buffer yourself (the image).
The following would fix your issue using just the image:
...
Bitmap image = new Bitmap(rect.Width, rect.Height, pf);
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(Color.Orange);
}
print(image, e);
You could also still use BufferedGraphicsContext, but you'd have to write the image to its Graphics property:
print(image, buffer.Graphics); // render your image to the buffer
buffer.Render(e.Graphics); // render the buffer to the paint event graphics
By the way, don't Dispose the graphics object provided by Form1_Paint (you currently do that in the print() method.
As a response to your comment, BufferedGraphicsContext seems to not support transparency when rendering it to the "main" graphics object, but you can draw transparent images to it correctly. The following example shows how the buffer is filled with a red color, and then a transparent image with a blue line is drawn to it:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (BufferedGraphicsContext context = new BufferedGraphicsContext())
using (BufferedGraphics buffer = context.Allocate(e.Graphics, new Rectangle(0, 0, 120, 120)))
{
// Create a bitmap with just a blue line on it
Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawLine(Pens.Blue, 0, 0, 100, 100);
}
// Fill a red square
buffer.Graphics.FillRectangle(Brushes.Red, 5, 5, 110, 110);
// Draw the blue-line image over the red square area
buffer.Graphics.DrawImage(bmp, 10, 10);
// Render the buffer to the underlying graphics
buffer.Render(e.Graphics);
}
}
In the result you can clearly see the blue line from the image over the red color in the background buffer (the red background is not overwritten) and there's a black border around the red rectangle where no background pixels where drawn.

How can I draw a known rectangle size on a new bitmap?

I have this code:
Bitmap newbmp = new Bitmap(512, 512);
foreach (Point s in CommonList)
{
w.WriteLine("The following points are the same" + s);
newbmp.SetPixel(s.X, s.Y, Color.Red);
}
w.Close();
newbmp.Save(#"c:\newbmp\newbmp.bmp", ImageFormat.Bmp);
newbmp.Dispose();
The code is not in a paint event.
Then I have a paint event of a pictureBox1:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (cloudPoints != null)
{
if (DrawIt)
{
e.Graphics.DrawRectangle(pen, rect);
pointsAffected = cloudPoints.Where(pt => rect.Contains(pt));
CloudEnteringAlert.pointtocolorinrectangle = pointsAffected.ToList();
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb);
CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp);
}
}
}
In the paint event I'm drawing a rectangle. The variable rect.
I want to draw the rectangle rect on the newbmp. After saving the newbmp to draw this rect on him.
How can I draw the rect on the newbmp ?
You should create a Graphics object from the bitmap to work on it:
..
Bitmap newbmp = new Bitmap(512, 512);
foreach (Point s in CommonList)
{
Console.WriteLine("The following points are the same" + s);
newbmp.SetPixel(s.X, s.Y, Color.Red);
}
using (Graphics G = Graphics.FromImage(newbmp))
{
G.DrawRectangle(Pens.Red, yourRectangle);
..
}
newbmp.Save(#"c:\newbmp\newbmp.bmp", ImageFormat.Bmp);
newbmp.Dispose();
For other Pen properties like Width or LineJoin use this format:
using (Graphics G = Graphics.FromImage(newbmp))
using (Pen pen = new Pen(Color.Red, 8f) )
{
// rounded corners please!
pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
G.DrawRectangle(pen, yourRectangle);
//..
}

How to draw object in PictureBox c#?

I trying draw a rectangles in PictureBox by mouse click:
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, e.X, e.Y, 50, 50);
pen.Dispose();
}
}
And rectangles are drawning. But when i move mouse beyond the PictureBox all rectangles are disappear. How to avoid it?
UPDATE
I added a Paint event:
private List<Rectangle> Rectangles { set; get; }
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
pen.Dispose();
}
}
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
Rectangles.Add(new Rectangle(e.X, e.Y, 50, 50));
MyPictureBox.Refresh();
}
But now rectangles not drawning.
Update
Oh it was my mistake.
g.DrawRectangle(pen, rect); -> e.Graphics.DrawRectangle(pen, rect);
Yes, you're drawing over the picture box. When the next paint messgae arrives, picturebox re-paints itself again at that time it'll overwrite your rectangles.
You either need to draw it in Paint event in order to make your rectangles survive or you can Draw over the PictureBox.Image so it will stay there.
For your edit: You need to use e.Graphics property. For instance following code works for me.
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (var pen = new Pen(Color.Black, 2))
{
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
}
}

Drawing a point in an image in C#

I have an image with the dimensions of 100x50 and I want to draw a dot in the center - i.e. at 50x25. How would I do this?
you could use the setPixle() function.
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.Black, new Rectangle(50, 25, 1, 1));
look here for saving the picture
it does not draw on form load so you should add your code in form paintevent :
private void Form1_Paint(object sender, PaintEventArgs e)
{
Application.DoEvents();
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.DarkBlue, new Rectangle(120, 90, 1, 1));
}
Image img = pictureBox1.Image;
Graphics g = Graphics.FromImage(img);
g.DrawEllipse(Pens.DarkBlue, new Rectangle(50, 25, 1, 1));
g.DrawImage(img, new Point(0, 0));
img.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
try this for other question :
Rectangle bounds = new Rectangle(10, 20, 50, 60);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty,Point.Empty, bounds.Size);
bitmap.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
this code will capture screen with bound of bounds rectangle.

Categories

Resources