I have this Paint program in C# where so far I can draw with different colours. The problem is that my bitmap only supports "normal" size picture box to work correctly. Is there a way to create another layer or something behind my main picture box to place an image in it?
This is the code, as you can see, in setSize function I create my rectangle which is white. I want it to be a stretched image, but at the same time to draw smoothly on my paint:
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 PaintGreenTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetSize();
}
private class ArrayPoints
{
private int index = 0;
private Point[] points;
public ArrayPoints(int size)
{
if (size <= 0) { size = 2; }
points = new Point[size];
}
public void SetPoint(int x, int y)
{
if (index >= points.Length)
{
index = 0;
}
points[index] = new Point(x, y);
index++;
}
public void ResetPoints()
{
index = 0;
}
public int GetCountPoints()
{
return index;
}
public Point[] GetPoints()
{
return points;
}
}
private bool isMouse = false;
private ArrayPoints arrayPoints = new ArrayPoints(2);
Bitmap map = new Bitmap(100, 100);
Graphics graphics;
Pen pen = new Pen(Color.Black, 3f);
private void SetSize()
{
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
map = new Bitmap(rectangle.Width, rectangle.Height);
graphics = Graphics.FromImage(map);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouse = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouse = false;
arrayPoints.ResetPoints();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!isMouse) { return; }
arrayPoints.SetPoint(e.X, e.Y);
if (arrayPoints.GetCountPoints() >= 2)
{
graphics.DrawLines(pen, arrayPoints.GetPoints());
pictureBox1.Image = map;
arrayPoints.SetPoint(e.X, e.Y);
}
}
private void button4_Click(object sender, EventArgs e)
{
pen.Color = ((Button)sender).BackColor;
}
private void button8_Click(object sender, EventArgs e)
{
// pen.Color = Color.Transparent;
}
private void button9_Click(object sender, EventArgs e)
{
if(colorDialog1.ShowDialog() == DialogResult.OK)
{
pen.Color = colorDialog1.Color;
((Button)sender).BackColor = colorDialog1.Color;
}
}
private void button2_Click(object sender, EventArgs e)
{
graphics.Clear(pictureBox1.BackColor);
pictureBox1.Image = map;
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
pen.Width = trackBar1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPG(*.JPG)|*.jpg";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if(pictureBox1.Image == null)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
}
}
}
If you have any idea how I can work on bitmap layers or if I can replace that white background with an image, I'll be very glad. Thank you!
This is how the paint looks like, the eraser is not finished. PaintScreenshot