Hello everybody,

I'm new here and in C# and I'm trying to create a nonogram solver, but have some problems with graphics(I can't even find out it with debugger)...
When I turn on my nonograms, loading works perfectly, but i see only red cross in main window:
http://www.mypicx.com/06212011/nono/
so i guess it's a first my problem... Here's the code:

public Point sizeHint() //done
        {
            return new Point((nono.colcount + nono.taskWidth) * 14, (nono.rowcount + nono.taskHeight) * 14);
        }

public virtual void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.loadFile("candle.jap"); // Just for testing.
            using (Graphics gBmp = e.Graphics)
            {
                
                //hGridStep=sizeHint().Y/(colcount+taskWidth);
	        //vGridStep=sizeHint().X/(rowcount+taskHeight);
                hGridStep = 14;
                vGridStep = 14;
                //hGridStep = 
                int Width = hGridStep * (nono.taskWidth + nono.colcount);
                int Height = (nono.taskHeight + nono.rowcount) * vGridStep;
                Point RequiredSize = new Point(Width, Height);
                //string cache;
                this.Width = Width;
                this.Height = Height;
                viewCache = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                //Graphics viewBitmap = Graphics.FromImage(viewCache);

                if ((viewCache.Width != RequiredSize.X) && (viewCache.Height != RequiredSize.Y))
                {
                    thumbGridStep = Math.Min((nono.taskWidth * hGridStep - 4) / nono.colcount, (nono.taskHeight * vGridStep - 4) / nono.rowcount);
                    thumbPaddingLeft = (nono.taskWidth * hGridStep - nono.colcount * thumbGridStep) / 2;
                    thumbPaddingTop = (nono.taskHeight * vGridStep - nono.rowcount * thumbGridStep) / 2;
                    viewCache = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                    Graphics viewBitmap = Graphics.FromImage(viewCache);
                    viewBitmap.FillRectangle(new SolidBrush(TaskBackgrCl), 0, 0, Width, nono.taskHeight * vGridStep); // top
                    viewBitmap.FillRectangle(new SolidBrush(TaskBackgrCl), 0, nono.taskHeight * vGridStep, nono.taskWidth * hGridStep, nono.rowcount * vGridStep); // left
                    //
                    // Draw lines
                    //
                    //Pen GridLine = new Pen(GridLineCl);
                    _pen.Color = GridLineCl;
                    for (int i = 1; i < nono.colcount; i += (i + 1) % 5 == 0 ? 2 : 1)
                        viewBitmap.DrawLine(_pen, (nono.taskWidth + i) * hGridStep, 0, (nono.taskWidth + i) * hGridStep, Height); // vertical
                    for (int i = 1; i < nono.rowcount; i += (i + 1) % 5 == 0 ? 2 : 1)
                        viewBitmap.DrawLine(_pen, 0, (nono.taskHeight + i) * vGridStep, Width, (nono.taskHeight + i) * vGridStep); // horizontal
                    //
                    // Lines in task
                    //
                    _pen.Color = Color.Black;
                    for (int i = 1; i < nono.taskHeight; i++)
                        viewBitmap.DrawLine(_pen, nono.taskWidth * hGridStep, i * vGridStep, Width, i * vGridStep);	//horz
                    for (int i = 1; i < nono.taskWidth; i++)
                        viewBitmap.DrawLine(_pen, i * hGridStep, nono.taskHeight * vGridStep, i * hGridStep, Height);	//vertical
                    viewBitmap.DrawRectangle(_pen, thumbPaddingLeft - 1, thumbPaddingTop - 1, thumbGridStep * nono.colcount + 1, thumbGridStep * nono.rowcount + 1);	//Frame for thumbnail

                    //
                    // Fifth line
                    //
                    //Pen FifthLine = new Pen(FifthLineCl);
                    _pen.Color = FifthLineCl;
                    for (int i = 0; i < nono.colcount; i += 5)
                        viewBitmap.DrawLine(_pen, (nono.taskWidth + i) * hGridStep, 0, (nono.taskWidth + i) * hGridStep, Height); // vertical
                    for (int i = 0; i < nono.rowcount; i += 5) viewBitmap.DrawLine(_pen, 0, (nono.taskHeight + i) * vGridStep, Width, (nono.taskHeight + i) * vGridStep);		//horizontal

                    for (int i = 0; i < nono.colcount; i++) drawTaskForCol(viewBitmap, i, false);
                    for (int i = 0; i < nono.rowcount; i++) drawTaskForRow(viewBitmap, i, false);
                    //
                    // Drawing filed and thumbnail
                    //
                    for (int i = 0; i < nono.rowcount; i++)
                    {
                        for (int k = 0; k < nono.colcount; k++)
                        {
                            viewBitmap.FillRectangle(new SolidBrush(colorForCell(nono.field[k][i])), hGridStep * (nono.taskWidth + k) + 1, vGridStep * (nono.taskHeight + i) + 1, hGridStep - 1, vGridStep - 1);
                            viewBitmap.FillRectangle(new SolidBrush(colorForCell(nono.field[k][i])), thumbGridStep * k + thumbPaddingLeft, thumbGridStep * i + thumbPaddingTop, thumbGridStep, thumbGridStep);
                        }
                    }
                }

                using (Graphics p = Graphics.FromImage(viewCache))
                {
                    p.DrawImage(viewCache, 0, 25);
                    // bool return true if mouse on Form
                    if ((highlightedCell.X < nono.colcount) && (highlightedCell.Y < nono.rowcount))
                    {
                        if (0 <= highlightedCell.X)
                        {
                            for (int i = highlightedCell.X; i < Math.Min(highlightedCell.X + 2, nono.colcount); i++)
                            {
                                //Pen highlight = new Pen((i%5 == 0)?HighlightedGridLineCl:HighlightedFifthLineCl);
                                _pen.Color = i % 5 == 0 ? HighlightedGridLineCl : HighlightedFifthLineCl;
                                p.DrawLine(_pen, (nono.taskWidth + i) * hGridStep, 0, (nono.taskWidth + i) * hGridStep, Height - 1);
                            }

                            drawTaskForCol(p, highlightedCell.X, true);
                        }
                        if (0 <= highlightedCell.Y)
                        {
                            for (int i = highlightedCell.Y; i < Math.Min(highlightedCell.Y + 2, nono.rowcount); i++)
                            {
                                //Pen highlight = new Pen((i % 5 == 0) ? HighlightedGridLineCl : HighlightedFifthLineCl);
                                _pen.Color = i % 5 == 0 ? HighlightedGridLineCl : HighlightedFifthLineCl;
                                p.DrawLine(_pen, 0, (nono.taskHeight + i) * vGridStep, Width - 1, (nono.taskHeight + i) * vGridStep);
                            }
                            drawTaskForRow(p, highlightedCell.Y, true);
                        }
                    }
                    if (hasSelected)
                    {
                        //Pen pen = new Pen(Color.Blue);
                        _pen.Color = Color.Blue;
                        p.DrawRectangle(_pen, (selectedRect.X + nono.taskWidth) * hGridStep, (selectedRect.Y + nono.taskHeight) * vGridStep, selectedRect.Width * hGridStep, selectedRect.Height * vGridStep);
                    }
                }
            }
        }

