-
Sep 17th, 2021, 05:32 AM
#1
Thread Starter
New Member
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.
-
Sep 17th, 2021, 05:49 AM
#2
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?
-
Sep 17th, 2021, 05:57 AM
#3
Thread Starter
New Member
Re: How to use a specific portion of an image as the form background
Sorry for the omission, vb.net 2019 windows forms application.
-
Sep 17th, 2021, 09:43 AM
#4
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
-
Sep 17th, 2021, 09:49 AM
#5
Re: How to use a specific portion of an image as the form background
Originally Posted by passel
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.
-
Sep 17th, 2021, 09:58 AM
#6
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
-
Sep 17th, 2021, 01:52 PM
#7
Thread Starter
New Member
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.
-
Sep 17th, 2021, 01:59 PM
#8
Thread Starter
New Member
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.
-
Sep 17th, 2021, 03:21 PM
#9
Thread Starter
New Member
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.
-
Sep 17th, 2021, 05:22 PM
#10
Re: How to use a specific portion of an image as the form background
Originally Posted by garyaum
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|