@Layen

Как сделать так, чтобы рисунки не исчезали с picturebox при сворачивании или сохранении?

Здравствуйте. Есть Windows Form, в которой - PictureBox. На нем все рисуется, как перенести рисунок на Bitmap? Знаю, на контролах нельзя рисовать и тд. Теперь не знаю как переделать
namespace GraphicApp_v3
{
    public partial class Form1 : Form
    {
        Bitmap bitmap;
        Graphics g;
        bool isPressed = false;
        List<Point> list = new List<Point>();
        bool flag = false;
        int currentX, currentY;

        public Form1()
        {
            InitializeComponent();
            Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = pictureBox1.CreateGraphics();
            
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        public void Rectangle()
        {
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            Rectangle Rect = new Rectangle(currentX, currentY, 150, 75);
            g.FillRectangle(Brushes.White, Rect);
            g.DrawRectangle(new Pen(Color.Black, 2), Rect);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString(textBox1.Text, new Font("Times", 12), Brushes.Black, Rect, sf);
        }

        public void Cucl()
        {
            Point[] points = new Point[6]
            {
                new Point(currentX, currentY),
                new Point(currentX+50, currentY+40),
                new Point(currentX+105, currentY+40),
                new Point(currentX+160, currentY),
                new Point(currentX+105, currentY-40),
                new Point(currentX+50, currentY-40)};
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.FillPolygon(Brushes.White, points);
            g.DrawPolygon(new Pen(Color.Black, 2), points);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            float[] data = new float[points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                data[0] = points[i].X;
                data[1] = points[i].Y;
                data[2] = points[i].X;
                data[3] = points[i].Y;
            }
            g.DrawString(textBox1.Text, new Font("Times", 12), Brushes.Black, data[0]+30, data[1]+35, sf);
        }

        public void Rectangle_Naklon()
        {
            Point[] points = new Point[4]
            {
                new Point(currentX, currentY),
                new Point(currentX-15, currentY+70),
                new Point(currentX+140, currentY+70),
                new Point(currentX+155, currentY)
            };
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.FillPolygon(Brushes.White, points);
            g.DrawPolygon(new Pen(Color.Black, 2), points);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            float[] data = new float[points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                data[0] = points[i].X;
                data[1] = points[i].Y;
                data[2] = points[i].X;
                data[3] = points[i].Y;
            }
            g.DrawString(textBox1.Text, new Font("Times", 12), Brushes.Black, data[0]-80, data[1]+30, sf);
        }

        public void Uslovue()
        {
            Point[] points = new Point[4] { new Point(currentX,currentY), new Point(currentX + 75,currentY+50), new Point(currentX+150,currentY), new Point(currentX+75,currentY-50)};
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.FillPolygon(Brushes.White, points);
            g.DrawPolygon(new Pen(Color.Black, 2), points);
            StringFormat sf = new StringFormat();           
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            float[] data = new float[points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                data[0] = points[i].X;
                data[1] = points[i].Y;
                data[2] = points[i].X;
                data[3] = points[i].Y;
            }
            g.DrawString(textBox1.Text, new Font("Times", 12), Brushes.Black, data[0], data[1]+50, sf);
        }

        public void Start_End()
        {
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            Rectangle Rect = new Rectangle(currentX, currentY, 75, 40);
            g.FillEllipse(Brushes.White, Rect);
            g.DrawEllipse(new Pen(Color.Black, 2), Rect);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString("Начало", new Font("Times", 12), Brushes.Black, Rect, sf);
        }

        public void End()
        {
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            Rectangle Rect = new Rectangle(currentX, currentY, 75, 40);
            g.FillEllipse(Brushes.White, Rect);
            g.DrawEllipse(new Pen(Color.Black, 2), Rect);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString("Конец", new Font("Times", 12), Brushes.Black, Rect, sf);
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
                bitmap.Save(saveFileDialog1.FileName);


            }
        }
        
        

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            currentX = e.X;
            currentY = e.Y;
            label1.Text = currentX.ToString();
            label2.Text = currentY.ToString();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isPressed = true;
            
            if (radioButton8.Checked == true)
            {
                if (flag)
                {
                    list.Add(new Point(e.X, e.Y));
                    g.DrawLines(new Pen(Color.Black, 2), list.ToArray());
                }
                else
                {
                    list.Add(new Point(e.X, e.Y));
                    flag = true;
                }
                if (list.Count >= 2)
                {
                    list.Clear();
                    flag = false;
                }

            }

            if (radioButton4.Checked == true)
            {
                if (isPressed)
                {
                    Rectangle();
                }
            }

            if (radioButton5.Checked == true)
            {
                if (isPressed)
                {
                    Uslovue();
                }
            }

            if (radioButton1.Checked == true)
            {
                if (isPressed)
                {
                    Start_End();
                }
            }

            if (radioButton2.Checked == true)
            {
                if (isPressed)
                {
                    End();
                }
            }

            if (radioButton3.Checked == true)
            {
                if (isPressed)
                {
                    Rectangle_Naklon();
                }
            }

            if (radioButton6.Checked == true)
            {
                if (isPressed)
                {
                    Cucl();
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isPressed = false;
        }
    }
}
  • Вопрос задан
  • 461 просмотр
Решения вопроса 1
@Satangelus
Все рисование это у Вас сделано хорошо. А вот событие onpaint за Вас, Трамп будет обрабатывать?

Вообщем создаете битмап. Дальше вызываете
Graphics grfx = Graphics.FromImage(bitmap);

рисунки рисуете уже не на контроле а на битмапе, и потом на контроле рисуете битмап.
перекрываете событие онпаинт и в нем опять рисуете свой битмап.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы