TizzyT

TransparentJPG NoJpgLib -TizzyT

Oct 16th, 2015
1,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.08 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Drawing
  3. Imports System.Drawing.Imaging
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class TransparentJPG
  7.     Private Shared ReadOnly _Magic() As Byte = {&H71, &H22, &H47, &H0}
  8.     Private Shared ReadOnly JMAGIC() As Byte = {&HFF, &HD8, &HFF, &HE0}
  9.     Private _Image As Bitmap
  10.  
  11.     Public ReadOnly Property Width() As Integer
  12.         Get
  13.             Return _Image.Width
  14.         End Get
  15.     End Property
  16.  
  17.     Public ReadOnly Property Height() As Integer
  18.         Get
  19.             Return _Image.Height
  20.         End Get
  21.     End Property
  22.  
  23.     Public Shared Sub SaveTJPG(ByVal Path As String, ByRef Image As Bitmap, Optional Quality As Long = 75)
  24.         Dim RBitmap As New Bitmap(Image.Width, Image.Height * 2)
  25.         Dim G As Graphics = Graphics.FromImage(RBitmap)
  26.         G.DrawImage(Image, New Rectangle(0, 0, Image.Width, Image.Height))
  27.         G.DrawImage(Image, New Rectangle(0, Image.Height, Image.Width, Image.Height))
  28.         G.Dispose()
  29.         Dim bitmapData As BitmapData = RBitmap.LockBits(New Rectangle(0, 0, RBitmap.Width, RBitmap.Height), _
  30.                                                         ImageLockMode.ReadWrite, RBitmap.PixelFormat)
  31.         Dim Iptr = bitmapData.Scan0
  32.         Dim Pixels(RBitmap.Width * RBitmap.Height * 4 - 1) As Byte
  33.         Marshal.Copy(Iptr, Pixels, 0, Pixels.Length)
  34.         Dim AlphaPos As Integer = 3
  35.         For i = Pixels.Length / 2 To Pixels.Length - 1 Step 4
  36.             Dim A As Byte = Pixels(AlphaPos)
  37.             Pixels(AlphaPos) = 255
  38.             Pixels(i) = A
  39.             Pixels(i + 1) = A
  40.             Pixels(i + 2) = A
  41.             Pixels(i + 3) = 255
  42.             AlphaPos += 4
  43.         Next
  44.         Marshal.Copy(Pixels, 0, Iptr, Pixels.Length)
  45.         RBitmap.UnlockBits(bitmapData)
  46.         Using ms As New MemoryStream
  47.             Dim qualityParam As New EncoderParameter(Encoder.Quality, Quality)
  48.             Dim encoderParams As New EncoderParameters(1)
  49.             encoderParams.Param(0) = qualityParam
  50.             Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
  51.             For i As Integer = 0 To codecs.Length - 1
  52.                 If (codecs(i).MimeType = "image/jpeg") Then
  53.                     RBitmap.Save(ms, codecs(i), encoderParams)
  54.                     RBitmap.Dispose()
  55.                     ms.Flush()
  56.                     Dim Data() As Byte = ms.GetBuffer.Skip(10).ToArray
  57.                     Using fs As New FileStream(Path, FileMode.Create)
  58.                         fs.Write(_Magic, 0, _Magic.Length)
  59.                         fs.Write(Data, 0, Data.Length)
  60.                         fs.Flush()
  61.                         fs.Close()
  62.                     End Using
  63.                 End If
  64.             Next
  65.             ms.Close()
  66.         End Using
  67.     End Sub
  68.  
  69.     Public Sub New(ByVal Path As String)
  70.         Dim SourceData() As Byte = System.IO.File.ReadAllBytes(Path)
  71.         For i = 0 To 3
  72.             SourceData(i) = JMAGIC(i)
  73.         Next
  74.         Dim MainBM As Image
  75.         Using ms As New MemoryStream(SourceData)
  76.             MainBM = Image.FromStream(ms)
  77.             ms.Close()
  78.         End Using
  79.         Dim HalfHeight As Integer = MainBM.Height / 2
  80.         Using Colors As New Bitmap(MainBM.Width, HalfHeight)
  81.             Using Alphas As New Bitmap(MainBM.Width, HalfHeight)
  82.                 Dim PixelCount As Integer = Colors.Width * Colors.Height * 4 - 1
  83.                 Dim G As Graphics = Graphics.FromImage(Colors)
  84.                 G.DrawImage(MainBM, New Rectangle(0, 0, MainBM.Width, MainBM.Height))
  85.                 G = Graphics.FromImage(Alphas)
  86.                 G.DrawImage(MainBM, New Rectangle(0, 0 - HalfHeight, MainBM.Width, MainBM.Height))
  87.                 G.Dispose()
  88.                 MainBM.Dispose()
  89.                 Dim ColorbitmapData As BitmapData = _
  90.                     Colors.LockBits(New Rectangle(0, 0, Colors.Width, Colors.Height), _
  91.                                     ImageLockMode.ReadWrite, Colors.PixelFormat)
  92.                 Dim ColorPixels(PixelCount) As Byte
  93.                 Dim ColorIptr As IntPtr = ColorbitmapData.Scan0
  94.                 Marshal.Copy(ColorIptr, ColorPixels, 0, ColorPixels.Length)
  95.                 Dim AlphaDepth As Integer = System.Drawing.Bitmap.GetPixelFormatSize(Alphas.PixelFormat)
  96.                 Dim AlphabitmapData As BitmapData = _
  97.                     Alphas.LockBits(New Rectangle(0, 0, Alphas.Width, Alphas.Height), _
  98.                                     ImageLockMode.ReadWrite, Alphas.PixelFormat)
  99.                 Dim AlphaPixels(PixelCount) As Byte
  100.                 Dim AlphaIptr As IntPtr = AlphabitmapData.Scan0
  101.                 Marshal.Copy(AlphaIptr, AlphaPixels, 0, AlphaPixels.Length)
  102.                 For i = 0 To AlphaPixels.Length - 1 Step 4
  103.                     Dim A As Single = AlphaPixels(i)
  104.                     A += AlphaPixels(i + 1)
  105.                     A += AlphaPixels(i + 2)
  106.                     ColorPixels(i + 3) = Math.Round(A / 3)
  107.                 Next
  108.                 Marshal.Copy(ColorPixels, 0, ColorIptr, ColorPixels.Length)
  109.                 Marshal.Copy(AlphaPixels, 0, AlphaIptr, AlphaPixels.Length)
  110.                 Colors.UnlockBits(ColorbitmapData)
  111.                 Alphas.UnlockBits(AlphabitmapData)
  112.                 Alphas.Dispose()
  113.                 _Image = Colors.Clone
  114.                 Colors.Dispose()
  115.             End Using
  116.         End Using
  117.     End Sub
  118.  
  119.     Public Shared Sub Decode(ByVal Data() As Byte, _
  120.                              ByRef Pixels() As Byte, ByRef Width As Integer, ByRef Height As Integer)
  121.         For i = 0 To 3
  122.             Data(i) = JMAGIC(i)
  123.         Next
  124.         Dim MainBM As Image
  125.         Using ms As New MemoryStream(Data)
  126.             MainBM = Image.FromStream(ms)
  127.             ms.Close()
  128.         End Using
  129.         Dim HalfHeight As Integer = MainBM.Height / 2
  130.         Using Colors As New Bitmap(MainBM.Width, HalfHeight)
  131.             Using Alphas As New Bitmap(MainBM.Width, HalfHeight)
  132.                 Dim PixelCount As Integer = Colors.Width * Colors.Height * 4 - 1
  133.                 Dim G As Graphics = Graphics.FromImage(Colors)
  134.                 G.DrawImage(MainBM, New Rectangle(0, 0, MainBM.Width, MainBM.Height))
  135.                 G = Graphics.FromImage(Alphas)
  136.                 G.DrawImage(MainBM, New Rectangle(0, 0 - HalfHeight, MainBM.Width, MainBM.Height))
  137.                 G.Dispose()
  138.                 MainBM.Dispose()
  139.                 Dim ColorbitmapData As BitmapData = _
  140.                     Colors.LockBits(New Rectangle(0, 0, Colors.Width, Colors.Height), _
  141.                                     ImageLockMode.ReadWrite, Colors.PixelFormat)
  142.                 Dim ColorPixels(PixelCount) As Byte
  143.                 Dim ColorIptr As IntPtr = ColorbitmapData.Scan0
  144.                 Marshal.Copy(ColorIptr, ColorPixels, 0, ColorPixels.Length)
  145.                 Dim AlphaDepth As Integer = System.Drawing.Bitmap.GetPixelFormatSize(Alphas.PixelFormat)
  146.                 Dim AlphabitmapData As BitmapData = _
  147.                     Alphas.LockBits(New Rectangle(0, 0, Alphas.Width, Alphas.Height), _
  148.                                     ImageLockMode.ReadWrite, Alphas.PixelFormat)
  149.                 Dim AlphaPixels(PixelCount) As Byte
  150.                 Dim AlphaIptr As IntPtr = AlphabitmapData.Scan0
  151.                 Marshal.Copy(AlphaIptr, AlphaPixels, 0, AlphaPixels.Length)
  152.                 For i = 0 To AlphaPixels.Length - 1 Step 4
  153.                     Dim A As Single = AlphaPixels(i)
  154.                     A += AlphaPixels(i + 1)
  155.                     A += AlphaPixels(i + 2)
  156.                     ColorPixels(i + 3) = Math.Round(A / 3)
  157.                 Next
  158.                 Marshal.Copy(ColorPixels, 0, ColorIptr, ColorPixels.Length)
  159.                 Marshal.Copy(AlphaPixels, 0, AlphaIptr, AlphaPixels.Length)
  160.                 Colors.UnlockBits(ColorbitmapData)
  161.                 Alphas.UnlockBits(AlphabitmapData)
  162.                 Pixels = ColorPixels
  163.                 Width = Colors.Width
  164.                 Height = Colors.Height
  165.                 Colors.Dispose()
  166.                 Alphas.Dispose()
  167.             End Using
  168.         End Using
  169.     End Sub
  170.  
  171.     Public Shared Function LoadTJPG(ByVal Path As String) As Bitmap
  172.         Dim SourceData() As Byte = System.IO.File.ReadAllBytes(Path)
  173.         For i = 0 To 3
  174.             SourceData(i) = JMAGIC(i)
  175.         Next
  176.         Dim MainBM As Image
  177.         Using ms As New MemoryStream(SourceData)
  178.             MainBM = Image.FromStream(ms)
  179.             ms.Close()
  180.         End Using
  181.         Dim HalfHeight As Integer = MainBM.Height / 2
  182.         Using Colors As New Bitmap(MainBM.Width, HalfHeight)
  183.             Using Alphas As New Bitmap(MainBM.Width, HalfHeight)
  184.                 Dim PixelCount As Integer = Colors.Width * Colors.Height * 4 - 1
  185.                 Dim G As Graphics = Graphics.FromImage(Colors)
  186.                 G.DrawImage(MainBM, New Rectangle(0, 0, MainBM.Width, MainBM.Height))
  187.                 G = Graphics.FromImage(Alphas)
  188.                 G.DrawImage(MainBM, New Rectangle(0, 0 - HalfHeight, MainBM.Width, MainBM.Height))
  189.                 G.Dispose()
  190.                 MainBM.Dispose()
  191.                 Dim ColorbitmapData As BitmapData = _
  192.                     Colors.LockBits(New Rectangle(0, 0, Colors.Width, Colors.Height), _
  193.                                     ImageLockMode.ReadWrite, Colors.PixelFormat)
  194.                 Dim ColorPixels(PixelCount) As Byte
  195.                 Dim ColorIptr As IntPtr = ColorbitmapData.Scan0
  196.                 Marshal.Copy(ColorIptr, ColorPixels, 0, ColorPixels.Length)
  197.                 Dim AlphaDepth As Integer = System.Drawing.Bitmap.GetPixelFormatSize(Alphas.PixelFormat)
  198.                 Dim AlphabitmapData As BitmapData = _
  199.                     Alphas.LockBits(New Rectangle(0, 0, Alphas.Width, Alphas.Height), _
  200.                                     ImageLockMode.ReadWrite, Alphas.PixelFormat)
  201.                 Dim AlphaPixels(PixelCount) As Byte
  202.                 Dim AlphaIptr As IntPtr = AlphabitmapData.Scan0
  203.                 Marshal.Copy(AlphaIptr, AlphaPixels, 0, AlphaPixels.Length)
  204.                 For i = 0 To AlphaPixels.Length - 1 Step 4
  205.                     Dim A As Single = AlphaPixels(i)
  206.                     A += AlphaPixels(i + 1)
  207.                     A += AlphaPixels(i + 2)
  208.                     ColorPixels(i + 3) = Math.Round(A / 3)
  209.                 Next
  210.                 Marshal.Copy(ColorPixels, 0, ColorIptr, ColorPixels.Length)
  211.                 Marshal.Copy(AlphaPixels, 0, AlphaIptr, AlphaPixels.Length)
  212.                 Colors.UnlockBits(ColorbitmapData)
  213.                 Alphas.UnlockBits(AlphabitmapData)
  214.                 Alphas.Dispose()
  215.                 LoadTJPG = Colors.Clone
  216.                 Colors.Dispose()
  217.             End Using
  218.         End Using
  219.     End Function
  220.  
  221.     Public Shared Widening Operator CType(ByVal TJPG As TransparentJPG) As Bitmap
  222.         Return TJPG._Image
  223.     End Operator
  224.  
  225.     Public ReadOnly Property ImageStream() As Stream
  226.         Get
  227.             Dim ms As New MemoryStream
  228.             _Image.Save(ms, Drawing.Imaging.ImageFormat.Png)
  229.             ms.Flush()
  230.             Return ms
  231.         End Get
  232.     End Property
  233.  
  234.     Public Sub Dispose()
  235.         _Image.Dispose()
  236.     End Sub
  237. End Class
Advertisement
Add Comment
Please, Sign In to add comment