简体   繁体   中英

How do i force load/ reload the winform in my program

I have searched a bit on the internet and found solutions involving reloading the winforms with Application.DoEvents() and Control.Invalidate() Control.Update()

But none of it works. I am trying to make program which takes a screenshot and then moves the pixels around randomly to the right. Kind of like a screen melter.

The only problem is that the form only shows when the application is done moving the pixels. How can i force him to show and re-draw whilst he is doing that.

This is the painting method:

// Draw the screenshot...
private void Form1_Paint(object sender, EventArgs e)
{
    Bitmap myBitmap = new Bitmap(@".\screenshot.jpg");

    int Xcount;

    int maxXValue = 1919;
    int maxYValue = 1079;

    Random random = new Random();
    for (Xcount = 0; Xcount < maxXValue; Xcount++)
    {
        screenshot.Invalidate();
        screenshot.Image = myBitmap;
        screenshot.Update();

        for (int Ycount = 0; Ycount < maxYValue; Ycount++)
        {
            int calculatedX = Xcount + random.Next(0, maxXValue);
            if (calculatedX > maxXValue) calculatedX = maxXValue;

            myBitmap.SetPixel(Xcount, Ycount, myBitmap.GetPixel(calculatedX, Ycount));
        }
    }
}

I tried your code and it refreshes just fine. My guess is that your image location in your Bitmap constructor is incorrect? Test it with a direct path: new Bitmap(@"c:\\yourImgDir\\screenshot.jpg"); and see if that works?

I fixed it using Form1_Load and Form1_Shown, Thanks @Andy Korneyev This is my code now:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScreenOutput
{
    public partial class Form1 : Form
    {
    private Bitmap myBitmap = new Bitmap(@".\screenshot.jpg");
    private PictureBox screenshot;
    private int Xcount;
    private int maxXValue = 1919;
    private int maxYValue = 1079;
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        screenshot.Load(@".\screenshot.jpg");
        screenshot.Image = myBitmap;
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        Random random = new Random();
        for (Xcount = 0; Xcount < maxXValue; Xcount++)
        {
            screenshot.Invalidate();
            screenshot.Update();

            for (int Ycount = 0; Ycount < maxYValue; Ycount++)
            {
                int calculatedX = Xcount + random.Next(0, maxXValue);
                if (calculatedX > maxXValue) calculatedX = maxXValue;
                myBitmap.SetPixel(Xcount, Ycount, myBitmap.GetPixel(calculatedX, Ycount));

            }
        }
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM