简体   繁体   中英

How can I generate a new Bitmap image upon mouse click in C#?

I've got the following code shown beneath. I want to be able to change the drawn image (a Mandelbrot) based on user input schaal (scale), when I click on the button on the GUI. The program is only able to draw one image upon running the program, with the default values.

private void Form1_Shown(object sender, EventArgs e)        
{     
    double max = 100;     
    Bitmap bitmap = new Bitmap(Figuur.Width, Figuur.Height);  
    for (int x = 0; x < Figuur.Width; x++)
    {     
        for (int y = 0; y < Figuur.Height; y++) 
        {     
            double a = (double)(x - (Figuur.Width * schaal)) / (double)(Figuur.Width * 0.05);    
            double b = (double)(y - (Figuur.Height * schaal)) / (double)(Figuur.Height * 0.05);      
            Mandelgetal getal = new Mandelgetal(a, b); 
            Mandelgetal waarde = new Mandelgetal(0, 0);
            int i = 0;
            while (i < max)
            {
                //i++; //v1
                waarde.Vermenigvuldig();
                waarde.Toevoegen(getal);
                if (waarde.Wortel() > 2.0)
                    break;
                else
                {
                    if (i % 2.0 == 0.0)
                    {
                        bitmap.SetPixel(x, y, Color.White);
                        i++; //v2
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, Color.Black);
                        i++; //v2
                    }
                } 
            }
            if (a * a + b * b > 4)
                bitmap.SetPixel(x, y, Color.Black);
            Figuur.Image = bitmap;
        }
    }
}

I think you should make it this way if you want to draw the image at the beginning and every time you click the button.

private void Form1_Shown(object sender, EventArgs e)        
{
    Scale();
}

private void But_Click(object sender, EventArgs e)
{
    Scale();
}

private void Scale()        
{     
    double max = 100;     
    Bitmap bitmap = new Bitmap(Figuur.Width, Figuur.Height);  
    for (int x = 0; x < Figuur.Width; x++)
    {     
        for (int y = 0; y < Figuur.Height; y++) 
        {     
            double a = (double)(x - (Figuur.Width * schaal)) / (double)(Figuur.Width * 0.05);    
            double b = (double)(y - (Figuur.Height * schaal)) / (double)(Figuur.Height * 0.05);      
            Mandelgetal getal = new Mandelgetal(a, b); 
            Mandelgetal waarde = new Mandelgetal(0, 0);
            int i = 0;
            while (i < max)
            {
                //i++; //v1
                waarde.Vermenigvuldig();
                waarde.Toevoegen(getal);
                if (waarde.Wortel() > 2.0)
                    break;
                else
                {
                    if (i % 2.0 == 0.0)
                    {
                        bitmap.SetPixel(x, y, Color.White);
                        i++; //v2
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, Color.Black);
                        i++; //v2
                    }
                } 
            }
            if (a * a + b * b > 4)
                bitmap.SetPixel(x, y, Color.Black);
            Figuur.Image = bitmap;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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