public void drawTaskForCol(Graphics g, int col, bool highlighted) //done
        {
            using (g = this.CreateGraphics())
            {
                for (int i = 0; i < nono.columns[col].blocksCount; i++)
                {
                    drawTaskCell(g, hGridStep * (nono.taskWidth + col), vGridStep * (i + (nono.taskHeight - nono.columns[col].blocksCount)), highlighted, nono.columns[col].contradiction, nono.columns[col].blocks[i]);
                }
            }
        }

        public void drawTaskForRow(Graphics g, int row, bool highlighted) //done
        {
            using (g = this.CreateGraphics())
            {
                for (int i = 0; i < nono.rows[row].blocksCount; i++)
                {
                    drawTaskCell(g, hGridStep * (i + (nono.taskWidth - nono.rows[row].blocksCount)), vGridStep * (nono.taskHeight + row), highlighted, nono.rows[row].contradiction, nono.rows[row].blocks[i]);
                }
            }
        }

public void drawTaskCell(Graphics g, int x, int y, bool highlighted, bool err, Block b) //+-done
        {
            using (g = this.CreateGraphics())
            {
                //string cache;
                g.FillRectangle(new SolidBrush(highlighted ? (err ? HighlightedTaskErrBackgrCl : (Convert.ToBoolean(b.mark) ? HighlightedTaskFoundCl : HighlightedTaskBackgrCl)) : (err ? TaskErrBackgrCl : (Convert.ToBoolean(b.mark) ? TaskFoundCl : TaskBackgrCl))), x + 1, y + 1, hGridStep - 1, vGridStep - 1);
                _pen.Color = Color.Black;
            }
        }

I hope I've added all code that need, to look, what's wrong.
And second problems, but smaller... My window resizes wrong... :confused:

Why do you create a new Graphics object in every method?
e.g. line 140, after the using clause the object will not exist anymore.
If I where a method :twisted: I would just use the things passed to me, unless I was called Dispose or something...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.