Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. Размер = Высота*Ширина*КоличествоБайтНаКаждыйПиксель
  2.  
  3. Imports System.IO
  4. Imports System.Drawing.Imaging
  5.  
  6. Module All
  7.  
  8. Public Function GetJpegContent(ByVal Pct As Image) As Byte()
  9. Dim File As New MemoryStream
  10. Pct.Save(File, ImageFormat.Jpeg)
  11. Return File.ToArray()
  12. End Function
  13.  
  14. Public Function GetJpegContent(ByVal Pct As Image, ByVal Quality As Long) As Byte()
  15. Dim File As New MemoryStream
  16. Dim EncoderParams As New EncoderParameters(1)
  17. EncoderParams.Param(0) = New EncoderParameter(Encoder.Quality, Quality)
  18. Pct.Save(File, GetEncoderInfo("image/jpeg"), EncoderParams)
  19. Return File.ToArray()
  20. End Function
  21.  
  22. Private Function GetEncoderInfo(ByVal MimeType As String) As ImageCodecInfo
  23. For Each Codec As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
  24. If Codec.MimeType = MimeType Then Return Codec
  25. Next Codec
  26. Return Nothing
  27. End Function
  28.  
  29. Public Function ReduceByQuality(ByVal Pct As Image, ByVal Lim As Integer) As Byte()
  30. Dim LastOk() As Byte = Nothing, Res() As Byte
  31. Dim L As Integer = 0, R As Integer = 100, Cur As Integer
  32.  
  33. Do While L < R
  34. Cur = (L + R + 1) >> 1
  35. Res = GetJpegContent(Pct, Cur)
  36. If Res.Length > Lim Then
  37. R = Cur - 1
  38. Else
  39. L = Cur
  40. LastOk = Res
  41. End If
  42. Loop
  43.  
  44. Return LastOk
  45. End Function
  46.  
  47. Public Function ReduceBySize(ByVal Pct As Image, ByVal Lim As Integer) As Byte()
  48. Dim LastOk() As Byte = Nothing, Res() As Byte
  49. Dim LHeight As Integer = 0, RHeight As Integer = Pct.Height, CurHeight As Integer
  50. Dim LWidth As Integer = 0, RWidth As Integer = Pct.Width, CurWidth As Integer
  51.  
  52. Do While LHeight < RHeight
  53. CurHeight = (LHeight + RHeight + 1) >> 1
  54. CurWidth = (LWidth + RWidth + 1) >> 1
  55. Res = GetJpegContent(New System.Drawing.Bitmap(CType(Pct, Bitmap), CurWidth, CurHeight))
  56. If Res.Length > Lim Then
  57. RHeight = CurHeight - 1
  58. RWidth = CurWidth - 1
  59. Else
  60. LHeight = CurHeight
  61. LWidth = CurWidth
  62. LastOk = Res
  63. End If
  64. Loop
  65.  
  66. Return LastOk
  67. End Function
  68.  
  69. Public Sub Main()
  70. My.Computer.FileSystem.WriteAllBytes("ReduceByQuality.jpg", ReduceByQuality(Bitmap.FromFile("input.jpg"), 307200), False)
  71. My.Computer.FileSystem.WriteAllBytes("ReduceBySize.jpg", ReduceBySize(Bitmap.FromFile("input.jpg"), 307200), False)
  72. MsgBox("Ready")
  73. End Sub
  74.  
  75. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement