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