ASP.NET裁剪图片不裁剪

ASP.NET crop image does not crop it

我想将图像调整到最大宽度和高度,然后进行裁剪。 调整大小有效。但是裁剪 code I found here 没有。

我自己现有的代码使用字节数组,所以我需要使用它。

所以下面的代码会调整大小,不裁剪,并保存新图像。我在裁剪方面做错了什么?

Dim imgNewWidth, imgNewHeight As Integer

Dim _originalThumbWidth As Integer = 900
Dim _originalThumbHeight As Integer = 900

Dim imageURL As String = "https://med.stanford.edu/news/all-news/2021/09/cat-fur-color-patterns/_jcr_content/main/image.img.780.high.jpg/cat_by-Kateryna-T-Unsplash.jpg"
Dim localImagePath As String = Server.MapPath("images\_tmp\") + "CROPPED.jpg"

ResizeAndSaveFast(_originalThumbWidth, _originalThumbHeight, imageURL, localImagePath, "", "", imgNewWidth, imgNewHeight)



  Private Function ResizeAndSaveFast(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal imageURL As String, ByVal saveToPath As String, ByVal userName As String, ByVal password As String,
                                    ByRef imgNewWidth As Integer, ByRef imgNewHeight As Integer) As Boolean

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

        Dim imgRequest As WebRequest = WebRequest.Create(imageURL)
        Dim imgResponse As WebResponse
        Dim memStream As New MemoryStream

        Try
            imgResponse = imgRequest.GetResponse()
            Dim streamPhoto As Stream = imgResponse.GetResponseStream()
            streamPhoto.CopyTo(memStream)
            memStream.Position = 0
        Catch ex As Exception
            Return False
        End Try


        Dim bfPhoto As BitmapFrame = ReadBitmapFrame(memStream)
        Dim newWidth, newHeight As Integer
        Dim scaleFactor As Double

        newWidth = bfPhoto.PixelWidth
        newHeight = bfPhoto.PixelHeight

        imgNewWidth = newWidth
        imgNewHeight = newHeight

        If bfPhoto.PixelWidth > maxWidth Or bfPhoto.PixelHeight > maxHeight Then
            If bfPhoto.PixelWidth > maxWidth Then
                scaleFactor = maxWidth / bfPhoto.PixelWidth
                newWidth = CInt(Math.Round(bfPhoto.PixelWidth * scaleFactor, 0))
                newHeight = CInt(Math.Round(bfPhoto.PixelHeight * scaleFactor, 0))
            End If
            If newHeight > maxHeight Then
                scaleFactor = maxHeight / newHeight
                newWidth = CInt(Math.Round(newWidth * scaleFactor, 0))
                newHeight = CInt(Math.Round(newHeight * scaleFactor, 0))
            End If
        End If

        imgNewWidth = newWidth
        imgNewHeight = newHeight

        Dim bfResize As BitmapFrame = FastResize(bfPhoto, newWidth, newHeight)

        Dim baResize As Byte() = ToByteArray(bfResize)

        Dim bmp As System.Drawing.Bitmap = imageFunctions.ConvertByteArrayToBitmap(baResize)

        Dim CropArea As Rectangle = New Rectangle(100, 100, bmp.Width - 100, bmp.Height - 100)
        Dim bm As Bitmap = New Bitmap(CropArea.Width, CropArea.Height)


        Using g As Graphics = Graphics.FromImage(bm)
            g.DrawImage(bm, New Rectangle(0, 0, bm.Width, bm.Height),
                             CropArea,
                             GraphicsUnit.Pixel)
        End Using


        bm.Save(saveToPath, ImageFormat.Jpeg)

        Return True


    End Function

我没有测试过,但我认为你应该在这行代码中使用不同的变量。

g.DrawImage(bmp, New Rectangle(0, 0, bm.Width, bm.Height),
                         CropArea,
                         GraphicsUnit.Pixel)

您的拼写错误是由于使用了过于相似的变量名。正如所写,您有这段代码:

Dim bmp As System.Drawing.Bitmap = imageFunctions.ConvertByteArrayToBitmap(baResize)

Dim CropArea As Rectangle = New Rectangle(100, 100, bmp.Width - 100, bmp.Height - 100)
Dim bm As Bitmap = New Bitmap(CropArea.Width, CropArea.Height)


Using g As Graphics = Graphics.FromImage(bm)
            g.DrawImage(bm, New Rectangle(0, 0, bm.Width, bm.Height),
                             CropArea,
                             GraphicsUnit.Pixel)
End Using

这一行:

g.DrawImage(bm, 
            New Rectangle(0, 0, bm.Width, bm.Height),
            CropArea,
            GraphicsUnit.Pixel)

应该是:

g.DrawImage(bmp, 
            New Rectangle(0, 0, bm.Width, bm.Height),
            CropArea,
            GraphicsUnit.Pixel)

在您的代码中,您创建了变量 bmp 来存储未裁剪的位图。换句话说,你的源数据。

然后您声明 bm 以存储裁剪后的位图,即您的目的地。 Graphics 对象 g 是使用目标创建的。

在您对 g.DrawImage 的调用中,第一个参数应该是源数据 bmp。相反,你有目的地,bm 那里。