【发布时间】:2015-10-29 21:51:07
【问题描述】:
我一直在尝试制作一个简单的 mandelbrot 渲染器,只是为了让自己进入 c# 和表单,但是当我渲染图像时,程序有时会耗尽内存。
内存增加到 2GB,然后崩溃。
但有时它会像这样构建一个拼图模式并且不会崩溃: http://puu.sh/l2ri9/2fcd47e6d7.png
==============要渲染的代码================
Renderer.CreateGraphics().Clear(Color.White);
double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);
SolidBrush MandelColor = new SolidBrush(Color.Red);
for (int y = 0; y < Renderer.Height; y++)
{
for (int x = 0; x < Renderer.Width; x++)
{
double cr = fitInRRange(x, Renderer.Width, minR, maxR);
double ci = fitInIRange(y, Renderer.Height, minI, maxI);
int n = findMandelbrot(cr, ci, maxN);
double t = ((n + 0.0) / (maxN + 0.0));
MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));
Renderer.CreateGraphics().FillRectangle(MandelColor, x, y, 1, 1);
}
}
===链接到 Github 页面=== https://github.com/JimAlexBerger/MandelbrotProject
【问题讨论】:
-
你是编译成 32 位还是 64 位?如果可能的话,你应该把它编译为 64 位,如果你有它,它会给你更多的内存,因为 32 位有 4GB 的内存限制。
-
不要不要在循环中使用
Renderer.CreateGraphics()。可能你不应该使用CreateGraphics根本。Renderer是什么?一个控制?最好使用Bitmap bmp并使用using (Graphics G = Graphics.FromImage(bmp)) { your loops here!}