Results 1 to 4 of 4

Thread: Drawing in a PictureBox

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Drawing in a PictureBox

    Hi guys!

    What I ask is probably pretty simple but it's been a long time since I last used C# so bear with me.

    Basically I'm creating a program to explore cellular automata and what I need to do is be able to render an array of 1's and 0's as pixels on a picturebox control.

    All I need for you guys is to tell me how I could plot these pixel points on the control (so the control would act like a canvas of sorts).

    Thanks for the information.
    My Blog.

    Ryan Jones.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Drawing in a PictureBox

    It really depends exactly what you want to do with them. Either way I think you will be using GDI+ but it might be better to draw onto an Image and display that in the PictureBox or it might be better to draw on the PictureBox itself.

    Basically, you want to get a Graphics object and call one of its Draw methods; in your case probably DrawRectangle. To draw on an Image you'd call Graphics.FromImage and to draw on a PictureBox you'd handle the Paint event and use e.Graphics.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Drawing in a PictureBox

    Would you be able to give me an example by any chance?

    As for what I want to do with them I'll process the cellular automata (which is basically a large multi-dimensional array of 1's and 0's) then render each of those values as a pixel of the specified colour. Once it's done then the control will allow saving of the image etc.
    My Blog.

    Ryan Jones.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Drawing in a PictureBox

    Code:
                Bitmap bmp = new Bitmap(100, 100);
    
                int x, y;
    
                for (x = 0; x < bmp.Width; x++)
                {
                    for (y = 0; y < bmp.Height; y++)
                    {
                        Color pixelColor = bmp.GetPixel(x, y);
                        Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                        bmp.SetPixel(x, y, newColor);
                    }
                }
    
                pictureBox1.Image = bmp;
    Just change the loops to loop through your array dimensions instead of the bitmap. And set the Bitmaps x and y in the constructor to the appropriate dimenional size of the array
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width