搜索後,我發現這個代碼:適應asp.net代碼來調整圖像
Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
Dim image__1 = System.Drawing.Image.FromStream(fromStream)
Dim newWidth = CInt(image__1.Width * scaleFactor)
Dim newHeight = CInt(image__1.Height * scaleFactor)
Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)
Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
thumbnailGraph.DrawImage(image__1, imageRectangle)
thumbnailBitmap.Save(toStream, image__1.RawFormat)
thumbnailGraph.Dispose()
thumbnailBitmap.Dispose()
image__1.Dispose()
End Sub
有兩件事情我不能「修改」,以解決我的問題:
- 我不想通過一個流,但我更喜歡通過像
C:\mysite\photo\myphoto.gif
這樣的路徑。我如何「轉換」它接受文件而不是流? - 在這個函數中我必須傳遞一個「scale」值。但我更願意檢查圖像是否太大(例如>
1024x768
),而不是將其調整爲最大爲1024x768
。我如何使用System.Drawing
進行檢查。
正如你所看到的,我對System.Drawing一無所知,所以我需要一個「硬」的幫助來解決這個工作。
ImageBuilder.Current.Build(sourcePathOrStream,destPathOrStream,new ResizeSettings(「maxwidth = 1024&maxheight = 768」))'//這是使用免費的http://imageresizing.net庫的一行代碼。並且可以避免[圖像調整大小陷阱](http://nathanaeljones.com/163/20-image-resizing-pitfalls/)。 – 2011-06-21 23:59:58