在VB.NET中调整图片大小

14

我希望制作一个简单的VB实用程序,使用vb.net调整图像大小。 我无法确定要使用哪个VB类来实际操作图像。 Image类和Bitmap类都不起作用。

如果有任何想法、提示、技巧、教程链接,将不胜感激。

谢谢。

6个回答

30
您可以在Visual Basic .Net中使用这一行代码来调整图像大小。
Public Shared Function ResizeImage(ByVal InputImage As Image) As Image
        Return New Bitmap(InputImage, New Size(64, 64))
End Function

在哪里;

  1. "InputImage"是您要调整大小的图像。
  2. "64 X 64"是所需的大小,您可以根据需要更改其大小,例如32X32等。

简单快捷。在宽高比方面可能会出现一些副作用,但可以处理。 - Mohamed Nagieb
4
@MohamedNagieb 你可以使用这个公式计算宽高比:(原始高度 / 原始宽度) x 新宽度 = 新高度 - Muhammad Saqib

17

这篇文章详细介绍了如何进行此操作。

Private Sub btnScale_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnScale.Click
    ' Get the scale factor.
    Dim scale_factor As Single = Single.Parse(txtScale.Text)

    ' Get the source bitmap.
    Dim bm_source As New Bitmap(picSource.Image)

    ' Make a bitmap for the result.
    Dim bm_dest As New Bitmap( _
        CInt(bm_source.Width * scale_factor), _
        CInt(bm_source.Height * scale_factor))

    ' Make a Graphics object for the result Bitmap.
    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

    ' Copy the source image into the destination bitmap.
    gr_dest.DrawImage(bm_source, 0, 0, _
        bm_dest.Width + 1, _
        bm_dest.Height + 1)

    ' Display the result.
    picDest.Image = bm_dest
End Sub

[编辑]
还有一个类似的


5
这将使用最佳质量调整任何图像,并支持32bpp的alpha通道。新图像将以原始纵横比在中心位置内嵌在新图像中。
#Region " ResizeImage "
    Public Overloads Shared Function ResizeImage(SourceImage As Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
        Dim bmSource = New Drawing.Bitmap(SourceImage)

        Return ResizeImage(bmSource, TargetWidth, TargetHeight)
    End Function

    Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
        Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight, Drawing.Imaging.PixelFormat.Format32bppArgb)

        Dim nSourceAspectRatio = bmSource.Width / bmSource.Height
        Dim nDestAspectRatio = bmDest.Width / bmDest.Height

        Dim NewX = 0
        Dim NewY = 0
        Dim NewWidth = bmDest.Width
        Dim NewHeight = bmDest.Height

        If nDestAspectRatio = nSourceAspectRatio Then
            'same ratio
        ElseIf nDestAspectRatio > nSourceAspectRatio Then
            'Source is taller
            NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio * NewHeight))
            NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth) / 2))
        Else
            'Source is wider
            NewHeight = Convert.ToInt32(Math.Floor((1 / nSourceAspectRatio) * NewWidth))
            NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight) / 2))
        End If

        Using grDest = Drawing.Graphics.FromImage(bmDest)
            With grDest
                .CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
                .InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                .PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
                .SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
                .CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver

                .DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight)
            End With
        End Using

        Return bmDest
    End Function
#End Region

Drawing2D.SmoothingMode doesn't apply here, it is only relevant for 2D vector drawing methods such as Graphics.DrawLine - alldayremix
请小心处理。With grDest 部分似乎略微增加了 alpha 值,只有在对带有半透明元素的图像进行迭代处理时才能注意到。随着时间的推移,这种不透明度越来越少。我注释掉了 .SmoothingMode 部分,并将 CompositingMode 更改为 SourceCopy。仍在测试中,但其中一个似乎已经解决了问题。恐怕我不能给出准确的答案,因为我不太了解 GDI。也许 @Carter 可以根据他对 GDI 的了解提供进一步帮助。 - stigzler

2

虽然我不太了解VB.NET语法,但是这里有一个想法

Dim source As New Bitmap("C:\image.png") 
Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb)

Using graphics As Graphics = Graphics.FromImage(target)
    graphics.DrawImage(source, new Size(48, 48)) 
End Using

1
这基本上就是Muhammad Saqib的答案,只有两个不同之处:
1: 增加了widthheight函数参数。
2: 这是一个可以忽略的小细节...使用'As Bitmap'而不是'As Image'。 'As Image'也能正常工作。我只是更喜欢匹配Return类型。请参见Image VS Bitmap Class.
Public Shared Function ResizeImage(ByVal InputBitmap As Bitmap, width As Integer, height As Integer) As Bitmap
    Return New Bitmap(InputImage, New Size(width, height))
End Function

Ex.
Dim someimage As New Bitmap("C:\somefile")
someimage = ResizeImage(someimage,800,600)

0
    Dim x As Integer = 0
    Dim y As Integer = 0
    Dim k = 0
    Dim l = 0
    Dim bm As New Bitmap(p1.Image)
    Dim om As New Bitmap(p1.Image.Width, p1.Image.Height)
    Dim r, g, b As Byte
    Do While x < bm.Width - 1
        y = 0
        l = 0
        Do While y < bm.Height - 1
            r = 255 - bm.GetPixel(x, y).R
            g = 255 - bm.GetPixel(x, y).G
            b = 255 - bm.GetPixel(x, y).B
            om.SetPixel(k, l, Color.FromArgb(r, g, b))
            y += 3
            l += 1
        Loop
        x += 3
        k += 1
    Loop
    p2.Image = om

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接