2008-04-06

圖形尺寸, 格式, 解析度轉存及壓縮處理

Dim CopyRightFont As New Font("Tahoma", 56, FontStyle.Bold)
  Dim NewItemFont As New Font("Tahoma", 140, FontStyle.Bold)
  Dim frame As Image = Nothing
  If CheckFrame.Checked Then frame = Image.FromFile("Images/frame.png")
  Dim AllFiles As Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(TextSource.Text)
  Dim TotalFiles As Integer = AllFiles.Count
  Dim CurrentFileIndex As Integer = 0
  Dim Progress As String = "Start Converting…"
  Dim jpgEncoder As Imaging.ImageCodecInfo = GetEncoder(Imaging.ImageFormat.Jpeg)
  Dim myEncoder As Imaging.Encoder = Imaging.Encoder.Quality
  Dim myEncoderParameters As New Imaging.EncoderParameters(1)
  Dim myEncoderParameter As New Imaging.EncoderParameter(myEncoder, TrackQuality.Value)
  myEncoderParameters.Param(0) = myEncoderParameter
  For Each file As String In AllFiles
   CurrentFileIndex += 1
   Dim FileName As String = My.Computer.FileSystem.GetFileInfo(file).Name
   FileName = Mid(FileName, 1, InStrRev(FileName, ".") – 1)
   Dim FileExtension As String = LCase(My.Computer.FileSystem.GetFileInfo(file).Extension)
   Progress += vbCrLf & FileName
   RichTextResult.Text = Progress & vbCrLf & CurrentFileIndex & " of " & TotalFiles
   ’——This is for auto scroll vertical scrollbar to the bottom-
   RichTextResult.Select(Progress.Length + 1, 1)
   RichTextResult.ScrollToCaret()
   ’————————————————————–
   Dim img As Image = Image.FromFile(file)
   Dim TheG As Graphics = Graphics.FromImage(img)
   If CheckCopyright.Checked Then TheG.DrawString(TextCopyright.Text, CopyRightFont, Brushes.Yellow, img.Width / 3, img.Height / 2)
   If CheckNewItem.Checked Then TheG.DrawString("New Item", NewItemFont, Brushes.Red, 200, 200)
   If CheckFrame.Checked Then TheG.DrawImage(frame, 0, 0, img.Width, img.Height)
   Dim format As Imaging.ImageFormat = img.RawFormat
   If CheckLarge.Checked Then
    Dim ImgOutput As New Bitmap(img, NumTextLargeW.Text, NumTextLargeH.Text)
    ImgOutput.Save(TextLarge.Text & "\" & FileName & FileExtension, jpgEncoder, myEncoderParameters)
    ImgOutput.Dispose()
   End If
   If CheckSmall.Checked Then
    Dim ImgOutput As New Bitmap(img, NumTextSmallW.Text, NumTextSmallH.Text)
    ImgOutput.Save(TextSmall.Text & "\" & FileName & FileExtension, jpgEncoder, myEncoderParameters)
    ImgOutput.Dispose()
   End If
   img.Dispose()
  Next
  If Not IsNothing(frame) Then frame.Dispose()
 
Private Function GetEncoder(ByVal format As Imaging.ImageFormat) As Imaging.ImageCodecInfo
  Dim codecs As Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageDecoders()
  Dim codec As Imaging.ImageCodecInfo
  For Each codec In codecs
   If codec.FormatID = format.Guid Then
    Return codec
   End If
  Next codec
  Return Nothing
 End Function

沒有留言:

張貼留言