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

  • Home
  • SEARCH
  • 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 7550271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:10:09+00:00 2026-05-30T10:10:09+00:00

I am designing a simple picture viewer with ability to do some basic image

  • 0

I am designing a simple picture viewer with ability to do some basic image processing. At the moment I have the problem of keeping the PictureBox centered inside a TabPage all the time as well as keeping the picturebox width and size same as the picture its showing. So far I had no success.

I have the following code that I call in form constructor to position it in center. it works the first time to center the picturebox:

private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None)
{
    if (makeImageNull) picBoxView.Image = null;
    picBoxView.Dock = dockStyle;

    var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width / 2) / 2);
    var yPoint = tabImageView.Location.Y;

    var width = tabImageView.Width / 2;
    var height = (tabImageView.Height / 2) - toolStripImageView.Height;

    if (picBoxView.Image == null) return;

    //Resize image according to width
    picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false); 

    picBoxView.Location = new Point(xPoint, yPoint);
    picBoxView.Width = width;
    picBoxView.Height = height;
}

But it does not resize the picturebox to its image (you can see the black part that is back color for the picturebox control):

IT is ok the first time

The problem is getting worse as soon as I resize the form, the picturebox position will goes to top:

Form resized

I call the code above in form’s resize event as well, no idea why it works when application starts. Would be nice if someone can tell me what properties I should take care of to achieve a nicely centered picturebox which always is as big as its image.

  • 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-05-30T10:10:10+00:00Added an answer on May 30, 2026 at 10:10 am

    It’s pretty easy if you just set the Anchor style to none:

    picBoxView = new PictureBox();
    picBoxView.SizeMode = PictureBoxSizeMode.AutoSize;
    picBoxView.Anchor = AnchorStyles.None;
    tabImageView.Controls.Add(picBoxView);
    CenterPictureBox(picBoxView, myImage);
    

    Then just center the PictureBox initially whenever you change the image of the PictureBox:

    private void CenterPictureBox(PictureBox picBox, Bitmap picImage) {
      picBox.Image = picImage;
      picBox.Location = new Point((picBox.Parent.ClientSize.Width / 2) - (picImage.Width / 2),
                                  (picBox.Parent.ClientSize.Height / 2) - (picImage.Height / 2));
      picBox.Refresh();
    }
    

    Having the Anchor = None will center the PictureBox control for you whenever the parent container gets resized because it “isn’t” anchored to the default Left and Top locations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Atleast defining my problem is simple... I have a Input control (Text) and a
I just learnt basic Servlets and JSP technology and am designing a simple website
I'm designing a fairly simple web site but as I don't have much experience
I'm designing a simple web-app in CI. I have a registration controller, and a
I am designing a simple model in Django(1.3) which has 3 fields and some
I am designing a simple Android game and I am having problem with collusion.
I'm designing a simple web app for some elderly family members, 1 of whom
I am looking for some help on designing a simple pivot so that I
I'm designing a simple GUI. I have Widgets, which have children and one parent.
I am designing a simple internal framework for handling time series data. Given that

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.