473,545 Members | 4,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB6: Resizing Images

1 New Member
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 ImageReductionA mount As Long = 0)
Dim ParentRatio As Single
Dim PictureRatio As Single
Dim ContainerWidth As Single
Dim ContainerHeight As Single
Dim ContainerContro l As Control
With ImageBox
.Visible = False
PictureRatio = .Width / .Height
On Error Resume Next
ContainerWidth = .Container.Scal eWidth
If Err.Number Then
ContainerWidth = .Container.Widt h
ContainerHeight = .Container.Heig ht
Else
ContainerHeight = .Container.Scal eHeight
End If
ParentRatio = ContainerWidth / ContainerHeight
If ParentRatio < PictureRatio Then
.Width = ContainerWidth - 2 * ImageReductionA mount
.Height = .Width / PictureRatio
Else
.Height = ContainerHeight - 2 * ImageReductionA mount
.Width = .Height * PictureRatio
End If
.Move (ContainerWidth - .Width) \ 2, _
(ContainerHeigh t - .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 23862
Pragash
3 New Member
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.wid th
img.height = img.picture.hei ght
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.wi dth/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.he ight/img.height)
end if
img.left = 0
img.top = 0
End Sub
Feb 3 '07 #2
BowlandHack
1 New Member
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.wid th
img.height = img.picture.hei ght
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.wi dth/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.he ight/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.Stretc h = False
ImgScale.Pictur e = LoadPicture(Pic Fle)
ImgScale.Stretc h = 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.Visibl e = True


End Sub
Feb 10 '07 #3
xiogster
1 New Member
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.wid th
img.height = img.picture.hei ght
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.wi dth/img.width)
end if

if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.he ight/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.Wi dth / img.Width)

Else
img.Height = pic.Height
img.Width = img.Width / (img.Picture.He ight / 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
5037
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
1873
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. I compared the smooth image resampling used in PHP to create good quality thumbnails of large images, used commonly in image galleries. i compared...
0
1287
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 different size. OK, I use ExternalMethod which uses PIL library to resize these images. External method: def xImageScale(img_file, maxx, maxy): from...
2
3002
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 before uploading them, he didn't do it. If there was a way to do this using ASP, it would solve all problems.
2
3337
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
3622
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 figure out how to do it. Any ideas on how to rescale the image with the button?
1
2114
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 sizes are a lot bigger(in file size) than the actual image being uploaded. The actual image being uploaded was around 22000bytes The smaller...
5
6183
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 either resolution 800 X 600 or 1152 X 864 it takes up the entire explorer size. In screen resolution 800 X 600, if I insert a pictures of 24 X 24...
1
3661
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 I'm worried that they will simply upload 1Mb photos from their camera and wonder why the performance goes down the pan. I've used ASPJpeg on another...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.