VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'SPEACIAL THANKS
'TO MIKLE(VBFORUMS): that make the rotate effect;
'LaVolte(VBFORUMS): i learned so many things thanks to him;
'VBFORUMS: i learned very with these forum;
'CerianKnight(XTREMEVBTALK): i learn very things... inclued DIB's with him;)
'XTREMEVBTALK: i learned very with these forum;
'Dennis Wynn

Private Type Color
    Red As Long
    Green As Long
    Blue As Long
    Alpha As Long
End Type

Public Enum MirrorState
    None = 0
    Horizontal = 1
    Vertical = 2
    HorizontalVertical = 3
End Enum

Private Type BITMAPINFOHEADER
  biSize As Long
  biWidth As Long
  biHeight As Long
  biPlanes As Integer
  biBitCount As Integer
  biCompression As Long
  biSizeImage As Long
  biXPelsPerMeter As Double
  biClrUsed As Double
End Type

Private Type BITMAPINFO
  bmiHeader As BITMAPINFOHEADER
  bmiColors As Long
End Type

Private Declare Function StretchDIBits Lib "gdi32" (ByVal hdc As Long, _
  ByVal x As Long, ByVal y As Long, ByVal dWidth As Long, ByVal dHeight _
  As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcWidth As _
  Long, ByVal SrcHeight As Long, lpBits As Any, lpBI As BITMAPINFO, _
  ByVal wUsage As Long, ByVal RasterOp 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

Dim bi32BitInfo As BITMAPINFO
Dim OriginalImage() As Long, ChangedImage() As Long, MaskImage() As Long, ShadowImage() As Long, ShadowMask() As Long
Dim inWidth As Long, inHeight As Long 'Size of OriginalImage
Dim outWidth As Long, outHeight As Long 'Size of ChangedImage
Dim sngRotate As Single
Dim blnTransparent As Boolean
Dim blnBlackAndWhite As Boolean
Dim lngBackColor As Long
Dim lngOldBackColor As Long
Dim lngNewColor As Long
Dim lngOldColor As Long
Dim lngTransparentColor As Long
Dim msMirrorState As MirrorState
Dim lngShadowColor As Long
Dim lngShadowX As Long
Dim lngShadowY As Long
Dim lngSelectionColor As Long
Dim lngSelectionWidth As Long
Dim blnStretch As Boolean
Dim blnTiles As Boolean
Dim lngOpacy As Long
Dim lngHeight As Long
Dim lngWidth As Long
Const PI As Single = 3.141593

Private Function GrayScale(ByVal lColor As Long) As Long
    GrayScale = ((77& * (lColor And &HFF&) + _
                 152& * (lColor And &HFF00&) \ &H100& + _
                  28& * (lColor \ &H10000)) \ 256&) * &H10101
End Function

Private Function OpacyChange(Pixel1 As Long, Pixel2 As Long, Opacy As Long) As Long
    
    Dim clrPixel1 As Color
    Dim clrPixel2 As Color
    Dim R As Long
    Dim G As Long
    Dim B As Long
    Dim dblAlpha As Long
    Dim dblAlphaDst As Long
    
    dblAlpha = Opacy / 100
    dblAlphaDst = 1 - dblAlpha
    
    clrPixel1 = RGBValues(Pixel1)
    clrPixel2 = RGBValues(Pixel2)
    
    R = (dblAlpha * clrPixel2.Red) + dblAlphaDst * clrPixel1.Red
    G = (dblAlpha * clrPixel2.Green) + dblAlphaDst * clrPixel1.Green
    B = (dblAlpha * clrPixel2.Blue) + dblAlphaDst * clrPixel1.Blue
    
    'Normal
    OpacyChange = RGB(R, G, B)
    
    'for DIB's
    'OpacyChange = RGB(B, G, R)
End Function

Private Function DIBRGB(ByVal c As Long) As Long
  DIBRGB = (c And &HFF&) * &H10000 Or (c And &HFF00&) Or (c And &HFF0000) \ &H10000
End Function

Private Function ConvertPositionY(PosY As Long) As Long
    ConvertPositionY = outHeight - PosY
End Function

Private Function RGBValues(ByVal Color As Long) As Color 'find the rgb color values of a color
    Dim ReturnColor As Color
    With ReturnColor
        .Red = Color And 255
        .Green = (Color And 65535) \ 256
        .Blue = (Color And &HFF0000) \ 65536
        .Alpha = ((Color And &HFF000000) \ 16777216) And &HFF
    End With
    RGBValues = ReturnColor
End Function

Public Sub GetImageData(Picture As Object)
    inWidth = Picture.ScaleWidth
    inHeight = Picture.ScaleHeight
    ReDim OriginalImage(inWidth - 1, inHeight - 1)
    With bi32BitInfo.bmiHeader
        .biBitCount = 32
        .biPlanes = 1
        .biSize = Len(bi32BitInfo.bmiHeader)
        .biWidth = inWidth
        .biHeight = inHeight
        .biSizeImage = 4 * inWidth * inHeight
    End With
    
    GetDIBits Picture.hdc, Picture.Image.Handle, 0, inHeight, OriginalImage(0, 0), bi32BitInfo, 0
    lngBackColor = OriginalImage(0, 0)
    lngOldBackColor = OriginalImage(0, 0)
    lngTransparentColor = OriginalImage(0, 0)
End Sub

Private Sub ChangeImage()
    Dim x As Long, y As Long
    Dim xx As Single, yy As Single
    Dim sx As Single, sy As Single
    Dim dx As Single, dy As Single
    Dim ix As Long, iy As Long
    Dim SinA As Single, CosA As Single
    Dim a As Single
    Dim clrNewColor As Color
    Dim clrOldColor As Color
    Dim TempColor As Long
    Dim TempColor1 As Long
    Dim i As Long
    Dim blnLeft As Boolean
    Dim blnUp As Boolean
    Dim TempX As Long
    Dim TempY As Long
       
    
    On Error Resume Next
    If Rotate <> 0 Then
        a = sngRotate * PI / 180
        SinA = Sin(a)
        CosA = Cos(a)
        outWidth = inWidth * Abs(Cos(45)) + inHeight * Abs(Sin(45))
        outHeight = inHeight * Abs(Cos(45)) + inWidth * Abs(Sin(45))
        sx = inWidth * 0.5
        sy = inHeight * 0.5
        dx = outWidth * 0.5
        dy = outHeight * 0.5
    Else
        outWidth = inWidth
        outHeight = inHeight
    End If
    
    ReDim ChangedImage(outWidth - 1, outHeight - 1)
    ReDim MaskImage(outWidth - 1, outHeight - 1)
    ReDim ShadowImage(outWidth - 1, outHeight - 1)
    ReDim ShadowMask(outWidth - 1, outHeight - 1)
    
    
    If Rotate <> 0 Then
        For y = 0 To outHeight - 1
            xx = sx - dx * CosA + (y - dy) * SinA
            yy = sy + (y - dy) * CosA + dx * SinA
            For x = 0 To outWidth - 1
                xx = xx + CosA
                yy = yy - SinA
                ix = xx
                iy = yy
                
                If ix < 0 Or ix >= inWidth Or iy < 0 Or iy >= inHeight Then
                    ChangedImage(x, y) = OriginalImage(0, 0)
                Else
                    ChangedImage(x, y) = OriginalImage(ix, iy)
                End If
            Next x
        Next y
    Else
        ChangedImage() = OriginalImage()
    End If
    
    For y = 0 To outHeight - 1
        For x = 0 To outWidth - 1
            
            'BackColor
            If lngBackColor <> lngOldBackColor Then
                TempColor = DIBRGB(lngOldBackColor)
                If ChangedImage(x, y) = TempColor Then
                    TempColor = DIBRGB(lngBackColor)
                    ChangedImage(x, y) = TempColor
                End If
            End If
            
            'Change Colors
            If lngOldColor <> lngNewColor Then
                TempColor = DIBRGB(lngOldColor)
                If ChangedImage(x, y) = TempColor Then
                    TempColor = DIBRGB(lngNewColor)
                    ChangedImage(x, y) = TempColor
                End If
            End If
            
            'Shadow Effect(needs more work)
            If ((lngShadowX <> 0) Or (lngShadowY <> 0)) And (lngShadowColor <> lngBackColor) Then
                TempY = -lngShadowY + y
                TempX = x + lngShadowX
                TempColor = DIBRGB(lngBackColor)
                If ChangedImage(x, y) <> TempColor And ChangedImage(x, y) <> TempColor1 Then
                    TempColor1 = DIBRGB(lngShadowColor)
                    If ChangedImage(TempX, TempY) = TempColor Then
                        ChangedImage(TempX, TempY) = TempColor1
                        MaskImage(TempX, TempY) = vbBlack
                    End If
                End If
            End If
            
            'Selection Color(needs more work)
            If lngSelectionWidth <> 0 And lngSelectionColor <> lngBackColor Then
                TempColor = DIBRGB(lngBackColor)
                TempColor1 = DIBRGB(lngSelectionColor)
                If ChangedImage(x, y) <> TempColor Then
                
                    ' check left
                    If ChangedImage(x - 1, y) = TempColor And ChangedImage(x, y) <> TempColor1 Then
                        For i = 1 To lngSelectionWidth
                            ChangedImage(x - i, y) = TempColor1
                            MaskImage(x - i, y) = vbBlack
                        Next i
                    End If
                    ' check top
                    If ChangedImage(x, y - 1) = TempColor And ChangedImage(x, y) <> TempColor1 Then
                        For i = 1 To lngSelectionWidth
                            ChangedImage(x, y - i) = TempColor1
                            MaskImage(x, y - i) = vbBlack
                        Next i
                    End If
                    ' check right
                    If ChangedImage(x + 1, y) = TempColor And ChangedImage(x, y) <> TempColor1 Then
                        For i = 1 To lngSelectionWidth
                            ChangedImage(x + i, y) = TempColor1
                            MaskImage(x + i, y) = vbBlack
                        Next i
                    End If
                    ' check bottom
                    If ChangedImage(x, y + 1) = TempColor And ChangedImage(x, y) <> TempColor1 Then
                        For i = 1 To lngSelectionWidth
                            ChangedImage(x, y + i) = TempColor1
                            MaskImage(x, y + i) = vbBlack
                        Next i
                    End If
                End If
            End If
            
            'BlackAndWhite
            If blnBlackAndWhite = True Then
                ChangedImage(x, y) = GrayScale(ChangedImage(x, y))
            End If
            
            'Mask Image
            If blnTransparent = True Then
                TempColor = DIBRGB(lngTransparentColor)
                
                If ChangedImage(x, y) = IIf(blnBlackAndWhite = True, GrayScale(TempColor), TempColor) Then
                    MaskImage(x, y) = vbWhite
                Else
                    MaskImage(x, y) = vbBlack
                End If
            End If
        Next x
    Next y
End Sub

Public Sub DrawImage(Picture As Object, Optional x As Long = 0, Optional y As Long = 0, Optional Width As Long = 0, Optional Height As Long = 0)
    Dim lngTileX As Long
    Dim lngTileY As Long
    Dim lngTileLimitX As Long
    Dim lngTileLimitY As Long
    
    Call ChangeImage
    If Width = 0 Then Width = outWidth
    If Height = 0 Then Height = outHeight
        
    With bi32BitInfo.bmiHeader
        .biBitCount = 32
        .biPlanes = 1
        .biSize = Len(bi32BitInfo.bmiHeader)
        .biWidth = outWidth
        .biHeight = outHeight
        .biSizeImage = 4 * outWidth * outHeight
    End With

      
    'draw image transparent or not
    'draw image mirrored or not
    If blnTiles = True Then
        lngTileLimitX = Picture.ScaleWidth
        lngTileLimitY = Picture.ScaleHeight
    Else
        lngTileLimitX = 1
        lngTileLimitY = 1
    End If
    For lngTileY = 0 To lngTileLimitY Step outHeight
        For lngTileX = 0 To lngTileLimitX Step outWidth
            If msMirrorState = None Then
                StretchDIBits Picture.hdc, lngTileX, lngTileY, Width, Height, 0, 0, _
                    outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, IIf(blnTransparent = False, vbSrcCopy, vbSrcInvert)
                If blnTransparent = True Then
                    StretchDIBits Picture.hdc, lngTileX, lngTileY, Width, Height, 0, 0, _
                        outWidth, outHeight, MaskImage(0, 0), bi32BitInfo, 0, vbSrcAnd
                    StretchDIBits Picture.hdc, lngTileX, lngTileY, Width, Height, 0, 0, _
                        outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, vbSrcInvert
                End If
            ElseIf msMirrorState = Horizontal Then
                StretchDIBits Picture.hdc, Width + lngTileX, lngTileY, -Width, Height, 0, 0, _
                    outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, IIf(blnTransparent = False, vbSrcCopy, vbSrcInvert)
                If blnTransparent = True Then
                    StretchDIBits Picture.hdc, Width + lngTileX, lngTileY, -Width, Height, 0, 0, _
                        outWidth, outHeight, MaskImage(0, 0), bi32BitInfo, 0, vbSrcAnd
                    StretchDIBits Picture.hdc, Width + lngTileX, lngTileY, -Width, Height, 0, 0, _
                        outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, vbSrcInvert
                End If
            ElseIf msMirrorState = Vertical Then
                StretchDIBits Picture.hdc, lngTileX, Height + lngTileY, Width, -Height, 0, 0, _
                    outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, IIf(blnTransparent = False, vbSrcCopy, vbSrcInvert)
                If blnTransparent = True Then
                    StretchDIBits Picture.hdc, lngTileX, Height + lngTileY, Width, -Height, 0, 0, _
                        outWidth, outHeight, MaskImage(0, 0), bi32BitInfo, 0, vbSrcAnd
                    StretchDIBits Picture.hdc, lngTileX, Height + lngTileY, Width, -Height, 0, 0, _
                        outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, vbSrcInvert
                End If
            ElseIf msMirrorState = HorizontalVertical Then
                StretchDIBits Picture.hdc, Width + lngTileX, Height + lngTileY, -Width, -Height, 0, 0, _
                    outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, IIf(blnTransparent = False, vbSrcCopy, vbSrcInvert)
                If blnTransparent = True Then
                    StretchDIBits Picture.hdc, Width + lngTileX, Height + lngTileY, -Width, -Height, 0, 0, _
                        outWidth, outHeight, MaskImage(0, 0), bi32BitInfo, 0, vbSrcAnd
                    StretchDIBits Picture.hdc, Width + lngTileX, Height + lngTileY, -Width, -Height, 0, 0, _
                        outWidth, outHeight, ChangedImage(0, 0), bi32BitInfo, 0, vbSrcInvert
                End If
            End If
        Next lngTileX
    Next lngTileY
End Sub

Public Property Get Rotate() As Single
    Rotate = sngRotate
End Property

Public Property Let Rotate(ByVal vNewValue As Single)
    If vNewValue >= 360 Then
        Do
            If vNewValue - 360 >= 0 Then
                vNewValue = vNewValue - 360
            Else
                Exit Do
            End If
        Loop
    ElseIf vNewValue <= -360 Then
        Do
            If vNewValue + 360 <= 0 Then
                vNewValue = vNewValue + 360
            Else
                Exit Do
            End If
        Loop
    End If
    sngRotate = vNewValue
End Property

Public Property Get Transparent() As Boolean
    Transparent = blnTransparent
End Property

Public Property Let Transparent(ByVal vNewValue As Boolean)
    blnTransparent = vNewValue
End Property

Public Property Get Mirror() As MirrorState
    Mirror = msMirrorState
End Property

Public Property Let Mirror(ByVal vNewValue As MirrorState)
    msMirrorState = vNewValue
End Property

Public Property Get BlackAndWhite() As Boolean
    BlackAndWhite = blnBlackAndWhite
End Property

Public Property Let BlackAndWhite(ByVal vNewValue As Boolean)
    blnBlackAndWhite = vNewValue
End Property

Public Property Get BackColor() As Long
    BackColor = lngBackColor
End Property

Public Property Let BackColor(ByVal vNewValue As Long)
    lngBackColor = vNewValue
    If lngBackColor = -1 Then lngBackColor = lngOldBackColor
    TransparentColor = lngBackColor
End Property

Public Property Get OldColor() As Long
   OldColor = lngOldColor
End Property

Public Property Let OldColor(ByVal vNewValue As Long)
    lngOldColor = vNewValue
End Property

Public Property Get NewColor() As Long
    NewColor = lngNewColor
End Property

Public Property Let NewColor(ByVal vNewValue As Long)
    lngNewColor = vNewValue
    If lngBackColor = lngOldColor Then lngBackColor = lngNewColor
End Property

Public Property Get TransparentColor() As Long
   TransparentColor = lngTransparentColor
End Property

Public Property Let TransparentColor(ByVal vNewValue As Long)
    lngTransparentColor = vNewValue
End Property

Public Property Get ShadowColor() As Long
    ShadowColor = lngShadowColor
End Property

Public Property Let ShadowColor(ByVal vNewValue As Long)
    lngShadowColor = vNewValue
End Property

Public Property Get ShadowX() As Long
    ShadowX = lngShadowX
End Property

Public Property Let ShadowX(ByVal vNewValue As Long)
    lngShadowX = vNewValue
End Property

Public Property Get ShadowY() As Long
    ShadowY = lngShadowX
End Property

Public Property Let ShadowY(ByVal vNewValue As Long)
    lngShadowY = vNewValue
End Property

Public Property Get SelectionColor() As Long
    SelectionColor = lngSelectionColor
End Property

Public Property Let SelectionColor(ByVal vNewValue As Long)
    lngSelectionColor = vNewValue
End Property

Public Property Get SelectionWidth() As Long
    SelectionWidth = lngSelectionWidth
End Property

Public Property Let SelectionWidth(ByVal vNewValue As Long)
    lngSelectionWidth = vNewValue
End Property

Public Property Get Stretch() As Boolean
    Stretch = blnStretch
End Property

Public Property Let Stretch(ByVal vNewValue As Boolean)
    blnStretch = vNewValue
    If blnStretch = True Then blnTiles = False
End Property

Public Property Get Tiles() As Boolean
    Tiles = blnTiles
End Property

Public Property Let Tiles(ByVal vNewValue As Boolean)
    blnTiles = vNewValue
    If blnTiles = True Then blnStretch = False
End Property

Public Property Get Opacy() As Long
    Opacy = lngOpacy
End Property

Public Property Let Opacy(ByVal vNewValue As Long)
    lngOpacy = vNewValue
End Property

Public Property Get Width() As Long
    Width = lngWidth
End Property

Public Property Get Height() As Long
    Height = lngHeight
End Property
