Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Blending
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

Public Class ClassBackgroundHelper
    Private _blendingComponent As XtraGridBlending
    Private _originalImage As Bitmap
    Private _grid As GridView
    Private _pictureAlignment As ContentAlignment

    Public Property Image() As Bitmap
        Get
            Return _originalImage
        End Get
        Set(value As Bitmap)
            _originalImage = value
            GenerateBackgroundImage()
        End Set
    End Property
    Public Property PictureAlignment() As ContentAlignment
        Get
            Return _pictureAlignment
        End Get
        Set(value As ContentAlignment)
            _pictureAlignment = value
            GenerateBackgroundImage()
        End Set
    End Property
    Public Sub New(grid As GridView, image As Bitmap, Optional alignment As ContentAlignment = ContentAlignment.BottomRight)
        _grid = grid
        InitializingBlendingComponent()
        _originalImage = image
        _grid.GridControl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center
        PictureAlignment = alignment

        AddHandler _grid.GridControl.Resize, AddressOf GridControl_Resize
        GenerateBackgroundImage()
    End Sub

    Private Sub GridControl_Resize(sender As Object, e As EventArgs)
        GenerateBackgroundImage()
    End Sub

    Public Shared Function SetImgageOpacity(ByVal imgPic As Image, ByVal imgOpac As Double) As Image

        Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
        Dim gfxPic As Graphics = Graphics.FromImage(bmpPic)
        Dim cmxPic As New ColorMatrix()
        Dim iaPic As New ImageAttributes()

        cmxPic.Matrix33 = imgOpac

        iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
        gfxPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic)

        gfxPic.Dispose()
        iaPic.Dispose()

        Return bmpPic

    End Function

    Private Sub GenerateBackgroundImage()
        Try
            Dim gvInfo As GridViewInfo = TryCast(_grid.GetViewInfo(), GridViewInfo)
            Dim _processedImage As New Bitmap(gvInfo.ViewRects.Client.Width + 1, gvInfo.ViewRects.Client.Height + 1)
            Using g As Graphics = Graphics.FromImage(_processedImage)
                Dim p As Point = CalculateImageLocation(_originalImage.Width, _originalImage.Height, gvInfo, _pictureAlignment)
                Dim transparentImage As Bitmap = SetImgageOpacity(_originalImage, 0.5)
                g.DrawImage(transparentImage, p)
                g.Save()
            End Using
            _grid.GridControl.BackgroundImage = _processedImage
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in GenerateBackgroundImage: " & ex.Message)
        End Try
       
    End Sub

    Private Function CalculateImageLocation(imageWidth As Integer, imageHeight As Integer, gvInfo As GridViewInfo, alignment As ContentAlignment) As Point
        Try
            Dim location As Point = Point.Empty
            Dim rect As Rectangle = gvInfo.ViewRects.Rows
            Dim indicatorWidth As Integer = gvInfo.ViewRects.IndicatorWidth
            Dim vScrollSize As Integer = If(gvInfo.VScrollBarPresence = ScrollBarPresence.Visible, 20, 0)
            Dim hSctollSize As Integer = If(gvInfo.HScrollBarPresence = ScrollBarPresence.Visible, 20, 0)

            Select Case alignment
                Case ContentAlignment.BottomCenter
                    location.Offset((rect.Width / 2) - (imageWidth / 2), rect.Bottom - imageHeight - hSctollSize)
                    Exit Select
                Case ContentAlignment.BottomLeft
                    location.Offset(indicatorWidth, rect.Bottom - imageHeight - hSctollSize)
                    Exit Select
                Case ContentAlignment.BottomRight
                    location.Offset(rect.Right - imageWidth - vScrollSize, rect.Bottom - imageHeight - hSctollSize)
                    Exit Select
                Case ContentAlignment.MiddleCenter
                    location.Offset((rect.Width / 2) - (imageWidth / 2), (rect.Height / 2) - (imageHeight / 2))
                    Exit Select
                Case ContentAlignment.MiddleLeft
                    location.Offset(indicatorWidth, (rect.Height / 2) - (imageHeight / 2))
                    Exit Select
                Case ContentAlignment.MiddleRight
                    location.Offset(rect.Width - imageWidth - vScrollSize, (rect.Height / 2) - (imageHeight / 2))
                    Exit Select
                Case ContentAlignment.TopCenter
                    location.Offset((rect.Width / 2) - (imageWidth / 2), rect.Top)
                    Exit Select
                Case ContentAlignment.TopLeft
                    location.Offset(indicatorWidth, rect.Top)
                    Exit Select
                Case ContentAlignment.TopRight
                    location.Offset(rect.Right - imageWidth - vScrollSize, rect.Top)
                    Exit Select
                Case Else
                    Exit Select
            End Select
            Return location
        Catch ex As Exception
            ClassLogger.Add("Unexpected Error in CalculateImageLocation: " & ex.Message)
            Return Nothing
        End Try
        
    End Function

    Private Sub InitializingBlendingComponent()
        _blendingComponent = New XtraGridBlending()
        _blendingComponent.GridControl = _grid.GridControl
    End Sub
End Class