Panel and Background Pictures

G

Guest

Hi there

I have a background image that I'm trying to place on a Panel. I'm trying to resize the image to fit the size of the panel but the image's size is read only. How do I make the picture always resize to the size of the panel? Is there a way of doing this

Here's a sample of my code

panel1.BackgroundImage = (Image.FromFile("C:\\bitmap1.bmp")
//panel1.BackgroundImage.Size = panel1.Size; //this does not work as the Size is Read Only.... How do I do this

Thanks for your help in advance
Tom.
 
K

Ken Tucker [MVP]

Hi,

Draw it yourself in paint event.

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
e.Graphics.DrawImage(Image.FromFile("C:\camera.bmp"),
Panel1.ClientRectangle)
End Sub

Ken
 
L

Lloyd Sheen

Check out the constructor for Bitmap. There is the following:

Initializes a new instance of the Bitmap class from the specified existing
image and with the specified size.
[Visual Basic] Public Sub New(Image, Integer, Integer)
[C#] public Bitmap(Image, int, int);

You can load your image and then create a new one to use with the new size.

Lloyd Sheen
 
B

Bob Powell [MVP]

Just draw the image in the Paint event..

panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(TheImage,panel1.ClientRectangle,0,0,TheImage.Width,
TheImage.Height,GraphicsUnit.Pixel);

//draw your foreground stuff here...
}

The GDI+ FAQ also has articles on how to resample or resize images.

--
Bob Powell [MVP]
Visual C#, System.Drawing

All you ever wanted to know about ListView custom drawing is in Well Formed.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*

The GDI+ FAQ: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41

*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*RSS*





Tom said:
Hi there,

I have a background image that I'm trying to place on a Panel. I'm trying
to resize the image to fit the size of the panel but the image's size is
read only. How do I make the picture always resize to the size of the panel?
Is there a way of doing this?
Here's a sample of my code:

panel1.BackgroundImage = (Image.FromFile("C:\\bitmap1.bmp");
//panel1.BackgroundImage.Size = panel1.Size; //this does not work as the
Size is Read Only.... How do I do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top