簡體   English   中英

我如何在程序中強制加載/重新加載Winform

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

我在互聯網上進行了一些搜索,找到了涉及使用Application.DoEvents()Control.Invalidate() Control.Update()重新加載Winforms的解決方案

但是,它們都不起作用。 我正在嘗試制作需要截圖的程序,然后將像素隨機向右移動。 有點像屏幕熔化器。

唯一的問題是,表單僅在應用程序完成移動像素后才顯示。 在他這樣做時,我怎么能強迫他展示並重畫。

這是繪畫方法:

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

我嘗試了您的代碼,它刷新就好了。 我的猜測是您在Bitmap構造函數中的圖像位置不正確? 使用直接路徑對其進行測試: new Bitmap(@"c:\\yourImgDir\\screenshot.jpg"); 看看是否可行?

我使用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