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 7170673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:15:35+00:00 2026-05-28T15:15:35+00:00

I did the following: public partial class Form1 : Form { public UserControl uc

  • 0

I did the following:

public partial class Form1 : Form
{
  public UserControl uc = new UserControl();

  private void Form1_Load(object sender, EventArgs e)
  {
    uc.Width = 100;
    uc.Height = 20;
    uc.BackColor = Color.White;

    uc.Paint += new PaintEventHandler((object s, PaintEventArgs pe) => {
      Graphics g = ((UserControl)s).CreateGraphics();
      g.DrawString("hello", this.Font, Brushes.Black, 0, 0);
    });

    uc.Visible = true;
    this.Controls.Add(uc);

    Bitmap bmp = new Bitmap(uc.Width, uc.Height);
    uc.DrawToBitmap(bmp, uc.ClientRectangle);
    bmp.Save("c:\\my_image.png", System.Drawing.Imaging.ImageFormat.Png);
  }

  private void button1_Click(object sender, EventArgs e)
  {
    Bitmap bmp = new Bitmap(uc.Width, uc.Height);
    uc.DrawToBitmap(bmp, uc.ClientRectangle);
    bmp.Save("c:\\my_image.png", System.Drawing.Imaging.ImageFormat.Png);
  }
}

Now, I see “hello” string shown properly on the form, but my_image.png file shows blank white background only. Clicking button1 has the same result. Why? And, more confusing thing happens if I write the above code in VB.NET; when button1 clicked, even white background is gone; uc behaves as if it was newly created, with width and height equal to 150px.
What am I missing?

  • 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-28T15:15:36+00:00Added an answer on May 28, 2026 at 3:15 pm

    Your Paint code is wrong. You’re passed a Graphics object in the PaintEventArgs.
    You don’t need to (and should not) call CreateGraphics.

    uc.Paint += new PaintEventHandler((object s, PaintEventArgs pe) => {
          pe.Graphics.DrawString("hello", this.Font, Brushes.Black, 0, 0);
    });
    

    The image file that you’re creating is probably blank because the form’s client area hasn’t been drawn yet at the time that the DrawToBitmap code is executed in the Load event.

    It should work fine in response to the button click, though. Which raises the question of why the code is in both places to begin with.

    It’s hard to say what the problem with your VB.NET code is, considering you didn’t show it. But it would be a good idea to do all of this UserControl creation/initialization inside of the form’s constructor, rather than in response to the Load event.

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

Sidebar

Related Questions

In the following: public class p { short? mID; short? dID; } short id
If I write the following code: public void Execute() { var stream = new
I did it by creating OLE object with Delphi in 2000/NT/XP as following: Voice
I have a method like the following: public void ImportDataCommand() { // some data
I have the following code: public abstract class BaseController : Controller { public IUserService
I have the following model (playframework): @Entity public class Album extends Model{ public Album(){
I have the following classes: public class ComplexType { public long? Code { get;
I have the following class: public class EmailData { public string FirstName{ set; get;
I did the following to upper case the first letter in each word but
I did the following steps to use the CDialog in win 32 application: Changed

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.