我想將影像調整為最大寬度和高度,然后裁剪它。調整大小有效。但是我在這里找到的裁剪代碼沒有。
我自己現有的代碼使用位元組陣列,所以我需要使用它。
所以下面的代碼會調整大小,不會裁剪,并且會保存新影像。我在裁剪時做錯了什么?
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
uj5u.com熱心網友回復:
使用太相似的變數名會導致您輸入錯誤。正如所寫,您有以下代碼塊:
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。
uj5u.com熱心網友回復:
我沒有測驗過,但我認為你應該在這行代碼中使用不同的變數。
g.DrawImage(bmp, New Rectangle(0, 0, bm.Width, bm.Height),
CropArea,
GraphicsUnit.Pixel)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402857.html
標籤:
上一篇:wcfjsonpajaxjquerycallback-如何自定義回應、回呼函式的名稱和包裝器?
下一篇:提取相關的XML組件
