Agregar archivos de proyecto.
This commit is contained in:
51
Imagen.vb
Normal file
51
Imagen.vb
Normal file
@@ -0,0 +1,51 @@
|
||||
Imports System.Drawing
|
||||
Imports System.Drawing.Drawing2D
|
||||
Imports System.Drawing.Imaging
|
||||
Imports System.IO
|
||||
|
||||
|
||||
|
||||
Public Class Imagen
|
||||
Public Shared Function ResizeImage(ByVal image As Image,
|
||||
ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
|
||||
Dim newWidth As Integer
|
||||
Dim newHeight As Integer
|
||||
If preserveAspectRatio Then
|
||||
Dim originalWidth As Integer = image.Width
|
||||
Dim originalHeight As Integer = image.Height
|
||||
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
|
||||
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
|
||||
Dim percent As Single = If(percentHeight < percentWidth,
|
||||
percentHeight, percentWidth)
|
||||
newWidth = CInt(originalWidth * percent)
|
||||
newHeight = CInt(originalHeight * percent)
|
||||
Else
|
||||
newWidth = size.Width
|
||||
newHeight = size.Height
|
||||
End If
|
||||
Dim newImage As Image = New Bitmap(newWidth, newHeight)
|
||||
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
|
||||
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
|
||||
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
|
||||
End Using
|
||||
Return newImage
|
||||
End Function
|
||||
Public Shared Function ImagenAByteArray(Imagen As Image, ByVal format As ImageFormat, Optional Calidad As Long = 100) As Byte()
|
||||
Dim jpgEncoder As ImageCodecInfo = ImageCodecInfo.GetImageEncoders().First(Function(x) x.FormatID = format.Guid)
|
||||
|
||||
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
|
||||
Dim myEncoderParameters As EncoderParameters = New EncoderParameters(1)
|
||||
Dim myEncoderParameter As EncoderParameter = New EncoderParameter(myEncoder, Calidad)
|
||||
'myEncoderParameters.Param.Append(myEncoderParameter)
|
||||
myEncoderParameters.Param(0) = myEncoderParameter
|
||||
|
||||
|
||||
'Dim converter As New ImageConverter
|
||||
'Dim bytes As Byte() = converter.ConvertTo(Imagen, GetType(Byte()))
|
||||
Dim ms As New MemoryStream
|
||||
Imagen.Save(ms, jpgEncoder, myEncoderParameters)
|
||||
Return ms.ToArray
|
||||
End Function
|
||||
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user