locked
FAQs: 如何按同比例调整图片大小? RRS feed

答案

  • 主要思路: 用一个新图片尺寸创建一个新的Bitmap,然后将原始的Bitmap画在新的Bitmap对象上面。

    先决条件: 拖动Button1, PictureBox1 PictureBox2 控件到 Form1窗体上面。

     

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            PictureBox1.ImageLocation = "D:\mm.jpg"

            PictureBox2.ImageLocation = "D:\mmm.jpg"

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            resizeImage(PictureBox1.ImageLocation, PictureBox1)

            resizeImage(PictureBox2.ImageLocation, PictureBox2)

        End Sub

     

        Public Sub resizeImage(ByVal imgPath As String, ByVal picbox As PictureBox)

            ' 获取源bitmap

            Dim bm_source As Bitmap = New Bitmap(imgPath)

            ' 记录源bitmap的比例  

            Dim scale As Double = bm_source.Height / bm_source.Width

            ' 创建一个新bitmap

            Dim bm_dest As New Bitmap(CInt(picbox.Width), CInt(picbox.Width * scale))

            ' 为新的bitmap创建一个新的图形对象Make  

            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            ' 将源bitmap复制到目标bitmap  

            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

            ' PictureBox 控件上面显示新的bitmap

            picbox.Image = bm_dest

        End Sub

    End Class

     

    相关帖子:

    http://social.msdn.microsoft.com/forums/en-US/vbide/thread/69bae6d7-eb4e-49cd-b078-704a028e9318


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月2日 12:22