Oct 27th, 2005, 09:09 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Looking for Favor (image resize)
I am looking for a favor here... I am going to sleep soon, and I just can not get this to work....
Can someone write code which will work will work with the following dimensions:
[/quote]Form Height: 15 000
Form Width: 13 000
picture1.height = 12 000
picture1.width = 13 000
1 label with the percentage that it is shrunk or grew.[/quote]
All I want is a picture load into the picture1 and resized accordingly (and give me the percentage of the resize in the label)... Also, I do not want it stretched too much on one side...
If someone could give me this code I would be grateful...
Last edited by Zeratulsdomain; Oct 31st, 2005 at 04:01 PM .
Oct 28th, 2005, 02:26 AM
#2
Frenzied Member
Re: Looking for Favor (image resize)
try doing it this way, coding it is something u'll have to do u're self
get the aspect ratio of the image (x:y)
decrease the width of the image to what u want
now decrease the height of the image accrding to the aspect ratio
as for the percentage, %=(newX/oldX)*100
Oct 28th, 2005, 09:39 AM
#3
Re: Looking for Favor (image resize)
Try this:
VB Code:
Public Sub LoadPic(ByVal sFileName As String)
Dim pic As StdPicture
Dim nWidth As Long
Dim nHeight As Long
Dim nWidthOrig As Long
Dim nHeightOrig As Long
Dim hDiff As Double, wDiff As Double, nDiff As Double
Set pic = LoadPicture(sFileName)
With Picture1
.AutoRedraw = True
nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
'The picture box width is assumed to be larger then the height
If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
wDiff = nWidthOrig / .ScaleWidth
hDiff = nHeightOrig / .ScaleHeight
nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
End If
If nDiff Then
nWidth = nWidthOrig / nDiff
nHeight = nHeightOrig / nDiff
Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
End If
.Cls
.PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
(.ScaleHeight - nHeight) \ 2, _
nWidth, nHeight, , , , , vbSrcCopy
End With
End Sub
Oct 28th, 2005, 05:43 PM
#4
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
VB Code:
Public Sub LoadPic(ByVal sFileName As String)
Dim pic As StdPicture
Dim nWidth As Long
Dim nHeight As Long
Dim nWidthOrig As Long
Dim nHeightOrig As Long
Dim hDiff As Double, wDiff As Double, nDiff As Double
Set pic = LoadPicture(sFileName)
With Picture1
.AutoRedraw = True
nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
'The picture box width is assumed to be larger then the height
If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
wDiff = nWidthOrig / .ScaleWidth
hDiff = nHeightOrig / .ScaleHeight
nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
End If
If nDiff Then
nWidth = nWidthOrig / nDiff
nHeight = nHeightOrig / nDiff
Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
End If
.Cls
.PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
(.ScaleHeight - nHeight) \ 2, _
nWidth, nHeight, , , , , vbSrcCopy
End With
End Sub
Private Sub Command1_Click()
LoadPic ("C:\Documents and Settings\Chris\Desktop\IMG\sky13.jpg")
End Sub
When I click on the image I get a invalid procedure or call argument.
The following code is in big bright yellow:
VB Code:
.PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
(.ScaleHeight - nHeight) \ 2, _
nWidth, nHeight, , , , , vbSrcCopy
Oct 28th, 2005, 06:51 PM
#5
Re: Looking for Favor (image resize)
Is the name of your control Picture1? You shoud use Option Explicit to help you find errors.
Oct 29th, 2005, 02:49 PM
#6
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Yes I have a picture1 and a label1
Oct 29th, 2005, 02:55 PM
#7
Re: Looking for Favor (image resize)
Where did you paste the code? It should reside in the same Form as you have the Picture1 picturebox in.
Oct 29th, 2005, 03:34 PM
#8
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
I only have one form.... I have a Picture1, Label1 and a Command1
Command1 has:
VB Code:
LoadPic ("C:\Documents and Settings\Chris\Desktop\IMG\sky13.jpg")
The rest of the code was just copy/pasted
Oct 29th, 2005, 03:58 PM
#9
Re: Looking for Favor (image resize)
Oct 29th, 2005, 04:03 PM
#10
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Oct 29th, 2005, 04:05 PM
#11
Re: Looking for Favor (image resize)
I just put a picturebox, a label and a command button on a Form. In the click event of the command button I call the above procedure and it works just fine.
Oct 29th, 2005, 10:37 PM
#12
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Can you upload the project then?
Right from the very beginning I had a bad felling this image thing would screw me in at every corner (hence why I quickly gave up on it after 1-2 hours and posted this tread)...
Oct 30th, 2005, 07:28 AM
#13
Re: Looking for Favor (image resize)
OK, here is my test project.
Attached Files
Oct 30th, 2005, 03:24 PM
#14
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Originally Posted by
Joacim Andersson
OK, here is my test project.
That doesn't give me a error... But it doesnt work when I make the window bigger (I use 2048 by 1536)
Oct 30th, 2005, 07:46 PM
#15
Re: Looking for Favor (image resize)
Sorry, but I don't understand when you say "it doesn't work when I make the window bigger"??? Do you mean that if you maximize the window and then it doesn't work???
Oct 30th, 2005, 08:48 PM
#16
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Sry, I was wrong... The bug with that code is if the image is smaller then the picture box it will not be displayed.
Oct 31st, 2005, 06:46 AM
#17
Re: Looking for Favor (image resize)
Oh yeah... Try this:
VB Code:
Public Sub LoadPic(ByVal sFileName As String)
Dim pic As StdPicture
Dim nWidth As Long
Dim nHeight As Long
Dim nWidthOrig As Long
Dim nHeightOrig As Long
Dim hDiff As Double, wDiff As Double, nDiff As Double
Set pic = LoadPicture(sFileName)
With Picture1
.AutoRedraw = True
nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
nWidth = nWidthOrig
nHeight = nHeightOrig
'The picture box width is assumed to be larger then the height
If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
wDiff = nWidthOrig / .ScaleWidth
hDiff = nHeightOrig / .ScaleHeight
nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
End If
If nDiff Then
nWidth = nWidthOrig / nDiff
nHeight = nHeightOrig / nDiff
Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
End If
.Cls
.PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
(.ScaleHeight - nHeight) \ 2, _
nWidth, nHeight, , , , , vbSrcCopy
End With
End Sub
Oct 31st, 2005, 04:00 PM
#18
Thread Starter
Hyperactive Member
Re: Looking for Favor (image resize)
Tnx man (takes Advil)...
Hopefully Ill finish my application tonight.
Nov 3rd, 2005, 11:40 PM
#19
Hyperactive Member
Re: Looking for Favor (image resize)
Originally Posted by
Joacim Andersson
Oh yeah... Try this:
VB Code:
Public Sub LoadPic(ByVal sFileName As String)
Dim pic As StdPicture
Dim nWidth As Long
Dim nHeight As Long
Dim nWidthOrig As Long
Dim nHeightOrig As Long
Dim hDiff As Double, wDiff As Double, nDiff As Double
Set pic = LoadPicture(sFileName)
With Picture1
.AutoRedraw = True
nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
nWidth = nWidthOrig
nHeight = nHeightOrig
'The picture box width is assumed to be larger then the height
If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
wDiff = nWidthOrig / .ScaleWidth
hDiff = nHeightOrig / .ScaleHeight
nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
End If
If nDiff Then
nWidth = nWidthOrig / nDiff
nHeight = nHeightOrig / nDiff
Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
End If
.Cls
.PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
(.ScaleHeight - nHeight) \ 2, _
nWidth, nHeight, , , , , vbSrcCopy
End With
End Sub
hi joacim,
your code works well. i currently need help from the similar senario. your code work perfectly when i insert a huge file and it load beatifully in to teh picturebox .how can i rescale a smaller image to fit the picturebox(stretching of smaller image to fit a bigger picture box?
???
ocw
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