Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As LongPrivate Sub Command1_Click() '利用picture2作为缓冲 Picture2.Picture = LoadPicture("F:\pictures\sniper\awp_a.jpg") StretchBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, vbSrcCopy Picture1.Refresh End SubPrivate Sub Form_Load() Picture1.AutoRedraw = True Picture2.AutoRedraw = True Picture2.AutoSize = True Picture2.Visible = False End Sub
你也可以放一个image控件在picturbox里,控制他们的大小就可以了
用PaintPicture就够了~Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.PaintPicture LoadPicture("0.gif"), 0, 0, X, YEnd Sub
'*************************************************** ' 将图片在PictureBox控件中按比例缩放后,居中显示 '*************************************************** Public Sub PictureToCenter(tPic As Picture, PicBox As PictureBox) Dim PicH As Long, PicW As Long Dim PicBoxW As Long, PicBoxH As Long Dim PicRate As Single, PicBoxRate As Single Dim NewH As Long, NewW As Long
PicBox.Cls If PicH <= PicBoxH And PicW <= PicBoxW Then PicBox.PaintPicture tPic, (PicBoxW - PicW) / 2, (PicBoxH - PicH) / 2 Exit Sub End If If PicBoxRate < PicRate Then NewH = PicBoxW / PicRate PicBox.PaintPicture tPic, 0, (PicBoxH - NewH) / 2, PicBoxW, NewH Else NewW = PicBoxH * PicRate PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0, NewW, PicBoxH End If End Sub'这个过程是我在工程中使用的,可以适应任何比例的图片 Private Sub Picture1_Click() Dim p As StdPicture
Set p = LoadPicture("H:\My Documents\Img15644950.jpg") PictureToCenter p, Picture1 End Sub
上樓的: Dim p As StdPicture
Set p = LoadPicture("H:\My Documents\Img15644950.jpg") 好象不行吧.有沒測試過?
'利用picture2作为缓冲
Picture2.Picture = LoadPicture("F:\pictures\sniper\awp_a.jpg")
StretchBlt Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture2.hdc, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, vbSrcCopy
Picture1.Refresh
End SubPrivate Sub Form_Load()
Picture1.AutoRedraw = True
Picture2.AutoRedraw = True
Picture2.AutoSize = True
Picture2.Visible = False
End Sub
Me.PaintPicture LoadPicture("0.gif"), 0, 0, X, YEnd Sub
1、完全用VB,如 AprilSong(X)介绍的PaintPicture法,
2、完全用API,不建议VB与API混用,问题倒没什么,只是感觉:要速度没速度,要节源也没节源,两头都靠不上,没必要如此。虽然,VB自带的示例中也是混用的,但我还是坚持这个观点。
laviewpbt(人一定要靠自己)说的StretchBlt法,用Picture2做缓冲,就是VB与API混用绘图,为了一个图片就加上个PictureBox,的确浪费,资源不能释放不说,而且PictureBox是耗源大户,还是越少越好。这样用API,还不如直接用PaintPicture。
Dim i As Integer, j As Integer
With Image1
.Stretch = False
.Visible = False
.Picture = LoadPicture("\\SWEB\datafile\photo\employee\24115.jpg")
ZX = .Width / 3000 '假設目標寬度155圖元
ZY = .Height / 3500 '假設目標高度165圖元 .Stretch = True
.Height = Int(.Height / ZY)
.Width = Int(.Width / ZX)
End With
' 将图片在PictureBox控件中按比例缩放后,居中显示
'***************************************************
Public Sub PictureToCenter(tPic As Picture, PicBox As PictureBox)
Dim PicH As Long, PicW As Long
Dim PicBoxW As Long, PicBoxH As Long
Dim PicRate As Single, PicBoxRate As Single
Dim NewH As Long, NewW As Long
PicBoxH = PicBox.ScaleHeight
PicBoxW = PicBox.ScaleWidth
PicBoxRate = PicBoxW / PicBoxH
PicW = ScaleX(tPic.Width, vbHimetric, PicBox.ScaleMode)
PicH = ScaleY(tPic.Height, vbHimetric, PicBox.ScaleMode)
PicRate = PicW / PicH
PicBox.Cls
If PicH <= PicBoxH And PicW <= PicBoxW Then
PicBox.PaintPicture tPic, (PicBoxW - PicW) / 2, (PicBoxH - PicH) / 2
Exit Sub
End If
If PicBoxRate < PicRate Then
NewH = PicBoxW / PicRate
PicBox.PaintPicture tPic, 0, (PicBoxH - NewH) / 2, PicBoxW, NewH
Else
NewW = PicBoxH * PicRate
PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0, NewW, PicBoxH
End If
End Sub'这个过程是我在工程中使用的,可以适应任何比例的图片
Private Sub Picture1_Click()
Dim p As StdPicture
Set p = LoadPicture("H:\My Documents\Img15644950.jpg")
PictureToCenter p, Picture1
End Sub
Dim p As StdPicture
Set p = LoadPicture("H:\My Documents\Img15644950.jpg")
好象不行吧.有沒測試過?