Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.86 KB | Source Code | 0 0
  1. Option Explicit
  2. 'Autor: Leandro Ascierto
  3. 'Web: www.leandroascierto.com
  4. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  5. Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByRef lplpVoid As Any, ByVal handle As Long, ByVal dw As Long) As Long
  6. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  7. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  8. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  9. Private Declare Function UpdateLayeredWindow Lib "user32" (ByVal hwnd As Long, ByVal hdcDst As Long, pptDst As Any, psize As Any, ByVal hdcSrc As Long, pptSrc As Any, ByVal crKey As Long, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Long) As Long
  10. Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  11. Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  12. Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (ByRef Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
  13.  
  14. Private Declare Function GdipCreateBitmapFromScan0 Lib "GdiPlus.dll" (ByVal mWidth As Long, ByVal mHeight As Long, ByVal mStride As Long, ByVal mPixelFormat As Long, ByVal mScan0 As Long, ByRef mBitmap As Long) As Long
  15. Private Declare Function GdipDisposeImage Lib "GdiPlus.dll" (ByVal mImage As Long) As Long
  16. Private Declare Function GdipSaveImageToFile Lib "gdiplus" (ByVal image As Long, ByVal FileName As Long, ByRef clsidEncoder As GUID, ByRef encoderParams As Any) As Long
  17. Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
  18. Private Declare Function GdipImageRotateFlip Lib "GdiPlus.dll" (ByVal mImage As Long, ByVal mRfType As Long) As Long
  19.  
  20. Private Const PixelFormat32bppPARGB     As Long = &HE200B
  21. Const ImageCodecPNG = "{557CF406-1A04-11D3-9A73-0000F81EF32E}"
  22.  
  23. Private Type GUID
  24.     Data1           As Long
  25.     Data2           As Integer
  26.     Data3           As Integer
  27.     Data4(0 To 7)   As Byte
  28. End Type
  29.  
  30.  
  31. Private Type POINTAPI
  32.    X                        As Long
  33.    Y                        As Long
  34. End Type
  35.  
  36. Private Type Size
  37.    cx                       As Long
  38.    cy                       As Long
  39. End Type
  40.  
  41. Private Type RECT
  42.     Left As Long
  43.     Top As Long
  44.     Right As Long
  45.     Bottom As Long
  46. End Type
  47.  
  48. Private Type BITMAPINFOHEADER
  49.    biSize                   As Long
  50.    biWidth                  As Long
  51.    biHeight                 As Long
  52.    biPlanes                 As Integer
  53.    biBitCount               As Integer
  54.    biCompression            As Long
  55.    biSizeImage              As Long
  56.    biXPelsPerMeter          As Long
  57.    biYPelsPerMeter          As Long
  58.    biClrUsed                As Long
  59.    biClrImportant           As Long
  60. End Type
  61.  
  62. Private Type BITMAPINFO
  63.    bmiHeader                As BITMAPINFOHEADER
  64.    bmiColors(3)             As Byte
  65. End Type
  66.  
  67. Private Type BLENDFUNCTION
  68.    BlendOp                  As Byte
  69.    BlendFlags               As Byte
  70.    SourceConstantAlpha      As Byte
  71.    AlphaFormat              As Byte
  72. End Type
  73.  
  74. Private Const ULW_ALPHA         As Long = &H2
  75. Private Const BI_RGB            As Long = 0&
  76. Private Const DIB_RGB_COLORS    As Long = 0&
  77. Private Const AC_SRC_ALPHA      As Long = &H1
  78.  
  79. Private Const GWL_EXSTYLE       As Long = -20
  80. Private Const WS_EX_TOPMOST     As Long = &H8&
  81. Private Const WS_EX_LAYERED     As Long = &H80000
  82.  
  83. Private c_lhDC          As Long
  84. Private c_lDIB          As Long
  85. Private m_Pointer       As Long
  86. Private m_OldBmp        As Long
  87. Private tSIZE           As Size
  88.  
  89. Private tBLENDFUNCTION  As BLENDFUNCTION
  90.  
  91. Public Sub DestroyCanvas()
  92.     If c_lDIB Then
  93.         Call DeleteObject(SelectObject(c_lhDC, m_OldBmp)): c_lDIB = 0
  94.         Call DeleteDC(c_lhDC): c_lhDC = 0
  95.     End If
  96. End Sub
  97.  
  98. Public Function CreateCanvas(ByVal Width As Long, ByVal Height As Long) As Boolean
  99.     Dim tBITMAPINFO     As BITMAPINFO
  100.  
  101.     If c_lDIB <> 0 Then DestroyCanvas
  102.  
  103.     tSIZE.cx = Width
  104.     tSIZE.cy = Height
  105.  
  106.     With tBITMAPINFO.bmiHeader
  107.         .biSize = Len(tBITMAPINFO.bmiHeader)
  108.         .biBitCount = 32
  109.         .biHeight = tSIZE.cy
  110.         .biWidth = tSIZE.cx
  111.         .biPlanes = 1
  112.         .biSizeImage = .biWidth * .biHeight * 4
  113.     End With
  114.  
  115.     c_lhDC = CreateCompatibleDC(0)
  116.     c_lDIB = CreateDIBSection(c_lhDC, tBITMAPINFO, DIB_RGB_COLORS, m_Pointer, 0&, 0&)
  117.     m_OldBmp = SelectObject(c_lhDC, c_lDIB)
  118.  
  119.     CreateCanvas = c_lDIB <> 0
  120. End Function
  121.  
  122. Public Sub Clear()
  123.     If m_Pointer Then FillMemory ByVal m_Pointer, tSIZE.cx * tSIZE.cy * 4&, 0
  124. End Sub
  125.  
  126. Public Property Get hdc() As Long
  127.     hdc = c_lhDC
  128. End Property
  129.  
  130. Public Sub UpdateLayered(hwnd As Long, Optional ByVal Alpha As Byte = 255)
  131.     Dim tPT As POINTAPI
  132.     Dim ExStyle As Long
  133.  
  134.     ExStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
  135.     If (ExStyle And WS_EX_LAYERED) <> WS_EX_LAYERED Then
  136.         Call SetWindowLong(hwnd, GWL_EXSTYLE, ExStyle Or WS_EX_LAYERED)
  137.     End If
  138.    
  139.     With tBLENDFUNCTION
  140.         .AlphaFormat = AC_SRC_ALPHA
  141.         .SourceConstantAlpha = Alpha
  142.     End With
  143.  
  144.     Call UpdateLayeredWindow(hwnd, 0&, ByVal 0&, tSIZE, c_lhDC, tPT, 0&, tBLENDFUNCTION, ULW_ALPHA)
  145. End Sub
  146.  
  147.  
  148. Public Function SaveAsPng(FileName As String) As Boolean
  149.     Dim hImage As Long
  150.     Dim tEncoder  As GUID
  151.  
  152.     GdipCreateBitmapFromScan0 tSIZE.cx, tSIZE.cy, tSIZE.cx * 4, PixelFormat32bppPARGB, ByVal m_Pointer, hImage
  153.     GdipImageRotateFlip hImage, &H6
  154.     CLSIDFromString StrPtr(ImageCodecPNG), tEncoder
  155.     SaveAsPng = GdipSaveImageToFile(hImage, StrPtr(FileName), tEncoder, ByVal 0) = 0
  156.     GdipDisposeImage hImage
  157. End Function
  158.  
  159. Private Sub Class_Terminate()
  160.     DestroyCanvas
  161. End Sub
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement