# [C#] Will draw rectangle on form, but not inside picturebox?

## Recommended Posts

Hello, I'm new to c# and am trying to make a map editor with it. For some reason, when I try and draw a rectangle inside my picturebox it wont work. If I copy the code from the picturebox1_Paint() and put it inside the form1_Paint() function it works fine. Im trying to write a simple system that allows you to place boxes on your map, and use them for colision data. I tried searching the net but I havent found an answer after about an hour, so I figure I will ask you guys :). Heres 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.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;

namespace Besome_Map_Editor_v1
{
public partial class mapDisplay : Form
{
public string filename = "";
public bool drawBox  = false;
public List&lt;Array&gt; name = new List&lt;Array&gt;();
public int bx1 = 0;
public int bx2 = 0;
public int by1 = 0;
public int by2 = 0;
public int boxw = 0;
public int boxh = 0;
public mapDisplay()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
BinaryWriter bw=new BinaryWriter(fs);
bw.Write(filename);

fs.Close();
}

private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
Bitmap background = new Bitmap(openFileDialog1.FileName);
filename = Path.GetFileName(openFileDialog1.FileName);
pictureBox1.BackgroundImage = background;

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
drawBox = true;
}

private void pictureBox1_mouseDown(object sender, EventArgs e)
{
if (drawBox == true)
{
if (bx1 == 0 && bx2 == 0)
{
bx1 = MousePosition.X;
by1 = MousePosition.Y;
}
else
{
bx2 = MousePosition.X;
by2 = MousePosition.Y;
}

boxw = bx2 - bx1;
boxh = by2 - by1;
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(50, 30, 100, 100);
g.FillRectangle(lBrush, rect);
g.DrawRectangle(Pens.Aqua, 10, 10, 10, 10);
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

}
}



##### Share on other sites
When you are drawing on the form, drawing coordinates are given in terms of the upper left corner of the form. When drawing in the picture box, the coordinates are given in terms of the upper left corner of the picture box. So to draw in the very top left corner of the picture box, you would pass (0, 0), as the coordinate.

##### Share on other sites
Quote:
 Original post by Mike.PopoloskiWhen you are drawing on the form, drawing coordinates are given in terms of the upper left corner of the form. When drawing in the picture box, the coordinates are given in terms of the upper left corner of the picture box. So to draw in the very top left corner of the picture box, you would pass (0, 0), as the coordinate.

No matter what the coordinates I draw at, it wont work. The picturebox is about 500x700 so it would still display. I still haven't been able to fix this yet :(

##### Share on other sites
FIXED :)

Don't ask me how this happened but the picturebox I was using was inside a panel. Somehow its location was set to -151, so it was drawing way off screen. I knew it was weird that this wasnt working, and it was because I made a small little mistake :)

Either way, thanks Mike, and plz let this bury :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628281
• Total Posts
2981800

• 10
• 11
• 17
• 14
• 9