Attribute VB_Name = "PicConv"
Option Explicit


Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
'Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
Private Type BITMAPINFOHEADER '40 bytes
        biSize As Long
        biWidth As Long
        biHeight As Long
        biPlanes As Integer
        biBitCount As Integer
        biCompression As Long
        biSizeImage As Long
        biXPelsPerMeter As Long
        biYPelsPerMeter As Long
        biClrUsed As Long
        biClrImportant As Long
End Type
Private Type RGBQUAD
        rgbBlue As Byte
        rgbGreen As Byte
        rgbRed As Byte
        rgbReserved As Byte
End Type
Private Type BITMAPINFO
        bmiHeader As BITMAPINFOHEADER
        bmiColors(255) As RGBQUAD ' or 0,1,15,255
End Type
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Dim iBitmap As Long
Dim iDC As Long
Dim XBinfo As BITMAPINFO
Dim bBytes() As Byte
Dim Cnt As Long
Dim Bpp As Integer
Global Bppa As Integer

Sub SetDib(Pwidth As Integer, Pheight As Integer, bitdepth As Integer, Optional Gray As Boolean)
Dim t As Integer
Dim tt As Integer
Bpp = bitdepth
'If n = 0 Then Bpp = 1
'If n = 1 Then Bpp = 4
'If n = 2 Then Bpp = 8
'If n = 3 Then Bpp = 16
'If n = 4 Then Bpp = 24
'If n = 5 Then Bpp = 32

'Create BMP header
    With XBinfo.bmiHeader
        .biBitCount = Bpp   'bpp
        .biCompression = BI_RGB
        .biPlanes = 1
        .biSize = Len(XBinfo.bmiHeader)
        .biWidth = Pwidth
        .biHeight = Pheight
       End With


'Create Palette
If Bpp = 8 Then
  For t = 0 To 255
'Color
  XBinfo.bmiColors(t).rgbBlue = (t And 3) * 64 * 1.328
  XBinfo.bmiColors(t).rgbGreen = (t And 28) * 8 * 1.138
  XBinfo.bmiColors(t).rgbRed = (t And 224) * 1.138
'BW
If Gray Then
  XBinfo.bmiColors(t).rgbBlue = t
  XBinfo.bmiColors(t).rgbRed = t
  XBinfo.bmiColors(t).rgbGreen = t
End If
Next t
End If


If Bpp = 4 Then
  For t = 0 To 15
'Color
tt = t * 17
  XBinfo.bmiColors(t).rgbBlue = (tt And 3) * 64 * 1.328
  XBinfo.bmiColors(t).rgbGreen = (tt And 28) * 8 * 1.138
  XBinfo.bmiColors(t).rgbRed = (tt And 224) * 1.138
 'BW
If Gray Then
  XBinfo.bmiColors(t).rgbBlue = tt
  XBinfo.bmiColors(t).rgbRed = tt
  XBinfo.bmiColors(t).rgbGreen = tt
End If
Next t
End If



If Bpp = 1 Then
  For t = 0 To 1
'Color
tt = t * 255
  XBinfo.bmiColors(t).rgbBlue = (tt And 3) * 64 * 1.328
  XBinfo.bmiColors(t).rgbGreen = (tt And 28) * 8 * 1.138
  XBinfo.bmiColors(t).rgbRed = (tt And 224) * 1.138
  Next t
End If

End Sub

Sub MakeDIB(Pbox As PictureBox)
'Convert picture
   ReDim bBytes(0 To XBinfo.bmiHeader.biWidth * XBinfo.bmiHeader.biHeight * Bpp \ 8) As Byte
    iDC = CreateCompatibleDC(0)
    iBitmap = CreateDIBSection(iDC, XBinfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
    SelectObject iDC, iBitmap
    BitBlt iDC, 0, 0, XBinfo.bmiHeader.biWidth, XBinfo.bmiHeader.biHeight, Pbox.hdc, 0, 0, vbSrcCopy
    GetDIBits iDC, iBitmap, 0, XBinfo.bmiHeader.biHeight, bBytes(1), XBinfo, DIB_RGB_COLORS
    'SetDIBitsToDevice pic2.hdc, 0, 0, XBinfo.bmiHeader.biWidth, XBinfo.bmiHeader.biHeight, 0, 0, 0, XBinfo.bmiHeader.biHeight, bBytes(1), XBinfo, DIB_RGB_COLORS
    DeleteDC iDC
    DeleteObject iBitmap
End Sub

Sub ShowDIB(Pbox As PictureBox)
SetDIBitsToDevice Pbox.hdc, 0, 0, XBinfo.bmiHeader.biWidth, XBinfo.bmiHeader.biHeight, 0, 0, 0, XBinfo.bmiHeader.biHeight, bBytes(1), XBinfo, DIB_RGB_COLORS
End Sub

Sub GetScreen(Pbox As PictureBox)
Dim ret As Long
SetStretchBltMode Pbox.hdc, 4 'HALFTONE

ret = StretchBlt(Pbox.hdc, 0, 0, Pbox.ScaleWidth, Pbox.ScaleHeight, GetWindowDC(GetDesktopWindow), 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY, vbSrcCopy)
Pbox.Refresh
End Sub


Sub SendDIBpic()

Form1.wsock.SendData bBytes
Form1.Label4 = UBound(bBytes)
End Sub
