473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

VB6: Resizing Images

I am having trouble getting images to fit inside either an image box or a picture box. I want to scale the picture to fit in side the box, without distortion. I found a post here that suggested a subroutine, but I this appeared to do nothin when the picture was loaded. Can anyone help? having an imagebox inside a picturebox, I used this code:

Private Sub SetImageBoxSize(ImageBox As Image, _
Optional ImageReductionAmount As Long = 0)
Dim ParentRatio As Single
Dim PictureRatio As Single
Dim ContainerWidth As Single
Dim ContainerHeight As Single
Dim ContainerControl As Control
With ImageBox
.Visible = False
PictureRatio = .Width / .Height
On Error Resume Next
ContainerWidth = .Container.ScaleWidth
If Err.Number Then
ContainerWidth = .Container.Width
ContainerHeight = .Container.Height
Else
ContainerHeight = .Container.ScaleHeight
End If
ParentRatio = ContainerWidth / ContainerHeight
If ParentRatio < PictureRatio Then
.Width = ContainerWidth - 2 * ImageReductionAmount
.Height = .Width / PictureRatio
Else
.Height = ContainerHeight - 2 * ImageReductionAmount
.Width = .Height * PictureRatio
End If
.Move (ContainerWidth - .Width) \ 2, _
(ContainerHeight - .Height) \ 2
.Visible = True
End With
End Sub

It resizes the image box to a correct ratio, but the picture inside stays at it's full size so you only see part of it.
Feb 14 '06 #1
3 23789
in my computer don't have vb so i could check this code but u can check this.
but if u understand the concept u can easly correct it.


image box name is "img"
picture box name is "pic"
commond button name is "cmd"
place the image control into picturebox and change the image control streach property to true.

Private Sub cmd_Click()
img.width = img.picture.width
img.height = img.picture.height
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.width/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.height/img.height)
end if
img.left = 0
img.top = 0
End Sub
Feb 3 '07 #2
in my computer don't have vb so i could check this code but u can check this.
but if u understand the concept u can easly correct it.


image box name is "img"
picture box name is "pic"
commond button name is "cmd"
place the image control into picturebox and change the image control streach property to true.

Private Sub cmd_Click()
img.width = img.picture.width
img.height = img.picture.height
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.width/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.height/img.height)
end if
img.left = 0
img.top = 0
End Sub

I have tried the following code and it works well...


Public Sub ScaleIMG(PicFle)

' This Sub-routine is designed as a physical add-in module
' {i.e. one which is copied into the parent application}

' PURPOSE
' The purpose of this subroutine is to proportionally scale any pictures
' down to the maximum size of a PictureBox Control inserted on a form,
' whilst maintaining the aspect ratio of the original picture! Pictures
' which are smaller than the PictureBox control are shown at their
' natural size.

' METHODOLOGY
' The Picture filename "PicFle" is derived within the main application
' and passed to this sub-routine as a parameter.

' The main application MUST HAVE:-
' =========

' 1. A Picture Box named <PicScale> on the default form whose
' width & height define the maximum dimensions of any image
' to be displayed.

' 2. An Image Box named <ImgScale> must be drawn WITHIN <PicScale>.
' This Image Box can be of any size and can lie anywhere within
' the Picture Box <PicScale>

' Pictures which are too large for the Picture Box will be scaled down to
' the dimensions of the Picture Box, whilst maintaining the aspect ratio
' of the image. Landscape oriented pictures will be constrained by the
' PicScale Width set at design time and portrait oriented pictures will
' be constrained to the height of PicScale. PicScale's dimensions are
' then changed to match the image being shown.

' Pictures which are smaller than PicScale will be shown at their natural
' size.

' ################################################## ########################

Static Init, DefH, DefW 'Static variables defined for this sub only

