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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:52:09+00:00 2026-06-06T21:52:09+00:00

I was nicely supplied a code from Alex M, for painting the background image

  • 0

I was nicely supplied a code from Alex M, for painting the background image to a panel but realized that if a PictureBox‘s BackgroundImage has its Center image property set, the drawn image becomes stretched but not centered. I so far have this code:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(pictureBox1.BackgroundImage, 
        new Rectangle(pictureBox1.Location, pictureBox1.Size));
}

This draws the background image to the panel but if pictureBox1‘s background image property is set to CENTER, it does not paint the image in the Center of the Rectangle, but instead stretches the image to fit the rectangle.

The only possible solution I’ve found is here but I can’t make any sense of it.

  • 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-06T21:52:11+00:00Added an answer on June 6, 2026 at 9:52 pm

    The reason the image is being stretched is because the second parameter to DrawImage specifies the location and size of where you’re drawing the image, and you’re specifying the entire area of the picture box, not the area of the image itself.

    If you want to center it, try something like this:

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        var hPadding = (pictureBox1.Width - pictureBox1.BackgroundImage.Width) / 2;
        var vPadding = (pictureBox1.Height - pictureBox1.BackgroundImage.Height) / 2;
        var imgRect = new Rectangle(pictureBox1.Left + hPadding, pictureBox1.Top + vPadding, pictureBox1.BackgroundImage.Width, pictureBox1.BackgroundImage.Height);
        e.Graphics.DrawImage(pictureBox1.BackgroundImage, imgRect);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code works nicely on the iPhone simulator but, on Android 2.3, I
This code works nicely in for example Chrome, but not in Internet Explorer 8/9.
I had a nicely striped table with some nifty mouseover effects that was working
The code below works nicely. Each title in the MySQL table submission is printed
I can print out a date nicely based on the user's locale. But how
So I can make a div to scale nicely from it's center pivot: http://jsfiddle.net/uTDay/
The following code nicely animates adding a new string to the end of a
dnspython will do my DNS lookups very nicely, but it entirely ignores the contents
My python2 script uploads files nicely using this method but python3 is presenting problems
When interacting with F# libraries from IronPython it seems that Python function object are

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.