Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 分形渲染器根本不显示图像?_C#_Fractals - Fatal编程技术网

C# 分形渲染器根本不显示图像?

C# 分形渲染器根本不显示图像?,c#,fractals,C#,Fractals,我正在将分形渲染器从Java转换为C#进行赋值,我想我已经设置好了一切,但分形本身不会渲染 这是我运行程序时的一张照片: 下面是我的文件在包含项目本身的文件夹中的布局: 这是我用于实际渲染本身的代码,我认为没有错误,但如果我错过了一些非常明显的东西,我很抱歉浪费了您所有的时间: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System

我正在将分形渲染器从Java转换为C#进行赋值,我想我已经设置好了一切,但分形本身不会渲染

这是我运行程序时的一张照片:

下面是我的文件在包含项目本身的文件夹中的布局:

这是我用于实际渲染本身的代码,我认为没有错误,但如果我错过了一些非常明显的东西,我很抱歉浪费了您所有的时间:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            init();
            start();
            this.DoubleBuffered = true;
        }

        //code to convert HSB to RGB from HSB.cs. All your code so i made it take up less space. 
        public struct HSBColor
        {
            float h;
            float s;
            float b;
            int a;
            public HSBColor(float h, float s, float b) { this.a = 0xff; this.h = Math.Min(Math.Max(h, 0), 255); this.s = Math.Min(Math.Max(s, 0), 255); this.b = Math.Min(Math.Max(b, 0), 255); }
            public HSBColor(int a, float h, float s, float b) { this.a = a; this.h = Math.Min(Math.Max(h, 0), 255); this.s = Math.Min(Math.Max(s, 0), 255); this.b = Math.Min(Math.Max(b, 0), 255); }
            public float H { get { return h; } }
            public float S { get { return s; } }
            public float B { get { return b; } }
            public int A { get { return a; } }
            public Color Color { get { return FromHSB(this); } }
            public static Color FromHSB(HSBColor hsbColor)
            {
                float r = hsbColor.b;
                float g = hsbColor.b;
                float b = hsbColor.b;
                if (hsbColor.s != 0)
                {
                    float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h * 360f / 255f;
                    if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; }
                    else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; }
                    else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; }
                    else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; }
                    else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; }
                    else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; }
                    else { r = 0; g = 0; b = 0; }
                }
                return Color.FromArgb(hsbColor.a, (int)Math.Round(Math.Min(Math.Max(r, 0), 255)), (int)Math.Round(Math.Min(Math.Max(g, 0), 255)), (int)Math.Round(Math.Min(Math.Max(b, 0), 255)));
            }
        }
        private const int MAX = 256;      // max iterations
        private const double SX = -2.025; // start value real
        private const double SY = -1.125; // start value imaginary
        private const double EX = 0.6;    // end value real
        private const double EY = 1.125;  // end value imaginary
        private static int x1, y1, xs, ys, xe, ye;
        private static double xstart, ystart, xende, yende, xzoom, yzoom;
        private static float xy;
        private int c = 0;
        //private Image picture; Taken out, not needed
        // create rectangle variable JGB
        Rectangle rec;
        private Graphics g1;
        //private Cursor c1, c2; Taken out, not needed
        private System.Drawing.Bitmap bitmap;
        public void init()
        {
            //setSize(640, 480); changed this code to JGB:
            this.Size = new Size(640, 480);
            // Taken all lines out below. Not needed.
            /*finished = false; 
            addMouseListener(this);
            addMouseMotionListener(this);
            c1 = new Cursor(Cursor.WAIT_CURSOR);
            c2 = new Cursor(Cursor.CROSSHAIR_CURSOR); */
            x1 = 640;
            y1 = 480;
            xy = (float)x1 / (float)y1;
            //picture = createImage(x1, y1); Taken out and replaced with JGB:
            bitmap = new Bitmap(x1, y1);
            //g1 = picture.getGraphics(); changed to get my bitmap 
            g1 = Graphics.FromImage(bitmap);
            //finished = true; Finished variable deleted so not needed
        }
        //Code below didnt appear to do anything so i deleted it
        /*public void destroy() // delete all instances 
        {
            if (finished)
            {
                removeMouseListener(this);
                removeMouseMotionListener(this);
                picture = null;
                g1 = null;
                c1 = null;
                c2 = null;
                System.gc(); // garbage collection
            }
        } */

        public void start()
        {
            //action = false;
            //rectangle = false;

            initvalues();

            // added dialog box for instance loading and save varaibles needed for position and zoom to text file
            DialogResult dialog = MessageBox.Show("Would You Like to Load Your Last Instance?", "Load Instance?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (dialog == DialogResult.Yes)
            {
                string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\Writelines.txt");
                xzoom = System.Convert.ToDouble(lines[0]);
                yzoom = System.Convert.ToDouble(lines[1]);
                xstart = System.Convert.ToDouble(lines[2]);
                ystart = System.Convert.ToDouble(lines[3]);
            }
            else
            {
                xzoom = (xende - xstart) / (double)x1;
                yzoom = (yende - ystart) / (double)y1;
            }

            mandelbrot();
        }
        public void stop()
        {
        }
        /*public void paint(Graphics g, PaintEventArgs e)
        {
            update(g);
        }
       public void update(Graphics g)
        {
            //g.DrawImage(picture, 0, 0);

        }*/
        private void mandelbrot()
        {
            int x, y;
            float h, b, alt = 0.0f;
            Color color;

            Pen pen = new Pen(Color.Black);
            for (x = 0; x < x1; x += 2)
                for (y = 0; y < y1; y++)
                {
                    h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y, c);
                    if (h != alt)
                    {
                        b = 1.0f - h * h;

                        color = HSBColor.FromHSB(new HSBColor(h * 255, 0.8f * 255, b * 255));
                        pen = new Pen(color);
                        alt = h;
                    }
                    g1.DrawLine(pen, x, y, x + 1, y);
                }
        }

        private float pointcolour(double xwert, double ywert, int j)
        {
            double r = 0.0, i = 0.0, m = 0.0;

            // int j = 0;

            while ((j < MAX) && (m < 4.0))
            {
                j++;
                m = r * r - i * i;
                i = 2.0 * r * i + ywert;
                r = m + xwert;
            }
            return (float)j / (float)MAX;
        }
        private void initvalues()
        {
            xstart = SX;
            ystart = SY;
            xende = EX;
            yende = EY;
            if ((float)((xende - xstart) / (yende - ystart)) != xy)
                xstart = xende - (yende - ystart) * (double)xy;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g1 = e.Graphics;
            g1.DrawImage(bitmap, 0, 0, x1, y1);
            using (Pen pen = new Pen(Color.White, 2))
            {
                e.Graphics.DrawRectangle(pen, rec);
            }
            Invalidate();


        }
        //added load method

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                xe = e.X;
                ye = e.Y;
                if (xs < xe)
                {
                    if (ys < ye) rec = new Rectangle(xs, ys, (xe - xs), (ye - ys));
                    else rec = new Rectangle(xs, ye, (xe - xs), (ys - ye));
                }
                else
                {
                    if (ys < ye) rec = new Rectangle(xe, ys, (xs - xe), (ye - ys));
                    else rec = new Rectangle(xe, ye, (xs - xe), (ys - ye));
                }
                this.Invalidate();
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                // e.consume();  
                xs = e.X;
                ys = e.Y; // starting point y
                this.Invalidate();
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            rec = new Rectangle(0, 0, 0, 0);
            if (e.Button == MouseButtons.Left)
            {
                int z, w;
                //e.consume();

                //xe = e.X;
                //ye = e.Y;
                if (xs > xe)
                {
                    z = xs;
                    xs = xe;
                    xe = z;
                }
                if (ys > ye)
                {
                    z = ys;
                    ys = ye;
                    ye = z;
                }
                w = (xe - xs);
                z = (ye - ys);
                if ((w < 2) && (z < 2)) initvalues();
                else
                {
                    if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
                    else xe = (int)((float)xs + (float)z * xy);
                    xende = xstart + xzoom * (double)xe;
                    yende = ystart + yzoom * (double)ye;
                    xstart += xzoom * (double)xs;
                    ystart += yzoom * (double)ys;
                }
                xzoom = (xende - xstart) / (double)x1;
                yzoom = (yende - ystart) / (double)y1;

                mandelbrot();

                string stringxzoom = xzoom.ToString();
                string stringyzoom = yzoom.ToString();
                string stringystart = ystart.ToString();
                string stringxstart = xstart.ToString();
                string[] lines = { stringxzoom, stringyzoom, stringxstart, stringystart };

                System.IO.File.WriteAllLines(@"C:\Users\Public\Writelines.txt", lines);
                this.Invalidate();
                //Repaint();
            }
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
init();
start();
this.DoubleBuffered=true;
}
//将HSB从HSB.cs转换为RGB的代码。所有代码都是您的,所以我使它占用更少的空间。
公共结构HSBColor
{
浮动h;
浮子;
浮球b;
INTA;
公共HSBColor(float h,float s,float b){this.a=0xff;this.h=Math.Min(Math.Max(h,0),255);this.s=Math.Min(Math.Max(s,0),255);this.b=Math.Min(Math.Max(b,0,255);}
公共HSBColor(inta,float h,float s,float b){this.a=a;this.h=Math.Min(Math.Max(h,0),255);this.s=Math.Min(Math.Max(s,0),255);this.b=Math.Min(Math.Max(b,0,255);}
公共浮点H{get{return H;}}
公共浮点S{get{return S;}}
公共浮点B{get{return B;}}
公共int A{get{return A;}}
公共颜色{get{returnfromHSB(this);}
来自HSB的公共静态颜色(HSBColor HSBColor)
{
浮动r=HSB颜色.b;
浮色g=HSB颜色b;
浮动b=hsb颜色.b;
如果(hsbColor.s!=0)
{
float max=hsbColor.b;float dif=hsbColor.b*hsbColor.s/255f;float min=hsbColor.b-dif;float h=hsbColor.h*360f/255f;
如果(h<60f){r=max;g=h*dif/60f+min;b=min;}
如果(h<120f){r=-(h-120f)*dif/60f+min;g=max;b=min;}
如果(h<180f){r=min;g=max;b=(h-120f)*dif/60f+min;}
如果(h<240f){r=min;g=-(h-240f)*dif/60f+min;b=max;}
如果(h<300f){r=(h-240f)*dif/60f+min;g=min;b=max;}
else if(hxe)
{
z=xs;
xs=xe;
xe=z;
}
如果(ys>ye)
{
z=ys;
ys=ye;
ye=z;
}
w=(xe-xs);
z=(ye-ys);
如果((w<2)和(&(z<2))初始值();
其他的
{
如果((浮点)w>(浮点)z*xy))ye=(int)((浮点)ys+(浮点)w/xy);
else xe=(int)((浮点)xs+(浮点)z*xy);
xende=xstart+xzoom*(双)xe;
yende=ystart+yzoom*(双)ye;
xstart+=xzoom*(双)xs;
ystart+=yzoom*(双)ys;
}
xzoom=(xende-xstart)/(双)x1;
yzoom=(延德-伊斯特)/(双)y1;
曼德布罗特();
字符串stringxzoom=xzoom.ToString();
字符串stringyzoom=yzoom.ToString();
stringystart=ystart.ToString();
字符串stringxstart=xstart.ToString();
string[]行={stringxzoom、stringyzoom、stringxstart、stringystart};
System.IO.File.writeAllines(@“C:\Users\Public\Writelines.txt”,行);
这个。使无效();
//重新油漆();
}
}
私有void restartToolStripMenuItem\u单击(对象发送方,事件参数e)
{
Application.Restart();
}
private void exitToolStripMenuItem1\u单击(对象发送者,事件参数e)
{
Application.Exit();
}
private void menuToolStripMenuItem\u单击(对象发送方,事件参数e)
{
}
私有无效菜单已单击IP1\u项(对象发送者、工具条目的ClickedEventArgs e)
{
}
}
}
将Form1()代码更改为

        InitializeComponent();
        init();
        start();
        this.DoubleBuffered = true;

        this.pictureBox1.Image = bitmap;

您执行了InitializeComponent调用(应自动生成),但您从未将生成的位图设置为pictureBox的图像。此外,您可能希望将pictureBox大小模式设置为缩放和放大它。

感谢您在两个网站上的帮助:)