-1

I draw a figures on a windows form, not on picturebox. And I would like to save view of my windows form to special folder on desktop. I dont know if better will be make a screenshot or i should try to save it as an image (bitmap) in my folder. Generally I tried both options but it doesnt work :( I put here one of my attempt ...

   public void Form1_Paint(object sender, PaintEventArgs e)
{
    int x1, y1, x2, y2;
    Random losowa1 = new Random();
    x1 = losowa1.Next(0, 200);
    y1 = losowa1.Next(120, 300);
    x2 = losowa1.Next(300, 480);
    y2 = losowa1.Next(120,300);
    e.Graphics.FillRectangle(Brushes.Black, x1, y1, 100, 100);
    e.Graphics.FillEllipse(Brushes.Black, x2, y2, 100, 100);

   Bitmap bitmap = new Bitmap(this.Width, this.Height);
    DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
    bitmap.Save("@C:\\Desktop", ImageFormat.Jpeg);
   // MessageBox.Show("saved");

   System.Threading.Thread.Sleep(1000);
   this.Close();

 }

and

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 3; i++)
    {
        Form1 OknoStart = new Form1();
        OknoStart.ShowDialog();


 }
Filburt
  • 17,626
  • 12
  • 64
  • 115
Kim
  • 25
  • 3
  • 1
    Possible duplicate of [Capture screenshot of active window?](https://stackoverflow.com/questions/1163761/capture-screenshot-of-active-window) – obl Nov 12 '18 at 16:21
  • Why are you drawing to the form? Drawing on the bitmap and displaying that as the form's background image seems a lot more sensible. Also note, whatever you think `Thread.Sleep(1000);` is doing there, it isn't. It just blocks your UI thread. Use proper events to signal completion of operations. – Nyerguds Nov 13 '18 at 14:03

1 Answers1

1

try this

 int i;
   public Form1(int i)
        {
            InitializeComponent();
            this.i = i;
        }

private void Form1_Load(object sender, EventArgs e)
        {
            using (Bitmap bmp = new Bitmap(this.Width, this.Height))
            {
                this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
               bmp.Save(@"C:\Users\User\Desktop\sample" + i+".png", ImageFormat.Png);
            }
        }

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            int x1, y1, x2, y2;
            Random losowa1 = new Random();
            x1 = losowa1.Next(0, 200);
            Random losowa2 = new Random();
            y1 = losowa2.Next(0, 480);
            Random losowa3 = new Random();
            x2 = losowa1.Next(300, 500);
            Random losowa4 = new Random();
            y2 = losowa2.Next(0, 480);
            e.Graphics.FillRectangle(Brushes.Black, x1, y1, 100, 100);
            e.Graphics.FillEllipse(Brushes.Black, x2, y2, 100, 100);
            System.Threading.Thread.Sleep(2000);
            this.Close();
        }




   private void button1_Click(object sender, EventArgs e)
        {
            for (int i=0;i<3;i++) {
                Form1 form1 = new Form1(i);
                form1.ShowDialog();

            }
        }