D
D
DAWNLOADER2020-10-08 16:27:42
C++ / C#
DAWNLOADER, 2020-10-08 16:27:42

How to save a picture on a form without window borders?

I write in C# in Windows Forms. I'm using the DrawToBitmap method, but it makes a copy of the entire form. Can this be bypassed somehow?
Edit: I solved the problem. But another appeared. Since I'm drawing on a form, I need to save the drawing. The drawing is not saved, I draw with the mouse, placing filled rectangles according to the coordinates of the movement.

Preservation

Bitmap newImage = new Bitmap(_myForm.Width, _myForm.Height);
            Rectangle rect = new Rectangle(new Point(0, 0), new Size(_myForm.Width, _myForm.Height));
            Graphics g = Graphics.FromImage(newImage);
            // Координаты верхнего левого угла ИСТОЧНИКА
            int x0 = 10;
            int y0 = 30;

            // Координаты верхнего левого угла ПРИЕМНИКА
            int x = 0;
            int y = 0;

            // Ширина и высота вырезамой части
            int width = _myForm.Width - 20;
            int height = _myForm.Height - 60 ;

            // Image - источник
            _myForm.DrawToBitmap(newImage, rect);

            // Image - приемник
            Bitmap img = new Bitmap(width, height);

            // Область для вырезания
            Rectangle srcRect = new Rectangle(x0, y0, width, height);
            GraphicsUnit units = GraphicsUnit.Pixel;

            // Рисуем
            Graphics gr = Graphics.FromImage(img);
            gr.DrawImage(newImage, x, y, srcRect, units);


            img.Save(@"C:\form.png");


Painting

private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isPress = false;
        }

        private void RightMouse(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) this.Refresh() ;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if ((isPress) & (e.Button == MouseButtons.Left))
                g.FillEllipse(new SolidBrush(Color.Black), new Rectangle(e.X, e.Y, 5, 5));
        } 

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isPress = true;
            if (e.Button == MouseButtons.Left)
                g.FillEllipse(new SolidBrush(Color.Black), new Rectangle(e.X, e.Y, 5, 5));
            else if (e.Button == MouseButtons.Right) this.Refresh();
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question