简体   繁体   English

我如何在程序中强制加载/重新加载Winform

[英]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() 我在互联网上进行了一些搜索,找到了涉及使用Application.DoEvents()Control.Invalidate() Control.Update()重新加载Winforms的解决方案

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? 我的猜测是您在Bitmap构造函数中的图像位置不正确? Test it with a direct path: new Bitmap(@"c:\\yourImgDir\\screenshot.jpg"); 使用直接路径对其进行测试: 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: 我使用Form1_Load和Form1_Shown修复了它,谢谢@Andy Korneyev这是我的代码:

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));

            }
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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