I am working on a program that should calculate the amount of material and the cost to make a certain object or product. There will be an option to draw something on picture box and also to draw sizes, i.e. elevations. I'm currently working on getting the lines to be straight. First I initialize the bitmap and immediately I have a problem with this.
Here's the code:
`
private void InitializeDrawing()
{
drawingBitmap = new Bitmap(pictureBox1.Height, pictureBox1.Width);
pictureBox1.Image = drawingBitmap;
drawingLine = false;
}`
Than I call it back like
`public Locksmith()
{
InitializeDrawing();
InitializeComponent();
}`
It seems to have a problem in the line " drawingBitmap = new Bitmap(pictureBox1.Height, pictureBox1.Width);" always throws an error " System.NullReferenceException: 'Object reference not set to an instance of an object.' pictureBox1 was null.".
I'm making an application in VS.
I tried to manually make the picturebox not null so that I could change something, but I didn't succeed with that idea. I was looking for similar problems, but I didn't manage to solve them even with the new overload. If anyone can give me some advice on what exactly I'm doing wrong. I tried to write the same code in a new program where I only have this.
`
namespace Straight__Line
{
public partial class Form1 : Form
{
Point startPoint;
Point endPoint;
bool drawingLine;
Bitmap drawingBitmap;
public Form1()
{
InitializeComponent();
InitializeDrawing();
}
private void InitializeDrawing()
{
drawingBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = drawingBitmap;
drawingLine = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (drawingBitmap != null)
{
e.Graphics.DrawImage(drawingBitmap, Point.Empty);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (!drawingLine)
{
startPoint = e.Location;
drawingLine = true;
}
else
{
endPoint = e.Location;
drawingLine = false;
using (Graphics g = Graphics.FromImage(drawingBitmap))
{
Pen pen = new Pen(Color.Black, 2);
g.DrawLine(pen, startPoint, endPoint);
pictureBox1.Invalidate();
}
}
}
}
}
`
I have the same thing written in that, so to speak, bigger application, but then the problem arises that my picture box was null. There is no error in this last code that I attached, but the same code is in another application and I have the problem that it is a picture box. My question is where should I look for the error? Or what should I do because I'm stuck on this problem for a long time.