Thread: Generating an image of squares?

  1. #1
    Registered User
    Join Date
    Jul 2007

    Generating an image of squares?


    I'm having some trouble to generate this image, with my code:

    My current problem is that I can't get the outer spacing around the green squares to appear. I've defined "g" as the outer spacing of the green squares, except its not producing the results that I need. Also I have trouble trying to generate the middle rectangles, which are double the width of the squares.

    Can someone please help me with these problems?

    Here's my code:
    namespace asn
        public partial class Question : Form
            public Question()
                Text = "Question ";
                BackColor = Color.Blue;
                ResizeRedraw = true;
                SetStyle(ControlStyles.UserPaint, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                SetStyle(ControlStyles.DoubleBuffer, true);
            protected override void OnPaint(PaintEventArgs e)
                Graphics grfx = e.Graphics;
                grfx.SmoothingMode = SmoothingMode.HighQuality;
                grfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
                int cx = ClientSize.Width;
                int cy = ClientSize.Height;
                int half_point = cx / 2;
                int s = 10;
                int g = 20; //for the outer gap
                if (cx > half_point)
                    SolidBrush black_brush = new SolidBrush(Color.Black);
                    Rectangle rect = new Rectangle(0, 0, half_point, cy);
                    grfx.FillRectangle(black_brush, rect);
                //Generate the small squares
                for (int i = 0; i < cx; i++)
                    i += s;
                    for (int j = 0; j < cy; j++)
                        SolidBrush green_brush = new SolidBrush(Color.GreenYellow);
                        grfx.FillRectangle(green_brush, i, j, s, s);
                        if (i == half_point)
                            grfx.FillRectangle(green_brush, half_point, j, s * 2, s);
                        j += g;
                    i += 10;
    Last edited by vopo; 09-21-2008 at 02:55 AM.

  2. #2
    Registered User
    Join Date
    Aug 2008
    a) is the size of the control fixed? if not, you hardcoded g to 20, but the width/height changes.
    b) you are generating way too many green squares (cx * cy)
    c) you should call Dispose() on all those brushes you create, or just use the static class Brushes to use what you want.

  3. #3
    Registered User
    Join Date
    Jul 2007
    The size of the image is not fixed, its designed to stretch. While its stretching its suppose to add more squares as it stretches on the x and y axis.

  4. #4
    Registered User
    Join Date
    Oct 2008
    having looked at the img you provided may i suggest that the "rectangles " in the middle are not rectangle but 2 squares side by side ?

    this may make your life easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  4. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM