Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aligning a picture within a picture box.

105 views
Skip to first unread message

Kim Hawker

unread,
Apr 13, 2022, 12:29:53 AM4/13/22
to
I have a few picture boxes measuring w-700 x h-950, is there something I need to do to get it easier to crop pictures to align them to be more or less centered? Seems I have to really tweak / crop everything to be way to one side to get it to show up in a decent manner within the picture boxes. What am I doing wrong?

ObiWan

unread,
Apr 13, 2022, 5:49:14 AM4/13/22
to
:: On Tue, 12 Apr 2022 21:29:52 -0700 (PDT)
:: (microsoft.public.vb.general.discussion)
:: <ff887e98-9d25-465d...@googlegroups.com>
Sorry, didn't understand what you want to achieve; are you trying to
resize an image to fit into your picturebox or what ?



ObiWan

unread,
Apr 13, 2022, 6:10:22 AM4/13/22
to
:: On Wed, 13 Apr 2022 11:49:11 +0200
:: (microsoft.public.vb.general.discussion)
:: <20220413114...@mvps.org>
:: ObiWan <obi...@mvps.org> wrote:

> Sorry, didn't understand what you want to achieve; are you trying to
> resize an image to fit into your picturebox or what ?

just for a test, place an "Image" control on a form, set the "stretch"
property to true and then use something like

Set Image1.Picture = LoadPicture("c:\temp\example.bmp")

to load a picture into the image and check the result, is that what you
were asking for ??


Mayayana

unread,
Apr 13, 2022, 8:03:32 AM4/13/22
to
"Kim Hawker" <hawke...@gmail.com> wrote
Maybe PaintPicture?
https://www.techrepublic.com/article/utilize-vb6s-paintpicture-method-to-create-special-effects/

You didn't explain clearly what you want or what
you've tried. But if you want an image centered you
should be able to do that with PaintPicture. I'm not
sure if I've ever actually used it, but it looks like you
could use LoadPicture to put it into an autosizing,
hidden PB, then use the Picture property of that in
PaintPicture, using x1 and x2 to center the image.


ObiWan

unread,
Apr 13, 2022, 10:16:41 AM4/13/22
to
:: On Wed, 13 Apr 2022 12:10:18 +0200
:: (microsoft.public.vb.general.discussion)
:: <20220413121...@mvps.org>
here's another idea, let's say you use a "frame" to mark the place
where your image will be sitting and you place an "Image" control
inside the frame, done so, when loading an image you proceed this way

Set the Image control (let's call it Image1) "stretch" propery to true

Load the picture into Image1 (Set Image1.Picture = LoadPicture...)

Retrieve the original image size

lWidth = Image1.Picture.Width
lHeight= Image1.Picture.Height

now, compute the Image1 width/height as a percentage of the image
width/height so that Image1 size will be less or equal to the
containing frame control

Done so, move/resize the Image1 so that it will have the computed
width/height and will be centered inside the container frame and
finally set Image1.Visible = True to show the rescaled and centered
picture

Would this work for you ?


Kim Hawker

unread,
Apr 13, 2022, 10:26:04 AM4/13/22
to
On Wednesday, April 13, 2022 at 7:03:32 AM UTC-5, Mayayana wrote:

> Maybe PaintPicture?
> https://www.techrepublic.com/article/utilize-vb6s-paintpicture-method-to-create-special-effects/
>
> You didn't explain clearly what you want or what
> you've tried. But if you want an image centered you
> should be able to do that with PaintPicture. I'm not
> sure if I've ever actually used it, but it looks like you
> could use LoadPicture to put it into an autosizing,
> hidden PB, then use the Picture property of that in
> PaintPicture, using x1 and x2 to center the image.

OK, my picture gets loaded into the picturebox but I guess I should of asked it more like is there any way using controls or code to adjust picture left to right or up and down after picture is in the picturebox.

ObiWan

unread,
Apr 13, 2022, 11:15:53 AM4/13/22
to
:: On Wed, 13 Apr 2022 07:26:02 -0700 (PDT)
:: (microsoft.public.vb.general.discussion)
:: <23ddf684-2159-4ad0...@googlegroups.com>
:: Kim Hawker <hawke...@gmail.com> wrote:

> OK, my picture gets loaded into the picturebox but I guess I should
> of asked it more like is there any way using controls or code to
> adjust picture left to right or up and down after picture is in the
> picturebox.

Oh, but that's easy, let me try an example

put a picturebox (Picture1) on a form and size it as you need

add an horizontal and a vertical scrollbars (Hscroll1, Vscroll1) to the
right and bottom sides of the picturebox

add a second picturebox (Picture2) INSIDE the first one (Picture1) and
set Picture2 so that it will autoresize to the size of the image and
set its borderstyle to none

set both scrollbars to disabled (or invisible if you prefer)

now load your image into Picture2 (NOT Picture1, that's just a
container or, if you prefer, our "viewport"), Picture2 will resize to
the image size and if the image is bigger than Picture1 you'll only see
the top left "chunk" of the image

if the image is bigger than Picture1, enable (or show) the horizontal
and/or vertical scrollbars and set their max value to 1/2 of the width
or height of the image

now, in the scrollbars "scroll" event change the "top" or "left2 of
picture 2 to -1*value, where value is the scrollbar value

done ... ok, will need some refinement, but I think you got the idea







Message has been deleted
Message has been deleted

Kim Hawker

unread,
Apr 13, 2022, 2:14:16 PM4/13/22
to
On Wednesday, April 13, 2022 at 10:15:53 AM UTC-5, ObiWan wrote:
> :: On Wed, 13 Apr 2022 07:26:02 -0700 (PDT)
> put a picturebox (Picture1) on a form and size it as you need
>
> add an horizontal and a vertical scrollbars (Hscroll1, Vscroll1) to the
> right and bottom sides of the picturebox
ObiWan, Thank you, this is I believe is working at least much better then what I had before. Assam. Thank you very much for the Picturebox lesson.

ObiWan

unread,
Apr 14, 2022, 3:20:46 AM4/14/22
to
:: On Wed, 13 Apr 2022 11:14:15 -0700 (PDT)
:: (microsoft.public.vb.general.discussion)
:: <45f03b0f-ca87-4bd3...@googlegroups.com>
:: Kim Hawker <hawke...@gmail.com> wrote:

> ObiWan, Thank you, this is I believe is working at least much better
> then what I had before. Assam. Thank you very much for the Picturebox
> lesson.

One thing you may consider is turning the whole thing (pictures,
scrollbars...) into a "usercontrol", that way, whenever you'll need an
image control with the ability to scroll the image, you may just pick
the usercontrol and place it onto a form; add properties and events as
needed and there you go :)



Larry Serflaten

unread,
Apr 14, 2022, 9:42:09 AM4/14/22
to
Kim Hawker wrote:
> OK, my picture gets loaded into the picturebox but I guess I should of asked it more like is there any way using controls or code to adjust picture left to right or up and down after picture is in the picturebox.

Is that what you are looking for, or would rather have the picture resize for a
'best fit' inside the available area?

I would think, unless you are doing some sort of inspection, that you would
want to see the entire image, without having to scroll around or make other
adjustments.

As was indicated for a best fit, add an Image control inside the picture box
with its Stretch property set to True. You can then calculate its aspect ratio
and use that to adjust the Image control to show the picture.

LFS

Kim Hawker

unread,
Apr 14, 2022, 4:46:40 PM4/14/22
to
On Thursday, April 14, 2022 at 8:42:09 AM UTC-5, Larry Serflaten wrote:

> As was indicated for a best fit, add an Image control inside the picture box
> with its Stretch property set to True. You can then calculate its aspect ratio
> and use that to adjust the Image control to show the picture.
>

Hi Larry Serflaten, Just so I can see what you are suggesting, How do I get to the stretch properties?

ObiWan

unread,
Apr 15, 2022, 3:08:02 AM4/15/22
to
:: On Thu, 14 Apr 2022 13:46:38 -0700 (PDT)
:: (microsoft.public.vb.general.discussion)
:: <dbfb22bb-a058-4c62...@googlegroups.com>
:: Kim Hawker <hawke...@gmail.com> wrote:

> How do I get to the stretch properties?

Didn't you read my previous messages ?

Kim Hawker

unread,
Apr 15, 2022, 6:19:09 AM4/15/22
to
On Friday, April 15, 2022 at 2:08:02 AM UTC-5, ObiWan wrote:
> Didn't you read my previous messages ?
Ok, I played around with it, but don't think it's what I want. Thank you ObiWan for your persistence, for it's now that I found and tried it.

Larry Serflaten

unread,
Apr 15, 2022, 10:36:45 AM4/15/22
to
Kim Hawker wrote:

> Hi Larry Serflaten, Just so I can see what you are suggesting, How do I get to the stretch properties?

On a new form, add a picturebox, then place an Image control inside the picturebox. Paste in the
code below and add your file name to the LoadPicture command. You should be able to adjust
the picturebox to any size you want, and the code will find the best fit for the new size.
(Too bad there is no code formatting in this newsgroup!)

HTH
LFS

Private Sub Form_Load()
Dim scl, wid&, hgt&, std As StdPicture
'just in case
Picture1.ScaleMode = vbPixels
Image1.Stretch = True
' get file
Set std = LoadPicture( <Your File Here> )
' convert to pixels
wid = ScaleX(std.Width, vbHimetric, vbPixels)
hgt = ScaleY(std.Height, vbHimetric, vbPixels)
' determine best fit
If ((wid / Picture1.ScaleWidth) > (hgt / Picture1.ScaleHeight)) Then
' fit to width
scl = Picture1.ScaleWidth / wid
Image1.Move 0, (Picture1.ScaleHeight - hgt * scl) / 2, wid * scl, hgt * scl
Else
' fit to height
scl = Picture1.ScaleHeight / hgt
Image1.Move (Picture1.ScaleWidth - wid * scl) / 2, 0, wid * scl, hgt * scl
End If
' assign image
Image1.Picture = std
End Sub

Kim Hawker

unread,
Apr 15, 2022, 9:59:54 PM4/15/22
to
On Friday, April 15, 2022 at 9:36:45 AM UTC-5, Larry Serflaten wrote:

> On a new form, add a picturebox, then place an Image control inside the picturebox. Paste in the
> code below and add your file name to the LoadPicture command. You should be able to adjust
> the picturebox to any size you want, and the code will find the best fit for the new size.
> (Too bad there is no code formatting in this newsgroup!)

> Private Sub Form_Load()
> Dim scl, wid&, hgt&, std As StdPicture
> 'just in case
> Picture1.ScaleMode = vbPixels
> Image1.Stretch = True
> ' get file
> Set std = LoadPicture( <Your File Here> )
> ' convert to pixels
> wid = ScaleX(std.Width, vbHimetric, vbPixels)
> hgt = ScaleY(std.Height, vbHimetric, vbPixels)
> ' determine best fit
> If ((wid / Picture1.ScaleWidth) > (hgt / Picture1.ScaleHeight)) Then
> ' fit to width
> scl = Picture1.ScaleWidth / wid
> Image1.Move 0, (Picture1.ScaleHeight - hgt * scl) / 2, wid * scl, hgt * scl
> Else
> ' fit to height
> scl = Picture1.ScaleHeight / hgt
> Image1.Move (Picture1.ScaleWidth - wid * scl) / 2, 0, wid * scl, hgt * scl
> End If
> ' assign image
> Image1.Picture = std
> End Sub

