在VB.NET中调整图像大小

时间:2011-12-08 12:35:38

标签: asp.net vb.net image ihttphandler

我的IHttpHandler中有以下代码:

            Dim MemoryStream1 As New System.IO.MemoryStream

            MemoryStream1.Write(SqlDataReader1("cover"), 0, SqlDataReader1("cover").Length - 1)

            Dim Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)

            Dim Width1 As Integer = Bitmap1.Width
            Dim Height1 As Integer = Bitmap1.Height

            Dim Width2 As Integer = 90
            Dim Height2 As Integer = Height1 * Width1 / Width1

            Dim Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)

            Dim Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)

            Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)

            Dim MemoryStream2 As New System.IO.MemoryStream

            Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)

            context.Response.BinaryWrite(MemoryStream2.ToArray)

它有效,但我不确定这是调整图像大小的正确方法。如何简化代码?

提前致谢!

2 个答案:

答案 0 :(得分:3)

Public Function ResizeImage(imgToResize As Image, size As Size) As Byte()
        Dim sourceWidth As Integer = imgToResize.Width
        Dim sourceHeight As Integer = imgToResize.Height

        Dim nPercent As Single = 0
        Dim nPercentW As Single = 0
        Dim nPercentH As Single = 0

        nPercentW = (CSng(size.Width) / CSng(sourceWidth))
        nPercentH = (CSng(size.Height) / CSng(sourceHeight))

        If nPercentH < nPercentW Then
            nPercent = nPercentH
        Else
            nPercent = nPercentW
        End If

        Dim destWidth As Integer = CInt(Math.Truncate(sourceWidth * nPercent))
        Dim destHeight As Integer = CInt(Math.Truncate(sourceHeight * nPercent))

        Dim b As New Bitmap(destWidth, destHeight)
        Dim g As Graphics = Graphics.FromImage(DirectCast(b, Image))
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight)
        g.Dispose()

        Return b.ToByteArray()
    End Function

此功能会将图像调整为指定大小,以保持其位置。它是在C#中,我把它通过在线转换器,所以可能不是100%正确

ToByteArray()是我写的一个扩展方法,用于将图像存储在数据库中,如果您愿意,我也可以为您提供。

答案 1 :(得分:2)

基本上代码是正确的,但是它有一些问题:

  • 当您写入第一个内存流时,您正在跳过最后一个字节。 Write调用中的最后一个属性应该是长度,而不是长度减一。
  • 您对Height2的计算不正确。表达式Height1 * Width1 / Width1将始终评估为Height1的值。您应该使用Height1 * Width2 / Width1代替。
  • 您没有处理内存流,位图或图形对象。使用Using块来确保处置对象。

您可以通过从字节数组创建第一个内存流而不是将数组写入流来简化代码:

Using MemoryStream1 As New System.IO.MemoryStream(SqlDataReader1("cover"))

  Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)

    Dim Width1 As Integer = Bitmap1.Width
    Dim Height1 As Integer = Bitmap1.Height

    Dim Width2 As Integer = 90
    Dim Height2 As Integer = Height1 * Width2 / Width1

    Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)

      Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)

        Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)

      End Using

      Using MemoryStream2 As New System.IO.MemoryStream

        Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)

        context.Response.BinaryWrite(MemoryStream2.ToArray)

      End Using

    End Using

  End Using

End Using