將一個圖片按比例縮放顯示在一個Frame中。
2024-07-21 02:16:23
供稿:網友
 
,歡迎訪問網頁設計愛好者web開發。代碼如下:
'form1.frm
version 5.00
object = "{f9043c88-f6f2-101a-a3c9-08002b2f49fb}#1.2#0"; "comdlg32.ocx"
begin vb.form form1 
 caption = "form1"
 clientheight = 5010
 clientleft = 60
 clienttop = 345
 clientwidth = 7800
 linktopic = "form1"
 scaleheight = 334
 scalemode = 3 'pixel
 scalewidth = 520
 startupposition = 3 '窗口缺省
 begin mscomdlg.commondialog commondialog1 
 left = 4635
 top = 3120
 _extentx = 847
 _extenty = 847
 _version = 393216
 end
 begin vb.frame frame1 
 caption = "frame1"
 height = 3000
 left = 4500
 tabindex = 2
 top = 30
 width = 3180
 begin vb.picturebox picture2 
 appearance = 0 'flat
 forecolor = &h80000008&
 height = 2625
 left = 120
 scaleheight = 173
 scalemode = 3 'pixel
 scalewidth = 194
 tabindex = 3
 top = 255
 width = 2940
 begin vb.image image1 
 height = 1575
 left = 465
 top = 390
 width = 1965
 end
 end
 end
 begin vb.commandbutton command1 
 caption = "&load picture"
 height = 330
 left = 5400
 tabindex = 0
 top = 3150
 width = 1425
 end
 begin vb.picturebox picture1 
 appearance = 0 'flat
 autosize = -1 'true
 borderstyle = 0 'none
 forecolor = &h80000008&
 height = 4425
 left = 60
 scaleheight = 4425
 scalewidth = 4380
 tabindex = 1
 top = 105
 width = 4380
 end
end
attribute vb_name = "form1"
attribute vb_globalnamespace = false
attribute vb_creatable = false
attribute vb_predeclaredid = true
attribute vb_exposed = false
option explicit
dim returnheight as long, returnwidth as long
private sub command1_click()
dim bigwidth as long, bigheight as long
dim stretchwidth as long, stretchheight as long
 commondialog1.filter = "jpeg文件|*.jpg|gif文件|*.gif|所有文件|*.*"
 commondialog1.showopen
 if commondialog1.filename <> "" then
 picture1.picture = loadpicture(commondialog1.filename)
 
 bigwidth = picture1.width
 bigheight = picture1.height
 stretchwidth = picture2.scalewidth
 stretchheight = picture2.scaleheight
 
 stretchimage bigwidth, bigheight, stretchwidth, stretchheight, true
 
 image1.stretch = true
 image1.width = returnwidth
 image1.height = returnheight
 
 image1.left = (picture2.scalewidth - image1.width) / 2
 image1.top = (picture2.scaleheight - image1.height) / 2
 image1.picture = loadpicture(commondialog1.filename)
 end if
end sub
private sub stretchimage(originalwidth as long, originalheight as long, stretchwidth as long, stretchheight as long, optional flag as boolean = false)
 if (originalwidth >= stretchwidth or originalheight > stretchheight) or flag = true then '需要縮放
 if originalwidth / originalheight >= stretchwidth / stretchheight then
 returnwidth = stretchwidth
 returnheight = stretchwidth / originalwidth * originalheight
 else
 returnheight = stretchheight
 returnwidth = stretchheight / originalheight * originalwidth
 end if
 else
 returnheight = originalheight
 returnwidth = originalwidth
 end if
end sub