Panel.BackgroundImage problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
Why are you setting the Panel's BackgroundImage property in the Forms
OnPaint method? Is this somehow necessary in your situation?
 
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.)
 
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.
 
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 said:
Why are you setting the Panel's BackgroundImage property in the Forms
OnPaint method? Is this somehow necessary in your situation?
 
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?
 
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.
 
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.
 
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?
 
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.
 
Hi Sharon,
Thanks for your feedback!
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.)
 
Back
Top