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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:22:22+00:00 2026-06-08T20:22:22+00:00

I want programmatically put an image into the form which is not active. Image

  • 0

I want programmatically put an image into the form which is not active.

Image myImage = new Image();
gfx = Form.ActiveForm.CreateGraphics();
gfx.DrawImage(myImage, 0,0);

It’s will be working perfectly only if the form is active, but it’s make no sense when the form is not active, just returns me an error:

In an instance of an object reference not set to an object.

how to handle the form which is not active in my application and put a picture to it?

upd 1

I did instance, and turned on DoubleBuffered property (true) but nothing happens:

Form1 form = new Form1();
gfx = form.CreateGraphics();
gfx.DrawImage(bmp, 0, 0);

upd 2 More Source, it’s my added class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;

namespace testimg
{
    class doimg
    {

        public void picture()
        {

            // some staffs to get a picture, so it's in bmp object now.

            gfx = this. // watch picture below
            gfx.DrawImage(bmp, 0, 0);

            // I tried to use PictureBox, but it's the same issue (I can't handle it on a form)
            PictureBox pb = new PictureBox();
            pb.CreateGraphics();
            pb.DrawToBitmap(bmp,pb.ClientRectangle);

        }

    }
}

and a picture for the Arif Eqbal solution: no graph methods for this (pic)

upd 3

The full source what I have (timer is on a 10 sec)

Form1.cs

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 testimg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            doimg pic = new doimg();

            pic.picture();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
     }
}

doimg.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;


namespace testimg
{
    class doimg
    {

        public void picture()
        {

            // some staffs to get a picture, so it's in bmp object now.
            Bitmap bmp = new Bitmap(200, 200,    System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics gfx = Graphics.FromImage(bmp);
            gfx = Form1.ActiveForm.CreateGraphics(); // works well with active form
            gfx.DrawImage(bmp, 0, 0);


        }


     }
}

and whole archive with project http://www.filedropper.com/testimg

as you can see all running well while the program is active. Still need help.

  • 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-08T20:22:23+00:00Added an answer on June 8, 2026 at 8:22 pm

    Part of the problem is that when you are setting your bitmap the Form is not updating, expecially if it is minimized or doesn’t have focus. You also have the issue that since it is not being set in the Paint Event Handler you have no retention of the Image. Try something like this, Note the Invalidate call and the Timer was set for 10,000 ms. This is a variation of @LarsTech’s answer if you are working with multiple Forms his is the way to go.

    public partial class Form1 : Form
    {
        Bitmap myImage;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            myImage = new Bitmap("Your Image Name Here");
            Invalidate();
    
        }
    
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
    
            if (myImage != null)
                e.Graphics.DrawImage(myImage,0,0);
    
        }
    
    
    }
    

    Modifed my Example to work with the OP’s structure:

    My original example does work when the form does not have focus. Your main problem with the error is using Form1.ActiveForm when it is not the active form, it will return null which is causing your error. Use the Forms Paint Event and Invalidate the Form to insure that the Image is painted.

    Form1.cs

    namespace testimg
    {
        public partial class Form1 : Form
        {
            Bitmap myImage;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                myImage = doimg.picture();
                Invalidate();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                if (myImage != null)
                    e.Graphics.DrawImage(myImage,0,0);
            }
        }
    }
    

    doimg.cs

    namespace testimg
    {
        static class  doimg
        {
            static Color[] clr = new Color[] { Color.Red, Color.Blue, Color.Black, Color.Violet, Color.Wheat };
            static  int count = 0;
            static  public Bitmap picture()
            {
    
                // some staffs to get a picture, so it's in bmp object now.
                Bitmap bmp = new Bitmap(200, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    
                // Added some drawing to bitmap to test functionality
                Graphics gfx = Graphics.FromImage(bmp);
                gfx.FillEllipse(new SolidBrush(clr[count]),new Rectangle(0,0,199,199));
    
                gfx.Dispose();
    
                if (count >= 4)
                    count = 0;
                else
                    count += 1;
    
                return bmp;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to put an image in my UILabel which should be right aligned
I want to programmatically create a new log4j ConsoleAppender and add it as an
Placing an image inside a Span (here : HtmlGenericControl ) programmatically Want to have
I have a simple button and I want to put an image inside, so
Programmatically I am creating buttons and keeping some background image using setBackgroundImage. I want
I have an application which has many icons (Buttons). I want to put a
I'm not sure how to put this best into a title, but this is
using 1.5, i want to programmatically create an mms and insert it into inbox.
I am creating table rows programmatically and i want to put a view that
I have a list definition in my solution, and i want programmatically be able

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.