Save Resized Image Class


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Public Class SaveResizedImage
  2.  
  3. ' SaveResizedImage
  4. Public Sub SaveResizedImage(ByVal objGraphic As System.Drawing.Image, ByVal strPath As String, ByVal intWidth As Integer)
  5.  
  6. Dim objFormat As System.Drawing.Imaging.ImageFormat = objGraphic.RawFormat
  7.  
  8. Dim objThumbSize As New System.Drawing.Size
  9. objThumbSize = GetSize(objGraphic.Width, objGraphic.Height, intWidth)
  10.  
  11. Dim objBmp As System.Drawing.Bitmap = CreateThumbnail(objGraphic, objThumbSize.Width, objThumbSize.Height)
  12.  
  13. If objFormat Is System.Drawing.Imaging.ImageFormat.Gif Then
  14. objBmp.Save(strPath, System.Drawing.Imaging.ImageFormat.Gif)
  15. Else
  16. objBmp.Save(strPath, System.Drawing.Imaging.ImageFormat.Jpeg)
  17. End If
  18.  
  19. objBmp.Dispose()
  20.  
  21. End Sub ' SaveResizedImage
  22.  
  23. ' GetSize
  24. Private Function GetSize(ByVal dblWidth As Double, ByVal dblHeight As Double, ByVal intWidth As Integer) As System.Drawing.Size
  25.  
  26. Dim dblNewWidth As Double
  27. Dim dblNewHeight As Double
  28.  
  29. dblNewWidth = intWidth
  30. ' Set new height, preserve original width/height ratio
  31. dblNewHeight = dblHeight * dblNewWidth / dblWidth
  32.  
  33. Dim NewSize As New System.Drawing.Size(CInt(dblNewWidth), CInt(dblNewHeight))
  34.  
  35. Return NewSize
  36.  
  37. End Function ' GetSize
  38.  
  39. ' CreateThumbnail
  40. Private Function CreateThumbnail(ByVal objGraphic As System.Drawing.Image, ByVal lnWidth As Integer, ByVal lnHeight As Integer) As System.Drawing.Bitmap
  41.  
  42. Dim bmpOut As System.Drawing.Bitmap = Nothing
  43.  
  44. Try
  45. Dim loBMP As New System.Drawing.Bitmap(objGraphic)
  46. Dim loFormat As System.Drawing.Imaging.ImageFormat = loBMP.RawFormat
  47. Dim lnRatio As Decimal
  48. Dim lnNewWidth As Integer = 0
  49. Dim lnNewHeight As Integer = 0
  50.  
  51. If loBMP.Width < lnWidth AndAlso loBMP.Height < lnHeight Then
  52. Return loBMP
  53. End If
  54.  
  55. If loBMP.Width > loBMP.Height Then
  56. lnRatio = CType(lnWidth, Decimal) / loBMP.Width
  57. lnNewWidth = lnWidth
  58. Dim lnTemp As Decimal = loBMP.Height * lnRatio
  59. lnNewHeight = CType(lnTemp, Integer)
  60. Else
  61. lnRatio = CType(lnHeight, Decimal) / loBMP.Height
  62. lnNewHeight = lnHeight
  63. Dim lnTemp As Decimal = loBMP.Width * lnRatio
  64. lnNewWidth = CType(lnTemp, Integer)
  65. End If
  66.  
  67. bmpOut = New System.Drawing.Bitmap(lnNewWidth, lnNewHeight)
  68. Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpOut)
  69. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
  70. g.FillRectangle(System.Drawing.Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
  71. g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight)
  72. loBMP.Dispose()
  73.  
  74. Catch
  75. Return Nothing
  76. End Try
  77.  
  78. Return bmpOut
  79.  
  80. End Function ' CreateThumbnail
  81.  
  82. End Class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.