If Init = 0 Then
' ___This is the first time this session that this subroutine has been called
Init = 1 '<< Prevents this "If Then..." statement being called again this session
DefH = PicScale.Height '__ Save Original Picture Box Height at Design time value
DefW = PicScale.Width '__ Save Original Picture Box Width at Design time value
End If

'__ Restore Picture Box's Default Height & Width
PicScale.Height = DefH
PicScale.Width = DefW


'__Load Picture into ImageBox and stretch to it's natural size
ImgScale.Stretch = False
ImgScale.Picture = LoadPicture(PicFle)
ImgScale.Stretch = True

Iw = ImgScale.Width ' Width of picture loaded into Img
Ih = ImgScale.Height ' Height of Picture loaded into Img

Pw = PicScale.Width ' Width of Picture Box
Ph = PicScale.Height ' Height of Picture Box


If Iw > Ih Then
'____ Format = Landscape
Fcr = Pw / Iw ' Scale on width
Else
'____ Format = Square or Portait
Fcr = Ph / Ih ' Scale on height
End If


If Fcr < 1 Then
'__ i.e. The Image is larger than the Picture Box and we must
' shrink it, otherwise we leave it the same size!
ImgScale.Width = ImgScale.Width * Fcr
ImgScale.Height = ImgScale.Height * Fcr
End If


'__Re-assign PictureBox dimensions to fit the image
PicScale.Width = ImgScale.Width
PicScale.Height = ImgScale.Height

'__Position the Top LH corner of the ImageBox
' into the top LH corner of the PictureBox
ImgScale.Left = 0
ImgScale.Top = 0

ImgScale.Visible = True


End Sub
Feb 10 '07 #3
in my computer don't have vb so i could check this code but u can check this.
but if u understand the concept u can easly correct it.


image box name is "img"
picture box name is "pic"
commond button name is "cmd"
place the image control into picturebox and change the image control streach property to true.

Private Sub cmd_Click()
img.width = img.picture.width
img.height = img.picture.height
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.width/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.height/img.height)
end if
img.left = 0
img.top = 0
End Sub
If img.Width > img.Height Then
img.Width = pic.Width
img.Height = img.Height / (img.Picture.Width / img.Width)

Else
img.Height = pic.Height
img.Width = img.Width / (img.Picture.Height / img.Height)


End If

img.Left = 0
img.Top = 0
Mar 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Can someone please tell me where I can get some open source code for resizing images? I know such code has been written a million times and I don't feel like doing it again from scratch.
0
by: Michael Rostkowski | last post by:
I posted this in alt.php, but as a reply, so now I'm posting it here for people who might have missed it but still could find it useful. Thanks to Andy Hassall for giving me good information. ...
0
by: KK | last post by:
Hallo. I have Zope 2-7-0 on Linux platform and the following problem :) I'm sending an image through form on the pythonScript. I want script to resize image and save on the zope folder in 3...
2
by: Fredo Vincentis | last post by:
Is there any possibility to resize images using ASP? I provided one of my clients with a Content Management System using SAFileup and although I told him to resize images to a reasonable size...
2
by: Bernhard Gsöllpointner | last post by:
Hello ! Does anyody know how to load and resize jpg or gif files in a cpp 6.0 Project ? Thanks in advance bernhard.g@utanet.at
3
by: curtis.barrett | last post by:
Hi I am trying to convert some forms of mine from a 1024 X 768 size to a 800 X 600 size and everything works fine except for the buttons. I need to scale the images on the button but I can't...
1
by: Ron Vecchi | last post by:
I am using asp.net to upload an image and then perform resizing on it and saving the different sizes to file. The resized images were coming up and being displayed in the bowser fine but the image...
5
by: Maxi | last post by:
I have a 30X16 cells table in my html page. Table height and width are set to 100%. I have set size of every cell inside the table to 24 pixel. When I open the html page in maximize state in...
1
by: eglons | last post by:
Hi, I've put a site together for a client, who now wants to Content Manage both the text and images. Text isn't a problem and I've implemented this, image replacement again isn't a problem, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.