简体   繁体   English

如何在 WinForms 中绘制大图像?

[英]How can I draw a big image in WinForms?

I'm trying to make a floor for a top down style game, where I used to use pictureboxes我正在尝试为自上而下风格的游戏制作地板,我曾经在其中使用图片框

I was told that instead of using a Picturebox, I should be using the Graphics.DrawImage();有人告诉我,我应该使用Graphics.DrawImage();而不是使用 Picturebox。 method, which seems to slightly help but still is very laggy.方法,这似乎有点帮助,但仍然很滞后。

My paint function looks like this to draw the background looks like this:我的油漆 function 看起来像这样绘制背景看起来像这样:

How should I make the drawing less laggy?我应该如何使绘图不那么滞后? The image is 2256 by 1504图像为 2256 x 1504

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.DrawImage(PeanutToTheDungeon.Properties.Resources.tutorialFloor, 0, 0);
}

There are two points that I would make here.我想在这里提出两点。

Firstly, DO NOT use Properties.Resources like that.首先,不要那样使用Properties.Resources Every time you use a property of that object, the data for that resource gets extracted from the assembly.每次使用该 object 的属性时,该资源的数据都会从程序集中提取。 That means that you are creating a new Image object every time the Paint event is raised, which is often.这意味着每次引发Paint事件时,您都会创建一个新的Image object,这很常见。 You should use that resource property once and once only, assign the value to a field and then use that field repeatedly.您应该只使用该资源属性一次,将值分配给一个字段,然后重复使用该字段。 That means that you will save the time of extracting that data and creating the Image object every time.这意味着您将节省每次提取该数据和创建Image object 的时间。

The second point may or may not be applicable but, if you are forcing the Paint event to be raised by calling Invalidate or Refresh then you need to make sure that you are invalidating the minimum area possible.第二点可能适用也可能不适用,但是如果您通过调用InvalidateRefresh强制引发Paint事件,那么您需要确保使最小区域无效。 If you call Refresh or Invalidate with no argument then you are invalidating the whole for, which means that the whole form will be repainted.如果您不带任何参数调用RefreshInvalidate ,那么您就是在使整个 for 无效,这意味着将重新绘制整个表单。 The repainting of pixels on screen is actually the slow part of the process, so that's the part that you need to keep to a minimum.屏幕上像素的重绘实际上是过程中较慢的部分,因此您需要将这部分保持在最低限度。 That means that, when something changes on your game board, you need to calculate the smallest area possible that will contain all the possible changes and specify that when calling Invalidate .这意味着,当您的游戏板上发生某些变化时,您需要计算包含所有可能变化的最小区域,并在调用Invalidate时指定该区域。 That will reduce the amount of repainting and speed up the process.这将减少重新绘制的数量并加快该过程。

Note that you don't need to change your drawing code.请注意,您不需要更改绘图代码。 You always DRAW everything, then the system will PAINT the part of the form that you previously invalidated.您总是绘制所有内容,然后系统将绘制您之前无效的表格部分。

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

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