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.
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
Invalidatecall 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.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.ActiveFormwhen 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
doimg.cs