Hi Larry Serflaten, I tried to follow your instructions, I get error, I took picture of error but this forum won't let me post an image.

Larry Serflaten

unread,
Apr 16, 2022, 5:56:53 AM4/16/22
to
Kim Hawker wrote:

> Hi Larry Serflaten, I tried to follow your instructions, I get error, I took picture of error but this forum won't let me post an image.

I suspect the error is this line:

> > Set std = LoadPicture( <Your File Here> )

< Your File Here > is not a valid file name. You must use a filename that is available on your computer.

Ex:
Set std = LoadPicture("C\:pictures\my cat.jpg" )

LFS

Message has been deleted
Message has been deleted

Mayayana

unread,
Apr 16, 2022, 7:56:18 AM4/16/22
to
"Kim Hawker" <hawke...@gmail.com> wrote

| image1.Stretch = Trust <-------- this is where the error highlights in
yellow

How did you post that, twice, without seeing
that you wrote "trust" instead of True?


Kim Hawker

unread,
Apr 16, 2022, 8:04:04 AM4/16/22
to
On Saturday, April 16, 2022 at 4:56:53 AM UTC-5, Larry Serflaten wrote:
> I suspect the error is this line:
> > > Set std = LoadPicture( <Your File Here> )
> < Your File Here > is not a valid file name. You must use a filename that is available on your computer.


> Ex:
> Set std = LoadPicture("C\:pictures\my cat.jpg" )


Here's what I have been trying with error
image1.Stretch = True <-------- this is where the error highlights in yellow
Set std = LoadPicture ("C:\tester\needed_files\Debbie.jpg")

Kim Hawker

unread,
Apr 16, 2022, 8:07:21 AM4/16/22
to
On Saturday, April 16, 2022 at 6:56:18 AM UTC-5, Mayayana wrote:


> | image1.Stretch = Trust <-------- this is where the error highlights in
> yellow
> I have is as follows
I did a copy and paste.

Larry Serflaten

unread,
Apr 16, 2022, 8:50:39 AM4/16/22
to
Kim Hawker wrote:

> > | image1.Stretch = Trust <-------- this is where the error highlights in

Keep working at it. I just tried copy/pasting the code I posted as instructed,
and except for the <Your File Here > issue, it worked right off.

Good luck!
LFS

Larry Serflaten

unread,
Apr 16, 2022, 9:14:42 AM4/16/22
to
Kim Hawker wrote:

> Here's what I have been trying with error
> image1.Stretch = True <-------- this is where the error highlights in yellow
> Set std = LoadPicture ("C:\tester\needed_files\Debbie.jpg")

Read my post again to follow the instructions.

Do you have a picturebox and an image control on the form?

HTH
LFS

Kim Hawker

unread,
Apr 16, 2022, 9:19:09 AM4/16/22
to
On Saturday, April 16, 2022 at 8:14:42 AM UTC-5, Larry Serflaten wrote:


> Do you have a picturebox and an image control on the form?

I have a single picturebox and I've got a ImageCombo inside the picturebox. is this the wrong item? ImageCombo versus ImageControll

Kim Hawker

unread,
Apr 16, 2022, 10:03:02 PM4/16/22
to
On Saturday, April 16, 2022 at 8:14:42 AM UTC-5, wrote:
> Kim Hawker wrote:

> Read my post again to follow the instructions.
>
> Do you have a picturebox and an image control on the form?

Larry Serflaten thanks for your persistence and commitment to help me get this up and running.
I finally got it working. Every one was calling if ImageControl so I was looking for a component named ImageControl but It’s actually just named “Image” I put an Image tool within my picture box and WaLa it all came up working. So again thanks for your efforts. Your very kind.

Message has been deleted

Stan Weiss

unread,
Apr 24, 2022, 6:02:29 PM4/24/22
to
Kim,
Not using anything that has been posted before. I use this to let my
Users load their logo into my software.

