Public Shared Function ImageResize(ByVal source As String, _
ByVal destination As String, _
Optional ByVal newWidth As Integer = 0, _
Optional ByVal newHeight As Integer = 0, _
Optional ByVal NewIsMax As Boolean = False, _
Optional ByVal Smoothing As Drawing2D.SmoothingMode = SmoothingMode.HighQuality, _
Optional ByVal Compositing As Drawing2D.CompositingQuality = CompositingQuality.HighQuality, _
Optional ByVal Interpolation As Drawing2D.InterpolationMode = InterpolationMode.High) As Boolean
Try
Dim fullSizeImage As Image
fullSizeImage = Image.FromFile(source)
Dim w As Integer = fullSizeImage.Width
Dim h As Integer = fullSizeImage.Height
If newWidth <= 0 And newHeight <= 0 Then
newWidth = w
newHeight = h
End If
If Not NewIsMax Then 'verilen degerlere göre direk dönüstürür
If newWidth > 0 And newHeight <= 0 Then newHeight = h * (newWidth / w)
If newWidth <= 0 And newHeight > 0 Then newWidth = w * (newHeight / h)
Else ' max deger kabul eder
If (w / h) > 1 Then 'yatay resim
'newHeight = h * (newWidth / w)
If (h * (newWidth / w)) <= newHeight Then 'height de tasma yoksa
newHeight = h * (newWidth / w) 'yükseklik ayarla
Else
newWidth = w * (newHeight / h) 'tasma varsa yükseklige dokunmadan genisligi küçült
End If
Else 'dikey resim
'newWidth = w * (newHeight / h)
If (w * (newHeight / h)) <= newWidth Then 'genslikte tasma yoksa
newWidth = w * (newHeight / h) 'genisligi ayarla
Else
newHeight = h * (newWidth / w) 'tasma varsa genislige dokunmadan yüksekligi ayarla
End If
End If
End If
Dim imgTmp As Image
Dim imgFoto As Bitmap
imgTmp = Image.FromFile(source)
imgFoto = New Bitmap(CInt(newWidth), CInt(newHeight))
Dim recDest As New Rectangle(0, 0, newWidth, imgFoto.Height)
Dim gphCrop As Graphics = Graphics.FromImage(imgFoto)
gphCrop.SmoothingMode = Smoothing
gphCrop.CompositingQuality = Compositing
gphCrop.InterpolationMode = Interpolation
gphCrop.DrawImage(imgTmp, recDest, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel)
Dim myEncoder As Imaging.Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
Dim arrayICI() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()
Dim jpegICI As Imaging.ImageCodecInfo = Nothing
Dim x As Integer = 0
For x = 0 To arrayICI.Length - 1
If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
jpegICI = arrayICI(x)
Exit For
End If
Next
myEncoder = Imaging.Encoder.Quality
myEncoderParameters = New Imaging.EncoderParameters(1)
myEncoderParameter = New Imaging.EncoderParameter(myEncoder, 80L)
myEncoderParameters.Param(0) = myEncoderParameter
imgFoto.Save(destination, jpegICI, myEncoderParameters)
imgFoto = Nothing
imgTmp = Nothing
imgFoto.Dispose()
imgTmp.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
|