在没有“大方块"的情况下放大图像(游戏地图). [英] Make an image (game map) bigger without "big squares"
问题描述
我在一个游戏中工作,该游戏中有动态高度和宽度的地图(例如30x30).在一个屏幕上,我想向更大的用户显示地图(政治地图).假设我要以600x600显示.
我知道,我可能无法使用.NET的经典方式,因为当我调整.NET的大小时,它就变得模糊了.避免模糊的唯一方法是设置InterpolationMode.NearestNeighbor.但是使用时是平方".
我只是想知道一件事-是否有一种调整大小的方法,该方法可以从旧像素中近似新像素而不是模糊它?
像
例举
我使用的是System.Drawing
,而不是WPF
Hi,
I work on a game where is a map of dynamic height and width (for example 30x30). On one screen I want to display the map to user bigger (political map). Let''s say I want to display it in 600x600.
I understand, I probably cannot use classic ways of .NET, because when I resize it, it is blurred. The only way to not have it blurred is to set InterpolationMode.NearestNeighbor. However is is "squared" when I use it.
I am just wondering about one thing -Is there a resize method, which can approximate the new pixels from the old one instead of blurring it?
Something like
exaxmple
I use System.Drawing
, not WPF
推荐答案
否,但是您可以轻松地渲染这样的东西.
如果您标记了问题,我将解释如何做:您使用的是什么:WPF或System.Drawing
(是否带有System.Windows.Forms
)?不要只是回答我的问题,请给它加上标签! (您想吸引其他专家来回答您的问题,因此请务必非常准确地进行标记.)
对于System.Windows.Forms
和System.Drawing
,您首先需要获取一个像素数组.这是使用System.Drawing.Bitmap.LockBits
完成的.您可以将其与System.Runtime.InteropServices.Marshal.Copy
或unsafe
模式下的指针一起使用.您可以在以下位置使用Marshal
找到代码示例:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx [ ^ ].
现在,您将其缩放为整数因子N(仅整数).这意味着每个像素应由正方形的N×N像素或均匀的颜色表示.您需要将所有这些正方形并排& msash;渲染.多达原始图片中的像素.您可以使用两种方法来做到这一点:
No, but you can easily render such thing.
I''ll explain how to do it if you tag your question: what do you use: WPF orSystem.Drawing
(withSystem.Windows.Forms
or not)? Don''t just answer my question, tag it! (You want to attract other experts to your question, so always tag very accurately.)
WithSystem.Windows.Forms
andSystem.Drawing
, you first need to get an array of pixels. This is done usingSystem.Drawing.Bitmap.LockBits
. You can use it with eitherSystem.Runtime.InteropServices.Marshal.Copy
or pointers inunsafe
mode. You can find the code sample usingMarshal
here:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^].
Now, you scale it by an integer factor N (only integer). It means each pixel should be represented by a square N×N pixel or the uniform color. You need to render all those squares side by side &msash; as many as pixels in the original picture. You can do it using two ways:
- 使用与以前相同的代码示例,并查看如何使用
LockBits
和Marshal
写入位图.主要区别是:您需要创建另一个位图进行渲染,将其放大N倍,并使用从原始位图读取的像素数组设置其中的所有像素 -
您可以创建N次新的位图更大,使用静态方法System.Drawing.Graphics.FromImage
创建它的实例System.Drawing.Graphics
,并使用该实例和实例方法System.Drawing.Graphics.FillRectangle
渲染所有矩形.
- Use the same code sample as before and see how to write to a bitmap using
LockBits
andMarshal
. The main difference is: you need to create another bitmap for rendering, N time bigger and set all pixels in it using the pixel array read from the original bitmap You can create a new bitmap N times bigger, create an instance of itSystem.Drawing.Graphics
using the static methodSystem.Drawing.Graphics.FromImage
and render all those rectangles using this instance and the instance methodSystem.Drawing.Graphics.FillRectangle
.
很难说哪种方法会更快.它也可能取决于您的缩放比例.请尝试一下.如果您分享您的结果,那就太好了.
无论如何,通过重新采样进行缩放是完全错误的方法.
您只需要使用矢量图形即可.有几种选择:使用您自己的数据层(向量基元的集合,无比例缩放,可伸缩)和针对每个比例动态渲染,基于图元文件进行渲染.我真的不想讨论这个问题,因为WPF胜过一切.它已经具有成熟的矢量图形,其中包含任何复杂性的对象,设计时或运行时,完全实现的缩放比例等等.几乎没有底层编程.完全没有渲染.我什至不愿意使用表单的想法.我希望它们已经过时了.
[END EDIT]
Hard to say which method will be faster; it may also depends on your scaling factor. Please experiment. Would be nice if you share your results.
Scaling via re-sampling is completely wrong approach anyway.
You need to use vector graphics and nothing else. There are several options: using your own data layer (collection of vector primitive, scale-free, scalable) and rendering on the fly for each scale, rendering based on metafile. I don''t really want to discuss this because WPF beats all. It already has fully-fledged vector graphics with objects of any complexity, design-time or run-time, fully implemented scaling and more. Almost no low-level programming. No rendering at all. I would not even play with the idea of using Forms. They are getting obsolete anyway, I hope.
[END EDIT]
这篇关于在没有“大方块"的情况下放大图像(游戏地图).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!