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

  • Home
  • SEARCH
  • 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 9170671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:03:28+00:00 2026-06-17T16:03:28+00:00

i have a class written in c# inherited from Control like below. class MyImage:Control

  • 0

i have a class written in c# inherited from Control like below.

    class MyImage:Control
    {
    private Bitmap bitmap;
    public MyImage(int width, int height)
    {
        this.Width = width;
        this.Height = height;
        bitmap = new Bitmap(width,height);
        Graphics gr = Graphics.FromImage(bitmap);
        gr.FillRectangle(Brushes.BlueViolet,0,0,width,height);
        this.CreateGraphics().DrawImage(bitmap,0,0);
    }        
   }

And from my main form i create an object of this class. and add thid object to the form, like below.

private void button1_Click(object sender, EventArgs e)
{
        MyImage m = new MyImage(100,100);
        m.Left = 100;
        m.Top = 100;
        this.Controls.Add(m);
}

but it doesnt appear on the form. What is the problem.
Thanks.

  • 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-06-17T16:03:29+00:00Added an answer on June 17, 2026 at 4:03 pm

    You should not draw anything in a class constructor. You should override OnPaint method and draw all of your custom graphics here.

    You can write someting like this:

    public partial class MyImage : Control
    {
        public MyImage()
        {
            InitializeComponent();
    
            bitmap = new Lazy<Bitmap>(InitializeBitmap);    
        }
    
        private Lazy<Bitmap> bitmap;
        private Bitmap InitializeBitmap()
        {
            var myImage = new Bitmap(Width, Height);
            using(var gr = Graphics.FromImage(myImage))
            {
                gr.FillRectangle(Brushes.BlueViolet, 0, 0, Width, Height);
            }
    
            return myImage;
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
    
            pe.Graphics.DrawImage(bitmap.Value, 0, 0);
        }       
    }
    

    The recepient code:

    private void button1_Click(object sender, EventArgs e)
    {
      var m = new MyImage(100,100)
      {
        Width = 100,
        Height = 100,
        Left = 100,
        Top = 100
      }
    
      Controls.Add(m);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class doing some jobs like GridView inherit from System.Web.UI.WebControls.WebControl. public
Per a client's request I have written a communication class that inherits from webclient.
I have a class written in C++ that uses also some definitions from cuda_runtime.h,
I have a class I've written to act like an enumeration: abstract class Enum
I have written a custom SessionStoreProvider class that inherits from the SessionStateStoreProviderBase. I have
I have a simple ViewModel class like public class TestViewModel { public String Information
I have written the following method in a class that inherits from the Thread
I have a custom UI control which has a JavaScript class written around the
I have written this class-methods for .net 2.0 to create objects from '|'-separated strings
I have written a .Net 4.0 Winforms Numeric Editor control (which inherits from TextBox),

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.