By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

Panel.BackgroundImage problem

P: n/a
I’m using a Panel control in my form to display a background image.
So in the Form OnPaint() I wrote:
myPanel.BackgroundImage = m_bmp;

While the m_bmp is the Form Bitmap member.

The problem is that the image is not shown on the screen.

But if change the code in the OnPaint() to:
Bitmap bmp = new Bitmap(m_bmp, m_bmp.Width, m_bmp.Height);
myPanel.BackgroundImage = bmp;

It works fine.

After doing some more testing, I found that if the image that I’m attaching
to the myPanel.BackgroundImage is a class member of the Form * It does not
work.
But if the attached image is a local image of the OnPaint() * It works OK.

Can anybody tell me why it happens and how to make it work with a bitmap
member?
---------
Thanks
Sharon
Nov 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Why are you setting the Panel's BackgroundImage property in the Forms
OnPaint method? Is this somehow necessary in your situation?

--
Tim Wilson
..NET Compact Framework MVP

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:9E**********************************@microsof t.com...
I'm using a Panel control in my form to display a background image.
So in the Form OnPaint() I wrote:
myPanel.BackgroundImage = m_bmp;

While the m_bmp is the Form Bitmap member.

The problem is that the image is not shown on the screen.

But if change the code in the OnPaint() to:
Bitmap bmp = new Bitmap(m_bmp, m_bmp.Width, m_bmp.Height);
myPanel.BackgroundImage = bmp;

It works fine.

After doing some more testing, I found that if the image that I'm attaching to the myPanel.BackgroundImage is a class member of the Form ? It does not
work.
But if the attached image is a local image of the OnPaint() ? It works OK.

Can anybody tell me why it happens and how to make it work with a bitmap
member?
---------
Thanks
Sharon

Nov 23 '05 #2

P: n/a
Hi Sharon,
Welcome to MSDN Newsgroup!

I don't make sure how you use a class bitmap member in code. However, if we
use it as follows, it could work well. You could refer to the following
code and if I misunderstood you, please feel free to let me know. Thanks!

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private Bitmap m_bmp;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
panel1.BackgroundImage = m_bmp;
}

#region Windows

private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(56, 56);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(128, 96);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

//
// Bitmap
//
this.m_bmp = new Bitmap("c:\\temp\\1.bmp");

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

If there is any question, please feel free to join the community and we are
here to support you at your convenience. Thanks again and have a nice day.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 24 '05 #3

P: n/a
Hi Terry,

The code you have posted is very simple and this is what I'm using, but off
course that I'm doing some; I mean that I have some more code, but it does
not change the idea.

So, as you probably already understand, I still have this whirred problem.

Any idea will be warmly welcome.
-------
Thanks
Sharon
--
Thanks
Sharon
""TerryFei"" wrote:
Hi Sharon,
Welcome to MSDN Newsgroup!

I don't make sure how you use a class bitmap member in code. However, if we
use it as follows, it could work well. You could refer to the following
code and if I misunderstood you, please feel free to let me know. Thanks!

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private Bitmap m_bmp;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
panel1.BackgroundImage = m_bmp;
}

#region Windows

private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(56, 56);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(128, 96);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

//
// Bitmap
//
this.m_bmp = new Bitmap("c:\\temp\\1.bmp");

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

If there is any question, please feel free to join the community and we are
here to support you at your convenience. Thanks again and have a nice day.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 24 '05 #4

P: n/a
Hi Tim,

It a bit complicated to explain, but the Idea is that I'm writing my own
control to replace the PictureBox control that will be able to handle very
large images (about 850MB) with 1 Bit Per pixel format.

