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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:53:36+00:00 2026-06-07T05:53:36+00:00

A simple code that should show an image using PictureBox doesn’t work (frm is

  • 0

A simple code that should show an image using PictureBox doesn’t work (frm is my form):

PictureBox pb = new PictureBox();
pb.Image = new Bitmap("1.jpg");
pb.SizeMode = PictureBoxSizeMode.Zoom;
frm.Controls.Add(pb);

When event with this code happens I have NullReferenceExcpetion

The error occurs at frm.Controls.Add(pb)

The exception is:

System.NullReferenceException: Object reference not set to an instance
of an object. at Form1.HotKeyManager_HotKeyPressed(Object sender,
HotKeyEventArgs e) in C:\Users\Алексей\Documents\Visual Studio
2010\Projects\NotepadCSharpSetup\WinFormsAgain\RealTrayForm\Test.cs:line
52

Full code :

static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
    Size ScreenSize = Screen.PrimaryScreen.Bounds.Size;

    Bitmap image = new Bitmap(ScreenSize.Width, ScreenSize.Height);
    using (Graphics g = Graphics.FromImage(image))
    {
        g.CopyFromScreen(Point.Empty, Point.Empty, ScreenSize);
    }
    Bitmap preview = new Bitmap(image.Width / 10, image.Height / 10);
    using (Graphics gr = Graphics.FromImage(preview))
    {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(image, new Rectangle(0, 0, image.Width / 10, image.Height / 10));
    }
    preview.Save("1.jpg");

    Form frm = (Form)sender;
    PictureBox pb = new PictureBox();
    pb.Image = new Bitmap("1.jpg");
    pb.SizeMode = PictureBoxSizeMode.Zoom;
    frm.Controls.Add(pb);

}
  • 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-07T05:53:38+00:00Added an answer on June 7, 2026 at 5:53 am

    I don’t believe the new keyword returns Null, unless you have no memory. The bet is that the sender is not the Form

    Form frm = (Form)sender;
    

    I think this line is null, that’s why frm.Controls.Add(pb) fails.

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

Sidebar

Related Questions

I'm using NASM 16 BITS. I'm trying to do a simple assembly code that
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
This is a simple code that open and print the file content. #include <stdio.h>
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
Here is a simple code that shows what I think is a bug when
I've implemented a TreeCellRenderer that returns a JCheckBox (simple code that the renderer extends
I am building a PhoneGap/Cordova app and have a simple code that uses http://ricostacruz.com/jquery.transit/
How do you guys write comments on some simple code that you use just
I have a simple java code that reads text csv file that contains sentences
I have a simple jQuery code that runs on a web page that has

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.