Imports System.Drawing.Drawing2D
Imports System.IO
Module com
Public Function ResizeImage(ByVal currentImage As Image, ByVal newSize As Size, _
Optional ByVal MaintainAspect As Boolean = True) As Image
If MaintainAspect = True Then
newSize = CalculateImageSize(currentImage.Size, newSize)
End If
Dim TargetBitmap As New Bitmap(currentImage, newSize)
Dim TargetGraphic As Graphics = Graphics.FromImage(TargetBitmap)
TargetGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic
TargetGraphic.SmoothingMode = SmoothingMode.HighQuality
TargetGraphic.DrawImage(currentImage, New Rectangle(0, 0, newSize.Width, newSize.Height) _
, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel)
TargetGraphic.Dispose()
TargetGraphic = Nothing
Return TargetBitmap
End Function
Public Function CalculateImageSize(ByVal ImageSize As Size, ByVal newsize As Size) As Size
Dim mySize As Size
Dim rNew As Double
Dim rCur As Double
rNew = newsize.Width / newsize.Height
rCur = ImageSize.Width / ImageSize.Height
If rCur > rNew Then
mySize.Width = newsize.Width
mySize.Height = CInt(mySize.Width / rCur)
End If
If rCur < rNew Then
mySize.Height = newsize.Height
mySize.Width = CInt(mySize.Height * rCur)
End If
If rCur = rNew Then
mySize.Width = newsize.Width
mySize.Height = newsize.Height
End If
Return mySize
End Function
Public Function CaricaImmagine(ByVal pFile As String, ByVal pMetodo As Boolean) As System.Drawing.Image
Dim retImage As Image = Nothing
If pMetodo Then
Dim img As Image = New Bitmap(pFile)
retImage = New Bitmap(img)
img.Dispose()
Else
Dim imgFile As FileStream = New FileStream(pFile, FileMode.Open)
Dim imgReader As BinaryReader = New BinaryReader(imgFile)
Dim imgStream As MemoryStream = New MemoryStream(imgReader.ReadBytes(CInt(imgFile.Length)))
imgReader.Close()
retImage = Image.FromStream(imgStream)
End If
Return retImage
End Function
Public Function CutImage(ByVal Original As Image, ByVal CutArea As RectangleF) As Image
Dim Result As New Bitmap(CInt(CutArea.Width), CInt(CutArea.Height))
Dim G As Graphics = Graphics.FromImage(Result)
G.DrawImage(Original, Original.GetBounds(GraphicsUnit.Pixel), CutArea, GraphicsUnit.Pixel)
G.Dispose()
Return Result
End Function
End Module