Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:19:04+00:00 2026-05-27T19:19:04+00:00

I have the code below that is supposed to draw lines from the top

  • 0

I have the code below that is supposed to draw lines from the top to the bottom of a fullscreen form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Graphics g;
        public Form1()
        {
            InitializeComponent();
            g = CreateGraphics();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Black);
            for (int i = 0; i < this.ClientSize.Height; i++)
            {
                g.DrawLine(pen, 100, i, 50, i);
            }
        }
    }
}

The form in question (Form1) is maximized, borderless and topmost. Result of the code is, form is displayed, lines are drawn one after another but when the number i of the loop reaches 1055 DrawLine starts not working and until the end of the loop from then on notghing else is drawn, therefore there is a blank space at the bottom of the form.

some extra information

My desktop resolution is 1920 x 1080

this.Size = 1920 x 1080

this.ClientSize = 1920 x 1080

Anyone is welcomed to create an empty project, set form properties (maximized, topmost, borderless) and copy-paste this code to reproduce the problem I have.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:19:05+00:00Added an answer on May 27, 2026 at 7:19 pm

    Here’s fixed code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
    //         Graphics g;
            public Form1()
            {
                InitializeComponent();
    //            g = CreateGraphics();
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Pen pen = new Pen(Color.Black);
                for (int i = 0; i < this.ClientSize.Height; i++)
                {
       //             g.DrawLine(pen, 100, i, 50, i);
                    e.Graphics.DrawLine(pen, 100, i, 50, i);
                }
            }
        }
    }
    

    The graphics object is meant to be used right away and then disposed. The main reason is, there is a real windows Device Context (HDC) that is used by the graphics object. This is a limited resource, and if you tie up too many in the program windows will cut the program off. If you have too many programs using too many, you’ll actually crash windows’ UI, or at least parts of it, although each subsequent version of windows adds protections to prevent this. Disposing the HDC explicitly and immediately with .Dispose or the using construct, when you’re done drawing, is required; by the time the GC gets around to it, you could have used them all up.

    The framework will create the Graphics and Dispose of it for you in the paint event. So when you’re drawing in paint (recommended), you don’t have to dispose the graphics.

    Any time you use creategraphics on a form, or Graphics.FromImage or similar, you should use it and dispose of it ASAP.

    Because of this design and the requirements that go with it, the HDC makes assumptions about the window at the moment it is grabbed. In the interem, the window could maximize or resize itself before drawing on it occurs again. The paint event also grabs another one from the same window handle – an unusual usage of HDC – so it is probably one of these issues which is causing the strange behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So below I have a code in C++ that is supposed to invert the
I have the code below: namespace WindowMover { using System.Windows.Forms; static class Logic {
I have the code below that is supposed to parse an XML document. I'm
I have this javascript code below that uses jquery, it is suppoed to be
I have the code below that hides and shows the navigational bar. It is
Background: I have a WPF UserControl (MainControl - not shown in code below) that
I have a plot (sample code pasted below) that I am trying to add
The code below works great. I have a MySQL database that contains book titles
I have a code like this below, which gives me a $link that equals
I have a data that looks like this . And my code below simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.