Stan

Private Sub Logo_Click()
On Error GoTo ErrHandlerOpen
    ChDrive App.Path
    With CommonDialog1
        .CancelError = True
        .InitDir = App.Path
        .Filter = "Bit Map Image Files (*.bmp)|*.bmp|GIF Files
(*.gif)|*.gif|JPG Files (*.jpg)|*.jpg|All Files (*.*)|*.*"
        .FilterIndex = 2
        .flags = &H4&     ' Don't show read only box
        .ShowOpen
        fName = .filename
    End With
    Image1 = LoadPicture(fName)
Exit Sub
'
ErrHandlerOpen:
Dummy = MsgBox("No Logo File has been Seleted", 48)
End Sub

On 4/24/2022 5:53 PM, Kim Hawker wrote:
> On Friday, April 15, 2022 at 9:36:45 AM UTC-5, Larry Serflaten wrote:
>
>> Private Sub Form_Load()
>> Dim scl, wid&, hgt&, std As StdPicture
>> 'just in case
>> Picture1.ScaleMode = vbPixels
>> Image1.Stretch = True
>> ' get file
>> Set std = LoadPicture( <Your File Here> )
>> ' convert to pixels
>> wid = ScaleX(std.Width, vbHimetric, vbPixels)
>> hgt = ScaleY(std.Height, vbHimetric, vbPixels)
>> ' determine best fit
>> If ((wid / Picture1.ScaleWidth) > (hgt / Picture1.ScaleHeight)) Then
>> ' fit to width
>> scl = Picture1.ScaleWidth / wid
>> Image1.Move 0, (Picture1.ScaleHeight - hgt * scl) / 2, wid * scl, hgt * scl
>> Else
>> ' fit to height
>> scl = Picture1.ScaleHeight / hgt
>> Image1.Move (Picture1.ScaleWidth - wid * scl) / 2, 0, wid * scl, hgt * scl
>> End If
>> ' assign image
>> Image1.Picture = std
>> End Sub
>
> I’ve been trying to get this to work when NOT on the form.load page, or to assign a variable to the load picture but have not been successful. Could someone help me to get this to work so I can do a random picture load.
> I’ve tried things like
> Test = “picture1.jpg”
> Set std = LoadPicture(App.Path & "\pictures\” & test)
>
> But without positive results.
>
>


--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Kim Hawker

unread,
Apr 24, 2022, 10:59:02 PM4/24/22
to
On Friday, April 15, 2022 at 9:36:45 AM UTC-5, Larry Serflaten wrote:

> Private Sub Form_Load()
> Dim scl, wid&, hgt&, std As StdPicture
> 'just in case
> Picture1.ScaleMode = vbPixels
> Image1.Stretch = True
> ' get file
> Set std = LoadPicture( <Your File Here> )
> ' convert to pixels
> wid = ScaleX(std.Width, vbHimetric, vbPixels)
> hgt = ScaleY(std.Height, vbHimetric, vbPixels)
> ' determine best fit
> If ((wid / Picture1.ScaleWidth) > (hgt / Picture1.ScaleHeight)) Then
> ' fit to width
> scl = Picture1.ScaleWidth / wid
> Image1.Move 0, (Picture1.ScaleHeight - hgt * scl) / 2, wid * scl, hgt * scl
> Else
> ' fit to height
> scl = Picture1.ScaleHeight / hgt
> Image1.Move (Picture1.ScaleWidth - wid * scl) / 2, 0, wid * scl, hgt * scl
> End If
> ' assign image
> Image1.Picture = std
> End Sub

I’ve been trying to get this to work when NOT on the form.load page, or to assign a variable to the load picture but have not been successful. Could someone help me to get this to work so I can do a random picture load.
I’ve tried things like
Test = “picture1.jpg”  This line being on or in a different Private sub.

Set std = LoadPicture(App.Path & "\pictures\” & test)  This being on the form.load page.

But without positive results.

Kim Hawker

unread,
Apr 25, 2022, 12:40:36 AM4/25/22
to
On Sunday, April 24, 2022 at 5:02:29 PM UTC-5, Stan Weiss wrote:

> Private Sub Logo_Click()
> On Error GoTo ErrHandlerOpen
> ChDrive App.Path
> With CommonDialog1
> .CancelError = True
> .InitDir = App.Path
> .Filter = "Bit Map Image Files (*.bmp)|*.bmp|GIF Files
> (*.gif)|*.gif|JPG Files (*.jpg)|*.jpg|All Files (*.*)|*.*"
> .FilterIndex = 2
> .flags = &H4& ' Don't show read only box
> .ShowOpen
> fName = .filename
> End With
> Image1 = LoadPicture(fName)
> Exit Sub
> '
> ErrHandlerOpen:
> Dummy = MsgBox("No Logo File has been Seleted", 48)
> End Sub
>

Thank you Stan, it’s nice to learn different ways of doing things so I appreciate your input and efforts.
For my current project this is not what I’m looking for, for it’s a button press with mouse that assigns the choice of pictures to be loaded among other things not a mouse click.

Message has been deleted

Kim Hawker

unread,
Apr 25, 2022, 3:40:25 PM4/25/22
to
It’s too complicated to try and explain here but just wanted to say I believe I’ve got it figured out.

Kim Hawker

unread,
Apr 25, 2022, 6:17:52 PM4/25/22
to
Now that I got it working with these new alignment tools you have to like all that but using the image tool at least with this coding has its own un-likeable issues so I for now will not be using the image control. Just saying.

Larry Serflaten

unread,
Apr 27, 2022, 5:06:55 PM4/27/22
to
Kim Hawker wrote:

> Now that I got it working with these new alignment tools you have to like all that but using the image tool at least with this coding has its own un-likeable issues so I for now will not be using the image control. Just saying.

That is OK, it is your project. All we know about your program is what you share in your posts. Your initial post said you have a few pictureboxes and want to center images on them. I posted a message about centering an image on a picturebox, but it seems you were unable to convert that to a function for multiple pictureboxes.

In any case, it does show the calculations used to center an image. Maybe they will be useful if you are still trying to center images.

LFS

Kim Hawker

unread,
Apr 27, 2022, 10:24:55 PM4/27/22
to
On Wednesday, April 27, 2022 at 4:06:55 PM UTC-5, Larry Serflaten wrote:

> That is OK, it is your project. All we know about your program is what you share in your posts. Your initial post said you have a few pictureboxes and want to center images on them. I posted a message about centering an image on a picturebox, but it seems you were unable to convert that to a function for multiple pictureboxes.
>
> In any case, it does show the calculations used to center an image. Maybe they will be useful if you are still trying to center images.
>
> LFS

Yes Larry, It’s all good, I have now done some reworking based on what you taught/showed me. And now am rethinking about using it after all, after I get a few more items worked out. You have been a great big assistance to me on this project I’m working on. I’m sorry if I offended you. I did not mean to do that. I love that you shared your sharing with me. Thank you again.

Larry Serflaten

unread,
Apr 28, 2022, 7:26:54 AM4/28/22
to
Kim Hawker wrote:

> Yes Larry, It’s all good, I have now done some reworking based on what you taught/showed me. And now am rethinking about using it after all, after I get a few more items worked out. You have been a great big assistance to me on this project I’m working on. I’m sorry if I offended you. I did not mean to do that. I love that you shared your sharing with me. Thank you again.

I am not offended, as I said, it is your project, you do what is best in your situation.

Just for your information; all of the items in the default tool box are Visual Basic intrinsic controls. You called them tools, but their class name is actually 'Control'. You can declare a variable as 'Control' and assign a control to it (if needed).

Right click the tool box and select Components to see a large list of other available controls.

HTH
LFS
0 new messages