简体   繁体   中英

Creating an image from a bitmap to a picturebox

I've been trying to create a bitmap, and use said bitmap to create an image which needs to be shown inside a picturebox. So far Google hasn't been of any help. The bitmap needs to be filled with black/white pixels defined in an array, but I've used Aliceblue for now.

When I run the code I get the error "value cant be null" at this line

Bitmap afbeelding = new Bitmap(resolutie, resolutie, g);

Here's what I've tried:

public void draw(Array array)
{

    Bitmap afbeelding = new Bitmap(resolutie, resolutie, g);

    for(int x = 0; x < array.Length; x++)
    {
        for (int y = 0; y < array.Length; y++)
        {
            afbeelding.SetPixel(x, y, Color.AliceBlue);
        }
    }

   pictureBox1.Image = afbeelding;
   //afbeelding = pictureBox1.CreateGraphics();
}

Does anyone know how to solve this? I'm not sure how to fill g since there isn't a DrawPixel function in Graphics

Assuming the array contains the definition of an image, you should probably fill the rows of the image with a chunk of the array, instead of filling the image horizontally and vertically with the array.

Let's suppose the array is for a 10 x 10 image, which would make the array 100 bytes long. You need to assign the first 10 bytes to the first row of the image, and so on. You also need to check the value of the array member whether to draw a pixel or not.

Example:

public void draw(bool[] array)
{
    Bitmap afbeelding = new Bitmap(resolutieX, resolutieY);

    for(int y = 0; y < resolutieY; y++)
    {
        for (int x = 0; x < resolutieX; x++)
        {
            if (array[y * resolutieX + x] == true)
                afbeelding.SetPixel(x, y, Color.Black);
            else
                afbeelding.SetPixel(x, y, Color.White);
        }
    }

    pictureBox1.Image = afbeelding;
}

To test this (assuming you have a button1 on the form):

int resolutieX = 100;
int resolutieY = 100;
Random R = new Random();    

private void button1_Click(object sender, EventArgs e)
{
    bool[] bArray = new bool[resolutieX * resolutieY];

    for (int i = 0; i < bArray.Length; i++)
        bArray[i] = R.Next(0, 2) == 1 ? true : false;

    draw(bArray);
}
    public void draw(int[] array)
    {

        Bitmap afbeelding = new Bitmap(11, 11);


        for (int i = 0; i < array.Length; i++)
        {
            afbeelding.SetPixel(array[i], array[i], Color.Black);
        }

        pictureBox1.Image = afbeelding;
        //afbeelding = pictureBox1.CreateGraphics();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        draw(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    }

Why not locking the pixel array? Check this out, it is faster:

BitmapData Class

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