Функция для рисования на picturebox - C#
Формулировка задачи:
Подскажите как мне написать функцию чтобы при её вызове рисовалось изображение на пикчербоксе ,проблема в том ,что пишу Морской бой...щас сделал этап,где кликая по пиечербоксу,на котором сетка(10,10) рисует в клетке крест,но при рисовании следующего креста предыдущий стирается так как код креста и самого поля записан так
я понимаю что он он обновляет пикчербокс,но как сделать чтобы он дополнял а не обновлял
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics gr = e.Graphics;
Pen pen = new Pen(Color.Blue, 2);
gr.DrawLine(pen, 0, 0, 200, 0);
gr.DrawLine(pen, 0, 20, 200, 20);
gr.DrawLine(pen, 0, 40, 200, 40);
gr.DrawLine(pen, 0, 60, 200, 60);
gr.DrawLine(pen, 0, 80, 200, 80);
gr.DrawLine(pen, 0, 100, 200, 100);
gr.DrawLine(pen, 0, 120, 200, 120);
gr.DrawLine(pen, 0, 140, 200, 140);
gr.DrawLine(pen, 0, 160, 200, 160);
gr.DrawLine(pen, 0, 180, 200, 180);
gr.DrawLine(pen, 0, 200, 200, 200);
gr.DrawLine(pen, 0, 0, 0, 200);
gr.DrawLine(pen, 20, 0, 20, 200);
gr.DrawLine(pen, 40, 0, 40, 200);
gr.DrawLine(pen, 60, 0, 60, 200);
gr.DrawLine(pen, 80, 0, 80, 200);
gr.DrawLine(pen, 100, 0, 100, 200);
gr.DrawLine(pen, 120, 0, 120, 200);
gr.DrawLine(pen, 140, 0, 140, 200);
gr.DrawLine(pen, 160, 0, 160, 200);
gr.DrawLine(pen, 180, 0, 180, 200);
gr.DrawLine(pen, 200, 0, 200, 200);
gr.DrawLine(pen, Convert.ToInt32(x1), Convert.ToInt32(y1), Convert.ToInt32(x1) + 20, Convert.ToInt32(y1) + 20);
gr.DrawLine(pen, Convert.ToInt32(x1), Convert.ToInt32(y1) + 20, Convert.ToInt32(x1) + 20, Convert.ToInt32(y1));
}Решение задачи: «Функция для рисования на picturebox»
textual
Листинг программы
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 _1921
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);
gr = Graphics.FromImage(bt);
pictureBox1.Image = bt;
}
Bitmap bt; Graphics gr; Pen pn;
private void DravGrid()
{
pn = new Pen(Color.Blue);
for (int i = 0; i < pictureBox1.Width; i += 25)
gr.DrawLine(pn, i, 0, i, pictureBox1.Height);
for (int i = 0; i < pictureBox1.Height; i += 25)
gr.DrawLine(pn, 0, i, pictureBox1.Width, i);
}
private void SetPoint(int x, int y)
{
pn = new Pen(Color.Black);
int koll = 0;
for (int i = 0; koll < x; i++)
koll += 25;
x = koll - 25;
koll = 0;
for (int i = 0; koll < y; i++)
koll += 25;
y = koll - 25;
gr.DrawLine(pn, x, y, x + 25, y + 25);
gr.DrawLine(pn, x + 25, y, x, y + 25);
}
private void Form1_Load(object sender, EventArgs e)
{
DravGrid();
pictureBox1.Invalidate();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
DravGrid();
Point pt = new Point(e.X, e.Y);
pn = new Pen(Color.Blue);
SetPoint(pt.X, pt.Y);
pictureBox1.Invalidate();
}
}
}