Här har du en färdig klass jag skrivit för länge sedan, dock i vb.net men du kan ju skriva om den. Det finns lite fler funktioner som rotation osv, samt hanterar fulhackar buggen jag skrev om tidigare genom att göra en dubbelrotation på 90 grader, då "förstörs" exif-datat och getThumbnail tar från orginalet. Anyway det var ett tag sedan denna skrevs. Men du har något att gå på iaf
Kod:
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Image
Imports System.IO
Imports System.Text
Namespace Image.Edit
* *Public Structure CropType
* * * *Public Height As String
* * * *Public Width As String
* *End Structure
* *Public Structure ResizeType
* * * *Public Height As String
* * * *Public Width As String
* *End Structure
* *Public Structure RotateType
* * * *Public Degrees As String
* *End Structure
* *Public Class ImageTransform
* * * *Public Crop As CropType
* * * *Public Resize As ResizeType
* * * *Public Rotate As RotateType
* *End Class
* *Public Class Edit
* * * *Public _pathOutputImage As String
* * * *Public imgTrans As ImageTransform
* * * *Public _inputImage As Drawing.Image
* * * *Public _strFileName As String
* * * *Private _inputImagePath As String
* * * *Public myBitmap As Bitmap
* * * *Public myImageCodecInfo As Imaging.ImageCodecInfo
* * * *Public myEncoder As Imaging.Encoder
* * * *Public myEncoderParameter As Imaging.EncoderParameter
* * * *Public myEncoderParameters As Imaging.EncoderParameters
* * * *Public Log As StringBuilder
* * * *ReadOnly Property Height() As String
* * * * * *Get
* * * * * * * *Return _inputImage.Height
* * * * * *End Get
* * * *End Property
* * * *ReadOnly Property GetLof() As String
* * * * * *Get
* * * * * * * *Return Log.ToString
* * * * * *End Get
* * * *End Property
* * * *ReadOnly Property Width() As String
* * * * * *Get
* * * * * * * *Return _inputImage.Width
* * * * * *End Get
* * * *End Property
* * * *Public Sub New(ByVal ImagePath As String)
* * * * * *Try
* * * * * * * *_inputImagePath = ImagePath
* * * * * * * *_inputImage = Drawing.Image.FromFile(_inputImagePath)
* * * * * * * *_pathOutputImage = _inputImagePath
* * * * * * * *Log = New StringBuilder()
* * * * * *Catch er As Exception
* * * * * * * *Throw er
* * * * * *End Try
* * * *End Sub
* * * *Public Sub FileName(ByVal strFileName As String)
* * * * * *_strFileName = strFileName
* * * *End Sub
* * * *Private Function getRandomName() As String
* * * * * *Dim fi As New FileInfo(_inputImagePath)
* * * * * *Return fi.DirectoryName & "\" & _strFileName
* * * *End Function
* * * *Public Function Transform(ByVal XMLFile As String)
* * * * * *Dim reader As New System.Xml.Serialization.XmlSerializer(GetType(ImageTransform))
* * * * * *Dim file As New System.IO.StreamReader(XMLFile)
* * * * * *imgTrans = reader.Deserialize(file)
* * * * * *file.Close()
* * * * * *'resize
* * * * * *If Not imgTrans.Resize.Height Is Nothing Then
* * * * * * * *Resize(imgTrans.Resize.Height.ToString, imgTrans.Resize.Width.ToString)
* * * * * *End If
* * * * * *'crop
* * * * * *If Not imgTrans.Crop.Height Is Nothing Then
* * * * * * * *Crop(imgTrans.Crop.Height.ToString, imgTrans.Crop.Width.ToString)
* * * * * *End If
* * * * * *'rotate
* * * * * *If Not imgTrans.Rotate.Degrees Is Nothing Then
* * * * * * * *Rotate(imgTrans.Rotate.Degrees)
* * * * * *End If
* * * *End Function
* * * *Public Function Crop(ByVal Height As String, ByVal Width As String)
* * * * * *_pathOutputImage = getRandomName()
* * * * * *Dim bmpImage As Bitmap
* * * * * *Dim recCrop As Rectangle
* * * * * *Dim bmpCrop As Bitmap
* * * * * *Dim gphCrop As Graphics
* * * * * *Dim recDest As Rectangle
* * * * * *Try
* * * * * * * *TakeNote("Crop " & Height & " x " & Width)
* * * * * * * *bmpImage = New Bitmap(_inputImage)
* * * * * * * *recCrop = New Rectangle(0, 0, Width, Height)
* * * * * * * *bmpCrop = New Bitmap(recCrop.Width, recCrop.Height, bmpImage.PixelFormat)
* * * * * * * *gphCrop = Graphics.FromImage(bmpCrop)
* * * * * * * *recDest = New Rectangle(0, 0, Width, Height)
* * * * * * * *gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
* * * * * * * * * *recCrop.Height, GraphicsUnit.Pixel)
* * * * * *Catch er As Exception
* * * * * * * *TakeNote("CROP ERR: " & er.ToString)
* * * * * * * *Throw er
* * * * * *Finally
* * * * * * * *bmpCrop.Save(_pathOutputImage)
* * * * * * * *_inputImage = Drawing.Image.FromFile(_pathOutputImage)
* * * * * *End Try
* * * *End Function
* * * *Public Function Rotate(ByVal Flip As String)
* * * * * *_pathOutputImage = getRandomName()
* * * * * *TakeNote("Rotating : " & Flip)
* * * * * *Dim thumb As System.Drawing.Image
* * * * * *Try
* * * * * * * *thumb = _inputImage
* * * * * * * *Select Case Flip
* * * * * * * * * *Case "90"
* * * * * * * * * * * *thumb.RotateFlip(RotateFlipType.Rotate90FlipXY)
* * * * * * * * * *Case "180"
* * * * * * * * * * * *thumb.RotateFlip(RotateFlipType.Rotate180FlipXY)
* * * * * * * * * *Case "270"
* * * * * * * * * * * *thumb.RotateFlip(RotateFlipType.Rotate270FlipXY)
* * * * * * * *End Select
* * * * * *Catch er As Exception
* * * * * * * *TakeNote("ROTATE ERR: " & er.ToString)
* * * * * * * *Throw
* * * * * *Finally
* * * * * * * *thumb.Save(_pathOutputImage)
* * * * * * * *_inputImage = thumb
* * * * * * * *thumb.Dispose()
* * * * * *End Try
* * * *End Function
* * * *Public Function Resize(ByVal Width As String, ByVal Height As String)
* * * * * *_pathOutputImage = getRandomName()
* * * * * *Dim thumb As System.Drawing.Image
* * * * * *thumb = _inputImage
* * * * * *Dim inp As New IntPtr()
* * * * * *Dim strMax As String = Width
* * * * * *If CInt(thumb.Width) > CInt(thumb.Height) Then
* * * * * * * *Width = strMax
* * * * * * * *Height = thumb.Height / (thumb.Width / Width)
* * * * * *Else
* * * * * * * *Height = CInt(strMax)
* * * * * * * *Width = thumb.Width / (thumb.Height / CInt(Height))
* * * * * *End If
* * * * * *Try
* * * * * * * *TakeNote("Resize " & Height & " x " & Width)
* * * * * * * *If Height > 0 And Width > 0 Then
* * * * * * * * * *thumb.RotateFlip(RotateFlipType.Rotate90FlipXY)
* * * * * * * * * *thumb.RotateFlip(RotateFlipType.Rotate270FlipXY)
* * * * * * * * * *If CInt(thumb.Width) > CInt(Width) Then
* * * * * * * * * * * *thumb = thumb.GetThumbnailImage(Width, Height, Nothing, inp)
* * * * * * * * * *End If
* * * * * * * *Else
* * * * * * * * * *thumb = _inputImage
* * * * * * * *End If
* * * * * *Catch er As Exception
* * * * * * * *TakeNote("RESIZE ERR: " & er.ToString)
* * * * * * * *Throw er
* * * * * *Finally
* * * * * * * *myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
* * * * * * * *myEncoder = Imaging.Encoder.Quality
* * * * * * * *myEncoderParameters = New EncoderParameters(1)
* * * * * * * *myEncoderParameter = New EncoderParameter(myEncoder, CType(75L, Int32))
* * * * * * * *myEncoderParameters.Param(0) = myEncoderParameter
* * * * * * * *thumb.Save(_pathOutputImage, myImageCodecInfo, myEncoderParameters)
* * * * * * * *_inputImage = thumb
* * * * * * * *thumb.Dispose()
* * * * * * * *thumb = Nothing
* * * * * * * *myImageCodecInfo = Nothing
* * * * * * * *myEncoder = Nothing
* * * * * * * *myEncoderParameters = Nothing
* * * * * * * *myEncoderParameter = Nothing
* * * * * *End Try
* * * *End Function
* * * *Public Function SaveImage() As String
* * * * * *Log = Nothing
* * * * * *Return _strFileName
* * * *End Function
* * * *Private Sub TakeNote(ByVal str As String)
* * * * * *Log.Append(str & vbCrLf)
* * * *End Sub
* * * *Private Function RndString(Optional ByVal sLen As Integer = 15) As String
* * * * * *Dim r As String = ""
* * * * * *Dim i As Integer
* * * * * *Randomize()
* * * * * *For i = 1 To sLen
* * * * * * * *Randomize(Int((57 - 48 + 1) * Rnd() + 48))
* * * * * * * *Randomize()
* * * * * * * *If Int(2 * Rnd() + 1) = 1 Then
* * * * * * * * * *r = r + Chr(Int((57 - 48 + 1) * Rnd() + 48))
* * * * * * * *Else
* * * * * * * * * *r = r + Chr(Int((90 - 65 + 1) * Rnd() + 65))
* * * * * * * *End If
* * * * * *Next i
* * * * * *Return DateTime.Now.Year & DateTime.Now.Month & DateTime.Now.Day & r
* * * *End Function
* * * *Private Shared Function GetEncoderInfo(ByVal format As Imaging.ImageFormat) As Imaging.ImageCodecInfo
* * * * * *Dim j As Integer
* * * * * *Dim encoders() As Imaging.ImageCodecInfo
* * * * * *encoders = Imaging.ImageCodecInfo.GetImageEncoders()
* * * * * *j = 0
* * * * * *While j < encoders.Length
* * * * * * * *If encoders(j).FormatID = format.Guid Then
* * * * * * * * * *Return encoders(j)
* * * * * * * *End If
* * * * * * * *j += 1
* * * * * *End While
* * * * * *Return Nothing
* * * *End Function 'GetEncoderInfo
* *End Class
End Namespace
För att bara skala om bilder;
Kod:
Dim ImageResize As New Image.Edit.Edit(strPath & "\" & strFilnamn)
ImageResize.FileName(strFilnamn)
ImageResize.Resize("640", "")
ImageResize.SaveImage()
ImageResize = Nothing