[英]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.