vb如何让图片自动缩放成图片框大小,图片框设定成固定大小,而图片要完整的显示在图片框里,自动缩放
展开全部
图片框设定成固定大小,而图片要完整的显示,
这就有一个问题了,载入的图片长宽比不是固定的,如果自动缩放为图片框大小,那么图片肯定会失真,比如把长幅图片里的瘦子变成了宽幅里的胖子,把宽幅里的胖子变成了长幅里的瘦子,那还能叫完整吗?,所以载入的图片一定还要保持原来的长宽比,对不对?
试试这个,要两个PictureBOX,一个COMMANDBUTTON,一个CommonDialog,Picture1的长宽要相同
Private Sub Command1_Click()
Dim pic_widht As Long
Dim pic_height As Long
CommonDialog1.Filter = "(图片文件)*.BMP;*.JPG;*.JPEG|*.BMP;*.JPG;*.JPEG"'用来选择图片
CommonDialog1.FilterIndex = 0
CommonDialog1.Action = 1
If CommonDialog1.FileName <> "" Then
Picture2.Picture = LoadPicture(CommonDialog1.FileName)
If Picture2.ScaleWidth > Picture2.ScaleHeight Then
pic_widht = Picture1.ScaleWidth
pic_height = Picture1.ScaleHeight * Picture2.ScaleHeight / Picture2.ScaleWidth
Else
pic_widht = Picture1.ScaleWidth * Picture2.ScaleWidth / Picture2.ScaleHeight
pic_height = Picture1.ScaleHeight
End If
Picture1.Cls
Picture1.PaintPicture Picture2, (Picture1.ScaleWidth - pic_widht) / 2, (Picture1.ScaleHeight - pic_height) / 2, pic_widht, pic_height, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight
Else
End If
End Sub
Private Sub Form_Load()
Picture1.AutoRedraw = True'用来显示图片可见
Picture1.AutoSize = False
Picture1.Visible = True
Picture1.BackColor = RGB(255, 0, 0)
Picture2.AutoSize = True'用来装载图片,不可见
Picture2.Visible = False
Command1.Caption = "装入图片"
End Sub
这就有一个问题了,载入的图片长宽比不是固定的,如果自动缩放为图片框大小,那么图片肯定会失真,比如把长幅图片里的瘦子变成了宽幅里的胖子,把宽幅里的胖子变成了长幅里的瘦子,那还能叫完整吗?,所以载入的图片一定还要保持原来的长宽比,对不对?
试试这个,要两个PictureBOX,一个COMMANDBUTTON,一个CommonDialog,Picture1的长宽要相同
Private Sub Command1_Click()
Dim pic_widht As Long
Dim pic_height As Long
CommonDialog1.Filter = "(图片文件)*.BMP;*.JPG;*.JPEG|*.BMP;*.JPG;*.JPEG"'用来选择图片
CommonDialog1.FilterIndex = 0
CommonDialog1.Action = 1
If CommonDialog1.FileName <> "" Then
Picture2.Picture = LoadPicture(CommonDialog1.FileName)
If Picture2.ScaleWidth > Picture2.ScaleHeight Then
pic_widht = Picture1.ScaleWidth
pic_height = Picture1.ScaleHeight * Picture2.ScaleHeight / Picture2.ScaleWidth
Else
pic_widht = Picture1.ScaleWidth * Picture2.ScaleWidth / Picture2.ScaleHeight
pic_height = Picture1.ScaleHeight
End If
Picture1.Cls
Picture1.PaintPicture Picture2, (Picture1.ScaleWidth - pic_widht) / 2, (Picture1.ScaleHeight - pic_height) / 2, pic_widht, pic_height, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight
Else
End If
End Sub
Private Sub Form_Load()
Picture1.AutoRedraw = True'用来显示图片可见
Picture1.AutoSize = False
Picture1.Visible = True
Picture1.BackColor = RGB(255, 0, 0)
Picture2.AutoSize = True'用来装载图片,不可见
Picture2.Visible = False
Command1.Caption = "装入图片"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询