On the panel (which is the control windows I'm drawing on) I'm drawing using
the P/Invoke for GDI function, and then I'm doing Panel.BackgroundImage =
m_bmp, where the bmp is a transparent Bitmap that contain some drawn details.
And the stage of Panel.BackgroundImage = m_bmp does not show the bmp on the
panel when the m_bmp is data menber of the Form.
It only work if the bmp is a local variable of the OnPaint().

Any Idea?
-------
Thanks
Sharon
--
Thanks
Sharon
"Tim Wilson" wrote:
Why are you setting the Panel's BackgroundImage property in the Forms
OnPaint method? Is this somehow necessary in your situation?

--
Tim Wilson
..NET Compact Framework MVP

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:9E**********************************@microsof t.com...
I'm using a Panel control in my form to display a background image.
So in the Form OnPaint() I wrote:
myPanel.BackgroundImage = m_bmp;

While the m_bmp is the Form Bitmap member.

The problem is that the image is not shown on the screen.

But if change the code in the OnPaint() to:
Bitmap bmp = new Bitmap(m_bmp, m_bmp.Width, m_bmp.Height);
myPanel.BackgroundImage = bmp;

It works fine.

After doing some more testing, I found that if the image that I'm

attaching
to the myPanel.BackgroundImage is a class member of the Form ? It does not
work.
But if the attached image is a local image of the OnPaint() ? It works OK.

Can anybody tell me why it happens and how to make it work with a bitmap
member?
---------
Thanks
Sharon


Nov 24 '05 #5

P: n/a
Hi Tim,

It a bit complicated to explain, but the Idea is that I'm writing my own
control to replace the PictureBox control that will be able to handle very
large images (about 850MB) with 1 Bit Per pixel format.

On the panel (which is the control windows I'm drawing on) I'm drawing using
the P/Invoke for GDI function, and then I'm doing Panel.BackgroundImage =
m_bmp, where the bmp is a transparent Bitmap that contain some drawn details.
And the stage of Panel.BackgroundImage = m_bmp does not show the bmp on the
panel when the m_bmp is data menber of the Form.
It only work if the bmp is a local variable of the OnPaint().

Any Idea?
-------
Thanks
Sharon
Nov 24 '05 #6

P: n/a
So the routine that loads the image into the Bitmap object works if the
object is a local variable but does not work if the object is stored at the
class level? How are you loading the image into the Bitmap object? Maybe if
you post the code that you're using to render the image we can take a look
at it.

--
Tim Wilson
..NET Compact Framework MVP

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
Hi Tim,

It a bit complicated to explain, but the Idea is that I'm writing my own
control to replace the PictureBox control that will be able to handle very
large images (about 850MB) with 1 Bit Per pixel format.

On the panel (which is the control windows I'm drawing on) I'm drawing using the P/Invoke for GDI function, and then I'm doing Panel.BackgroundImage =
m_bmp, where the bmp is a transparent Bitmap that contain some drawn details. And the stage of Panel.BackgroundImage = m_bmp does not show the bmp on the panel when the m_bmp is data menber of the Form.
It only work if the bmp is a local variable of the OnPaint().

Any Idea?
-------
Thanks
Sharon

Nov 24 '05 #7

P: n/a
Ok, the code is too large to post in here, so I summed it up, and here it is.

////////////////////////////////////////////////////////////////////////////////////////////////

m_bmp = new Bitmap(myPanel.Width, myPanel.Height);
m_bmpGrp = Graphics.FromImage(m_mapImg);
m_bmpGrp.Clear(Color.Transparent);

...... // some code

m_bmpGrp.DrawImage(otherBitmap_1 ,0, 0, m_mapImg.Width, m_mapImg.Height);
m_bmpGrp.DrawImage(otherBitmap_2 ,0, 0, m_mapImg.Width, m_mapImg.Height);

protected override void OnPaint(PaintEventArgs e)
{
// Drawing on the Panel window using the GDI function StretchDIBits(...)

m_DrawArea.BackgroundImage = m_bmp; // The m_bmp does not show on screen.

// If in here I do:
// m_bmp.Save("MapImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
// I can see in the file that the image is well drawn.
}

////////////////////////////////////////////////////////////////////////////////////////////////
I hope it give some more info.
----------
Thanks
Sharon
Nov 24 '05 #8

P: n/a
What happens if you draw the image directly to the form within the forms
OnPaint?

e.Graphics.DrawImage(m_bmp, ...);

Does it properly paint on the forms background?

--
Tim Wilson
..NET Compact Framework MVP

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:A1**********************************@microsof t.com...
Ok, the code is too large to post in here, so I summed it up, and here it is.
////////////////////////////////////////////////////////////////////////////
////////////////////
m_bmp = new Bitmap(myPanel.Width, myPanel.Height);
m_bmpGrp = Graphics.FromImage(m_mapImg);
m_bmpGrp.Clear(Color.Transparent);

..... // some code

m_bmpGrp.DrawImage(otherBitmap_1 ,0, 0, m_mapImg.Width, m_mapImg.Height);
m_bmpGrp.DrawImage(otherBitmap_2 ,0, 0, m_mapImg.Width, m_mapImg.Height);

protected override void OnPaint(PaintEventArgs e)
{
// Drawing on the Panel window using the GDI function StretchDIBits(...)

m_DrawArea.BackgroundImage = m_bmp; // The m_bmp does not show on screen.

// If in here I do:
// m_bmp.Save("MapImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
// I can see in the file that the image is well drawn.
}

////////////////////////////////////////////////////////////////////////////
////////////////////

I hope it give some more info.
----------
Thanks
Sharon

Nov 24 '05 #9

P: n/a
Yes, it does paint on the forms background?

Mmmm..... ?

-----
Thanks
Sharon
Nov 27 '05 #10

P: n/a
What if you create a custom control to handle the painting of the image
using its OnPaint override and the DrawImage method? Perhaps it's something
in the way that the image is handled through the BackgroundImage property of
the Panel.

--
Tim Wilson
..NET Compact Framework MVP

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:4A**********************************@microsof t.com...
Yes, it does paint on the forms background?

Mmmm..... ?

-----
Thanks
Sharon

Nov 27 '05 #11

P: n/a
Hi Sharon,
Thanks for your feedback!
I mean that I have some more code, but it does not change the idea.

In this scenario, I hope you can provide me a simplified sample, which
reproduces your issue so that I could debug it on our side. I think it will
help us get closer to resolving your issue, so I appreciate your time in
performing them.

If there is any question, please feel free to let me know. Thanks for your
understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 28 '05 #12

P: n/a
I think it's too much.

I think I'll stick with your first idea.

--
Thanks
Sharon
Nov 28 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.