Results 1 to 10 of 10

Thread: How to use a specific portion of an image as the form background

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    5

    How to use a specific portion of an image as the form background

    I know this sounds stupidly simple but I have been searching for hours. I am using a large (much larger than the screen) image as my form background and it works fine showing the top left corner of the image in the top left corner of the form. What I actually want to do is to use a specific portion of my image as the form background. I do not want to crop the image or make a dozen different versions of the image. It is not scrolling, resizing or being manipulated in any way, I just want to be able to specify the x and y coordinate of the image to be the top left corner of my form background image. I have search all the form parameters, looked at bitblt, etc. to no avail. Thank you for any assistance.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to use a specific portion of an image as the form background

    What are we using here? VB6? VB.Net? C#? WPF? WinForms?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    5

    Re: How to use a specific portion of an image as the form background

    Sorry for the omission, vb.net 2019 windows forms application.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to use a specific portion of an image as the form background

    At startup, I would create a bitmap and load the large image into it.
    Create a second bitmap of whatever size you want and then draw into that bitmap the portion you want from the first bitmap.
    You can then assign that bitmap to the BackgroundImage of the form, and dispose the first bitmap loaded. You don't dispose of the second bitmap, it is assigned to the Background Image, but the local reference to that bitmap will cease to exist when you leave the Load method.

    Actually, since the Clone method allows you to create a bitmap and specify the rectangle of the source you want, that would be the simplest.
    Code:
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Using bmp As New Bitmap("c:\c\img_5849.jpg")
          BackgroundImage = bmp.Clone(New Rectangle(1000, 1000, 1024, 768), Imaging.PixelFormat.Format32bppArgb)
        End Using 'dispose the original bitmap loaded
      End Sub
    Last edited by passel; Sep 17th, 2021 at 09:52 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to use a specific portion of an image as the form background

    Quote Originally Posted by passel View Post
    At startup, I would create a bitmap and load the large image into it.
    Create a second bitmap of whatever size you want and then draw into that bitmap the portion you want from the first bitmap.
    You can then assign that bitmap to the BackgroundImage of the form, and dispose the first bitmap loaded. You don't dispose of the second bitmap, it is assigned to the Background Image, but the local reference to that bitmap will cease to exist when you leave the Load method.
    If we were to honor his request to the letter of how he described it, a far easier solution would be to load the image into a PictureBox and simply shift it beyond the upper left corner of the Form by using negative co-ordinates for it's Left and Top properties. The Z order could then be manipulated to make sure it doesn't cover any controls on the Form.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to use a specific portion of an image as the form background

    Well, he did mention bitblt...
    And I assume he didn't want to have to crop the original image to make a bunch of different images to load.
    But, a movable picturebox would work as well.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    5

    Re: How to use a specific portion of an image as the form background

    If what I am seeing is accurate, you cannot adjust the location of the background image in a form. That answers my biggest question, you can't do it by simply setting parameters. This application has a number of controls and bitmaps pasted on the form so the if I use the PictureBox, how do I keep all of my added items on top? How do I adjust the Z order, never ran into the need for that. Making a bunch of images doesn't work because I have no idea what size the form is going to be since the user can adjust it to any size they want. There is no way to guarantee the user will be able to see the bottom right corner of the image.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    5

    Re: How to use a specific portion of an image as the form background

    Also, is there a good tutorial on how to use bitblt for this simple use? Not being a game programmer I never had a use for it. Another thought I have, is there a simple way to make a new smaller bitmap from another larger bitmap specifying the x, y corner and vertical and horizontal size? I could then assign that as the form background.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    5

    Re: How to use a specific portion of an image as the form background

    I found a simple solution. I created a bitmap with the full image, then created another bitmap to keep a partial image in. I used the bitmap.clone command to get the portion I wanted of the full image set the BackgroundImage to that portion bitmap. Works like a charm in the test run. I will do a full implementation tomorrow. Thanks for getting me to think in new directions.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to use a specific portion of an image as the form background

    Quote Originally Posted by garyaum View Post
    I found a simple solution. I created a bitmap with the full image, then created another bitmap to keep a partial image in. I used the bitmap.clone command to get the portion I wanted of the full image set the BackgroundImage to that portion bitmap. ...
    Did you find that in post #4?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width