En del af det: Dim WidthVsHeightRatio As Double = CType(originalBitmap.Width, Single) / CType(originalBitmap.Height, Single) ' Using Dim newg As Graphics = Graphics.FromImage(newbmp) Try newg.Clear(Color.White) If WidthVsHeightRatio = 1 Then newg.DrawImage(originalBitmap, 0, 0, 100, 100) newg.Save Else If WidthVsHeightRatio < 1 Then newg.DrawImage(originalBitmap, New RectangleF(New PointF(CType((50 - ((100 * WidthVsHeightRatio) / 2)), Single), 0), New SizeF(CType((100 * WidthVsHeightRatio), Single), 100F))) newg.Save Else Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1) newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, CType((50 - ((100 * inverse) / 2)), Single)), New SizeF(100F, CType((100 * inverse), Single)))) newg.Save newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality End If End If Finally CType(newg, IDisposable).Dispose() End Try
Dim FileToResize As String = Server.MapPath("~/images/1.jpg") Dim originalBitmap As Bitmap = CType(ConversionHelpers.AsWorkaround(Bitmap.FromFile(FileToResize, True), GetType(Bitmap)), Bitmap) Dim newbmp As Bitmap = New Bitmap(100, 100) Dim WidthVsHeightRatio As Double = CType(originalBitmap.Width, Single) / CType(originalBitmap.Height, Single) ' Using Dim newg As Graphics = Graphics.FromImage(newbmp) Try newg.Clear(Color.White) If WidthVsHeightRatio = 1 Then newg.DrawImage(originalBitmap, 0, 0, 100, 100) newg.Save Else If WidthVsHeightRatio < 1 Then newg.DrawImage(originalBitmap, New RectangleF(New PointF(CType((50 - ((100 * WidthVsHeightRatio) / 2)), Single), 0), New SizeF(CType((100 * WidthVsHeightRatio), Single), 100F))) newg.Save Else Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1) newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, CType((50 - ((100 * inverse) / 2)), Single)), New SizeF(100F, CType((100 * inverse), Single)))) newg.Save newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality End If End If Finally CType(newg, IDisposable).Dispose() End Try newbmp.Save(Server.MapPath("~/images/1_resized.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.