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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:50:00+00:00 2026-05-11T21:50:00+00:00

I have two controls which are both contained within a user control. Panel location:

  • 0

I have two controls which are both contained within a user control.

  • Panel location: 49, 400
  • Panel size: 787, 70
  • Picturebox location: 25, 0
  • Picturebox Size: 737, 700

Picturebox is seen underneath panel.

I could use some advice on the best way to determine the area of the PictureBox image covered by the panel, so I can copy the image onto the panel correctly in DrawImage call.

My answer is: I will copy screen from this area: 24,400 top left, it will be 714 wide, 70 high
But how do I automate to work with any combination of panel and picture box for a re-usable control?

Background if you need more info: The PictureBox contains an image of a map. The panel contains tools for manipulating the map, panel is positioned on top of PictureBox. Panel needs to be semi-transparent so the map image is still seen through it.

Because of the way that winforms paints transparency, (calling up through the control tree to have the parent paint its self) – when I paint my panel it takes on the color of the background of the usercontrol, not the image of the map below it.

My thoughts for this are: if I can copy the image of the map that is below the panel to the background of the panel and then paint my semi-transparent background I will be able to simulate the effect that designers have requested.

Original code from the panel is where I grab the image from the picture box.

            Dim x As Integer = GetXOffset()
            Dim y As Integer = GetYOffset()

            Dim sizeOfImage As Size = New Size(ClientRectangle.Width _
                                  , ClientRectangle.Height)
            bmpScreenshot = New Bitmap(sizeOfImage.Width, sizeOfImage.Height, _
                     PixelFormat.Format32bppArgb)

            gfxScreenshot = Graphics.FromImage(bmpScreenshot)
            Dim destrect As New Rectangle(ClientRectangle.X, ClientRectangle.Y, _
                        ClientRectangle.Width, ClientRectangle.Height)


            gfxScreenshot.DrawImage(mPictureBox1.Image, destrect, New Rectangle(x, y, _
               sizeOfImage.Width, sizeOfImage.Height), GraphicsUnit.Pixel)

I copy this image to the background in the OnPaint event.

If bmpScreenshot Is Nothing Then
            PushScreen()
        End If
        If Not bmpScreenshot Is Nothing Then
            pevent.Graphics.DrawImage(bmpScreenshot, GetPaintOffset())
        End If

And finally, after adding the change from the accepted answer, here is the modified code where the image is grabbed.

 Dim sizeOfImage As Size = New Size(ClientRectangle.Width _
                                  , ClientRectangle.Height)
            bmpScreenshot = New Bitmap(sizeOfImage.Width, sizeOfImage.Height, PixelFormat.Format32bppArgb)

            gfxScreenshot = Graphics.FromImage(bmpScreenshot)

            Dim rect As Rectangle = Rectangle.Intersect(mPictureBox1.Bounds, Bounds)
            Dim destrect As Rectangle = New Rectangle(rect.Left - Left, _
             rect.Top - Top, rect.Width, rect.Height)
            Dim imgrect As Rectangle = _
              New Rectangle(rect.Left - mPictureBox1.Bounds.Left, _
              rect.Top - mPictureBox1.Bounds.Top, rect.Width, rect.Height)

            gfxScreenshot.DrawImage(mPictureBox1.Image, destrect, _
               imgrect, GraphicsUnit.Pixel)
  • 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-11T21:50:00+00:00Added an answer on May 11, 2026 at 9:50 pm

    You can use the Rectangle.Intersect method (together with a tiny bit of calculation) to get the result you want. C# sample:

    Rectangle rect = Rectangle.Intersect(_pictureBox.Bounds, _panel.Bounds);
    rect = new Rectangle(rect.Top - _panel.Top, rect.Left - _panel.Left, rect.Width, rect.Height);
    e.Graphics.FillRectangle(Brushes.Red, rect);
    

    Update

    I played around with this a bit more and came up with the following solution, that I find a bit simpler and also a bit more robust (this time VB.NET code):

    Private Sub DrawPanelBackground(ByVal pictureBox As PictureBox, ByVal panel As Panel)
        If pictureBox.Image Is Nothing Then
            Exit Sub
        End If
    
        Dim rect As Rectangle = New Rectangle(pictureBox.Left - panel.Left, pictureBox.Top - panel.Top, pictureBox.Image.Width, pictureBox.Image.Height)
        Using g As Graphics = Panel.CreateGraphics()
            g.DrawImage(pictureBox.Image, rect)
        End Using
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I am using standard windows forms controls under C#. I have two controls which
I have two custom controls that are analogous to a node and the control
I've got two Silverlight Controls in my project, both have properties TeamId. I would
I have two very similar .aspx pages. Both of them contain a DropDownList control.
I have two web forms which are filled in by the user. They contain
I have a user control which contains a CustomValidator which is used according to
I have two TDBLookupComboBox controls that I'd like to connect to the same dataset,
I've got a form where I have two radio buttons and two interchangeable controls
I have two different grid controls on the same form. They share the same

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.