Solved

GDI+ Gurus needed: How to tile part of one image inside another. Skinning app.

Posted on 2004-10-26
229 Views
Last Modified: 2010-04-23
I want to skin my own buttons and control backgrounds in the Paint Event. To do that, a picture needs to be divided into a 3 x 3 grid, like a tic-tac-toe grid. This allows the picture to be resized with no weirdness at the corners and edges. To maximize flexibility for the skin artist, the edges and middle should be allowed to stretch or tile when the background is resized. The corners stay as they are.

The test app is downloadable at www.Omnibrowser.com/SkinningApplication.zip .  Check it out...click the button in the center of the form to toggle between stretching and tiling the edges. If we can get this working it's kind of cool. The code that stretches the edges and middle works just fine. But the code that TILES the edges and middle doesn't work. It should work, but it doesn't.

Here's an example...PaintImage is the source bitmap, sknButton is the destination. The variable iRightMargin, iTopMargin, etc., are concerned with how much of a margin exists around the skinned image.
------------------------------
        ':::::RIGHT MIDDLE:::::
        Dim MiddleRightSource As New RectangleF(PaintImage.Width - iRightMargin, iTopMargin, iRightMargin, PaintImage.Height - iBottomMargin - iTopMargin)
        Dim MiddleRightDestination As New RectangleF(sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin)

        If bRightTile = True Then
            G.FillRegion(New TextureBrush(PaintImage, MiddleRightSource), New Region(MiddleRightDestination)) 'Tile
        Else
            G.DrawImage(PaintImage, MiddleRightDestination, MiddleRightSource, GraphicsUnit.Pixel) 'Stretch
        End If
----------------------------
It's like the Fill Region command decides to start the fill from the edge of sknButton, rather than from the edge of the region described. FillRectangle has the same problem. Creating an ImageAttributes and setting the Wrapmode to Tile doesn't tile.

Would love to get this working and it's driving me nuts and holding up my project. I'll put a page up on my site with a sample app on how to do this credit those who contribute. Thanks for any help.

Roger
0
Question by:rdavis101
    10 Comments
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Hi Rdavis,

    Try this

    Option Explicit On
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Color

    Public Class cSkinObject

        Dim WithEvents sknButton As Button

        Dim iLeftMargin As Integer
        Dim iRightMargin As Integer
        Dim iTopMargin As Integer
        Dim iBottomMargin As Integer

        Dim imgNormal As Image
        Dim imgMouseover As Image
        Dim imgPressed As Image

        Dim bInside As Boolean

        Dim bLeftTile As Boolean
        Dim bRightTile As Boolean
        Dim bTopTile As Boolean
        Dim bBottomTile As Boolean
        Dim bMiddleTile As Boolean
        Dim iar As New ImageAttributes


        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property NormalImage() As Image
            Get
                NormalImage = imgNormal
            End Get
            Set(ByVal Value As Image)
                imgNormal = Value
            End Set
        End Property

        Public Property MouseoverImage() As Image
            Get
                MouseoverImage = imgMouseover
            End Get
            Set(ByVal Value As Image)
                imgMouseover = Value
            End Set
        End Property

        Public Property PressedImage() As Image
            Get
                PressedImage = imgPressed
            End Get
            Set(ByVal Value As Image)
                imgPressed = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property SkinButton() As Button
            Get
                SkinButton = sknButton
            End Get
            Set(ByVal Value As Button)
                sknButton = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftMargin() As Integer
            Get
                LeftMargin = iLeftMargin
            End Get
            Set(ByVal Value As Integer)
                iLeftMargin = Value
            End Set
        End Property

        Public Property RightMargin() As Integer
            Get
                RightMargin = iRightMargin
            End Get
            Set(ByVal Value As Integer)
                iRightMargin = Value
            End Set
        End Property

        Public Property TopMargin() As Integer
            Get
                TopMargin = iTopMargin
            End Get
            Set(ByVal Value As Integer)
                iTopMargin = Value
            End Set
        End Property

        Public Property BottomMargin() As Integer
            Get
                TopMargin = iBottomMargin
            End Get
            Set(ByVal Value As Integer)
                iBottomMargin = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftTile() As Boolean
            Get
                LeftTile = bLeftTile
            End Get
            Set(ByVal Value As Boolean)
                bLeftTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property RightTile() As Boolean
            Get
                RightTile = bRightTile
            End Get
            Set(ByVal Value As Boolean)
                bRightTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property TopTile() As Boolean
            Get
                TopTile = bTopTile
            End Get
            Set(ByVal Value As Boolean)
                bTopTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property BottomTile() As Boolean
            Get
                BottomTile = bBottomTile
            End Get
            Set(ByVal Value As Boolean)
                bBottomTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property MiddleTile() As Boolean
            Get
                MiddleTile = bMiddleTile
            End Get
            Set(ByVal Value As Boolean)
                bMiddleTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Private Sub sknButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles sknButton.Paint
            Call SkinMe(e.Graphics)

        End Sub

        Private Sub SkinMe(ByVal G As Graphics)
            If imgNormal Is Nothing Then Exit Sub 'If the user hasn't assigned an skinning image yet, just exit

            Dim PaintImage As Image
            If bInside = True And Not imgMouseover Is Nothing Then 'The the user isn't using a Mouseover image.
                PaintImage = imgMouseover
            Else
                PaintImage = imgNormal
            End If
            If bInside = False Then PaintImage = imgNormal

            ': UPPER(Left)
            Dim UpperLeftSource As New Rectangle(0, 0, iLeftMargin, iTopMargin)
            Dim UpperLeftDestination As New Rectangle(0, 0, iLeftMargin, iTopMargin)

            G.FillRegion(New TextureBrush(PaintImage, UpperLeftSource), New Region(UpperLeftDestination))

            ': Left(MIDDLE)
            Dim MiddleLeftSource As New Rectangle(0, iTopMargin, iLeftMargin, PaintImage.Height - iBottomMargin - iTopMargin)
            Dim MiddleLeftDestination As New Rectangle(0, iTopMargin, iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)


            If bLeftTile = True Then
                G.DrawImage(PaintImage, MiddleLeftDestination, 0, iTopMargin, iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin, GraphicsUnit.Pixel, iar)
            Else
                G.DrawImage(PaintImage, MiddleLeftDestination, MiddleLeftSource, GraphicsUnit.Pixel)
            End If

            ':::::MIDDLE MIDDLE:::::
            Dim MiddleMiddleSource As New Rectangle(iLeftMargin, iTopMargin, PaintImage.Width - iRightMargin - iLeftMargin, PaintImage.Height - iBottomMargin - iTopMargin)
            Dim MiddleMiddleDestination As New Rectangle(iLeftMargin, iTopMargin, sknButton.Width - iRightMargin - iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bMiddleTile = True Then
                G.DrawImage(PaintImage, MiddleMiddleDestination, iLeftMargin, iTopMargin, sknButton.Width - iRightMargin - iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin, GraphicsUnit.Pixel, iar)
            Else
                G.DrawImage(PaintImage, MiddleMiddleDestination, MiddleMiddleSource, GraphicsUnit.Pixel)
            End If

            ': Right(MIDDLE)
            Dim MiddleRightSource As New Rectangle(PaintImage.Width - iRightMargin, iTopMargin, iRightMargin, PaintImage.Height - iBottomMargin - iTopMargin)
            Dim MiddleRightDestination As New Rectangle(sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bRightTile = True Then
                G.DrawImage(PaintImage, MiddleRightDestination, sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin, GraphicsUnit.Pixel, iar)
                '            G.FillRegion(New TextureBrush(PaintImage, MiddleRightSource), New Region(MiddleRightDestination))
            Else
                G.DrawImage(PaintImage, MiddleRightDestination, MiddleRightSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER LEFT:::::
            Dim LowerLeftSource As New Rectangle(0, PaintImage.Height - iBottomMargin, iLeftMargin, iBottomMargin)
            Dim LowerLeftDestination As New Rectangle(0, sknButton.Height - iBottomMargin, iLeftMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerLeftDestination, LowerLeftSource, GraphicsUnit.Pixel)

            ':::::: LOWER(MIDDLE)
            Dim LowerMiddleSource As New Rectangle(iLeftMargin, PaintImage.Height - iBottomMargin, PaintImage.Width - iRightMargin - iLeftMargin, iBottomMargin)
            Dim LowerMiddleDestination As New Rectangle(iLeftMargin, sknButton.Height - iBottomMargin, sknButton.Width - iRightMargin - iLeftMargin, iBottomMargin)

            If bBottomTile = True Then
                G.DrawImage(PaintImage, LowerMiddleDestination, iLeftMargin, sknButton.Height - iBottomMargin, sknButton.Width - iRightMargin - iLeftMargin, iBottomMargin, GraphicsUnit.Pixel, iar)
            Else
                G.DrawImage(PaintImage, LowerMiddleDestination, LowerMiddleSource, GraphicsUnit.Pixel)
            End If


            ':::::UPPER MIDDLE:::::
            Dim UpperMiddleSource As New Rectangle(iLeftMargin, 0, PaintImage.Width - iRightMargin - iLeftMargin, iTopMargin)
            Dim UpperMiddleDestination As New Rectangle(iLeftMargin, 0, sknButton.Width - iRightMargin - iLeftMargin, iTopMargin)

            If bTopTile = True Then
                G.DrawImage(PaintImage, UpperMiddleDestination, iLeftMargin, 0, sknButton.Width - iRightMargin - iLeftMargin, iTopMargin, GraphicsUnit.Pixel, iar)
            Else
                G.DrawImage(PaintImage, UpperMiddleDestination, UpperMiddleSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER RIGHT:::::
            Dim LowerRightSource As New Rectangle(PaintImage.Width - iRightMargin, PaintImage.Height - iBottomMargin, iRightMargin, iBottomMargin)
            Dim LowerRightDestination As New Rectangle(sknButton.Width - iRightMargin, sknButton.Height - iBottomMargin, iRightMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerRightDestination, LowerRightSource, GraphicsUnit.Pixel)

            ':::::UPPER RIGHT
            Dim UpperRightSource As New Rectangle(PaintImage.Width - iRightMargin, 0, iRightMargin, iTopMargin)
            Dim UpperRightDestination As New Rectangle(sknButton.Width - iRightMargin, 0, iRightMargin, iTopMargin)

            G.DrawImage(PaintImage, UpperRightDestination, UpperRightSource, GraphicsUnit.Pixel)


        End Sub

        Public Sub New()
            bInside = False
            iar.SetWrapMode(Drawing2D.WrapMode.Tile)
        End Sub

        Private Sub sknButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseLeave
            bInside = False
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub

        Private Sub sknButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseEnter
            bInside = True
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub
    End Class





    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Hmmm, I just realised maybe this wasn't what you meant
    0
     

    Author Comment

    by:rdavis101
    Hi Ron,

    Thanks for at least having a look...it's a tough problem for me to explain, and that's why I made the project downloadable. If a solution can be worked out, though, it should be a nice addition to the arsenal of every .Net programmer. In the code posted, the original image tiles in the body of the painted control. The problem is that sections of the original image need to be tiled inside sections of the painted control (maybe that frames the problem better??). If you have any further advice, I'm all ears. I don't have a clue how to solve this, or even if its solvable.

    Roger
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Hi Roger,

    I'll have another look.

    Just considering the top row (upperright, middleRight,upperleft) what do you want to see there ?

    The 3 "circles" repeated,
    0
     

    Author Comment

    by:rdavis101
    Hi Ron,

    Thanks for taking a look. Each of the 9 sections has to either stretch or tile independently. That's the key.

    Roger
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Ok, I'll be back, hopefully with a solution ;-)
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Hi rdavis101,

    See if this works for you, you need to clean up the code a bit (used some generic variables names (dddd))

    Option Explicit On
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Color

    Public Class cSkinObject

        Dim WithEvents sknButton As Button
        Dim ia1 As New ImageAttributes
        Dim iLeftMargin As Integer
        Dim iRightMargin As Integer
        Dim iTopMargin As Integer
        Dim iBottomMargin As Integer

        Dim imgNormal As Image
        Dim imgMouseover As Image
        Dim imgPressed As Image

        Dim bInside As Boolean

        Dim bLeftTile As Boolean
        Dim bRightTile As Boolean
        Dim bTopTile As Boolean
        Dim bBottomTile As Boolean
        Dim bMiddleTile As Boolean

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property NormalImage() As Image
            Get
                NormalImage = imgNormal
            End Get
            Set(ByVal Value As Image)
                imgNormal = Value
            End Set
        End Property

        Public Property MouseoverImage() As Image
            Get
                MouseoverImage = imgMouseover
            End Get
            Set(ByVal Value As Image)
                imgMouseover = Value
            End Set
        End Property

        Public Property PressedImage() As Image
            Get
                PressedImage = imgPressed
            End Get
            Set(ByVal Value As Image)
                imgPressed = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property SkinButton() As Button
            Get
                SkinButton = sknButton
            End Get
            Set(ByVal Value As Button)
                sknButton = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftMargin() As Integer
            Get
                LeftMargin = iLeftMargin
            End Get
            Set(ByVal Value As Integer)
                iLeftMargin = Value
            End Set
        End Property

        Public Property RightMargin() As Integer
            Get
                RightMargin = iRightMargin
            End Get
            Set(ByVal Value As Integer)
                iRightMargin = Value
            End Set
        End Property

        Public Property TopMargin() As Integer
            Get
                TopMargin = iTopMargin
            End Get
            Set(ByVal Value As Integer)
                iTopMargin = Value
            End Set
        End Property

        Public Property BottomMargin() As Integer
            Get
                TopMargin = iBottomMargin
            End Get
            Set(ByVal Value As Integer)
                iBottomMargin = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftTile() As Boolean
            Get
                LeftTile = bLeftTile
            End Get
            Set(ByVal Value As Boolean)
                bLeftTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property RightTile() As Boolean
            Get
                RightTile = bRightTile
            End Get
            Set(ByVal Value As Boolean)
                bRightTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property TopTile() As Boolean
            Get
                TopTile = bTopTile
            End Get
            Set(ByVal Value As Boolean)
                bTopTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property BottomTile() As Boolean
            Get
                BottomTile = bBottomTile
            End Get
            Set(ByVal Value As Boolean)
                bBottomTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property MiddleTile() As Boolean
            Get
                MiddleTile = bMiddleTile
            End Get
            Set(ByVal Value As Boolean)
                bMiddleTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Private Sub sknButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles sknButton.Paint
            ia1.SetWrapMode(Drawing2D.WrapMode.Tile)
            Call SkinMe(e.Graphics)

        End Sub

        Private Sub SkinMe(ByVal G As Graphics)
            If imgNormal Is Nothing Then Exit Sub 'If the user hasn't assigned an skinning image yet, just exit

            Dim PaintImage As Image
            If bInside = True And Not imgMouseover Is Nothing Then 'The the user isn't using a Mouseover image.
                PaintImage = imgMouseover
            Else
                PaintImage = imgNormal
            End If
            If bInside = False Then PaintImage = imgNormal

            ':::::UPPER LEFT:::::
            Dim UpperLeftSource As New Rectangle(0, 0, iLeftMargin, iTopMargin)
            Dim UpperLeftDestination As New Rectangle(0, 0, iLeftMargin, iTopMargin)

            G.FillRegion(New TextureBrush(PaintImage, UpperLeftSource), New Region(UpperLeftDestination))

            ':::::LEFT MIDDLE:::::
            Dim MiddleLeftSource As New Rectangle(0, iTopMargin, iLeftMargin, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleLeftDestination As New Rectangle(0, iTopMargin, iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bLeftTile = True Then
                Dim dd As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Height - iBottomMargin - iTopMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, 0, 25 + CInt((xx * (PaintImage.Height - iBottomMargin - iTopMargin + 10))), MiddleLeftSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, MiddleLeftDestination, MiddleLeftSource, GraphicsUnit.Pixel)
            End If

            ':::::MIDDLE MIDDLE:::::
            Dim MiddleMiddleSource As New Rectangle(iLeftMargin, iTopMargin, PaintImage.Width - iRightMargin - iLeftMargin + 10, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleMiddleDestination As New Rectangle(iLeftMargin, iTopMargin, sknButton.Width - iRightMargin - iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bMiddleTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))
                Dim dddD As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddddD As Integer = CInt(dddD / (PaintImage.Height - iBottomMargin - iTopMargin))

                For yy As Integer = 0 To ddddD
                    For xx As Integer = 0 To ddd
                        G.DrawImage(PaintImage, 15 + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), 25 + (yy * (PaintImage.Height - iBottomMargin - iTopMargin + 10)), MiddleMiddleSource, GraphicsUnit.Pixel)
                    Next
                Next
            Else
                    G.DrawImage(PaintImage, MiddleMiddleDestination, MiddleMiddleSource, GraphicsUnit.Pixel)
            End If

            ':::::RIGHT MIDDLE:::::
            Dim MiddleRightSource As New RectangleF(PaintImage.Width - iRightMargin, iTopMargin, iRightMargin, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleRightDestination As New RectangleF(sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bRightTile = True Then
                Dim dd As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Height - iBottomMargin - iTopMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, sknButton.Width - iRightMargin, 25 + CInt((xx * (PaintImage.Height - iBottomMargin - iTopMargin + 10))), MiddleRightSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, MiddleRightDestination, MiddleRightSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER LEFT:::::
            Dim LowerLeftSource As New Rectangle(0, PaintImage.Height - iBottomMargin, iLeftMargin, iBottomMargin)
            Dim LowerLeftDestination As New Rectangle(0, sknButton.Height - iBottomMargin, iLeftMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerLeftDestination, LowerLeftSource, GraphicsUnit.Pixel)

            ':::::: LOWER(MIDDLE)
            Dim LowerMiddleSource As New Rectangle(iLeftMargin, PaintImage.Height - iBottomMargin, PaintImage.Width - iRightMargin - iLeftMargin + 10, iBottomMargin)
            Dim LowerMiddleDestination As New Rectangle(iLeftMargin, sknButton.Height - iBottomMargin, sknButton.Width - iRightMargin - iLeftMargin, iBottomMargin)

            If bBottomTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, 15 + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), sknButton.Height - iBottomMargin, LowerMiddleSource, GraphicsUnit.Pixel)
                Next

            Else
                G.DrawImage(PaintImage, LowerMiddleDestination, LowerMiddleSource, GraphicsUnit.Pixel)
            End If


            ':::::UPPER MIDDLE:::::
            Dim UpperMiddleSource As New RectangleF(15, 0, PaintImage.Width - iRightMargin - iLeftMargin + 10, iTopMargin)
            Dim UpperMiddleDestination As New RectangleF(15, 0, sknButton.Width - iRightMargin - iLeftMargin, iTopMargin)
            Dim t As New Region(UpperMiddleDestination)
            If bTopTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, 15 + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), 0, UpperMiddleSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, UpperMiddleDestination, UpperMiddleSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER RIGHT:::::
            Dim LowerRightSource As New Rectangle(PaintImage.Width - iRightMargin, PaintImage.Height - iBottomMargin, iRightMargin, iBottomMargin)
            Dim LowerRightDestination As New Rectangle(sknButton.Width - iRightMargin, sknButton.Height - iBottomMargin, iRightMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerRightDestination, LowerRightSource, GraphicsUnit.Pixel)

            ': UPPER(Right)
            Dim UpperRightSource As New Rectangle(PaintImage.Width - iRightMargin, 0, iRightMargin, iTopMargin)
            Dim UpperRightDestination As New Rectangle(sknButton.Width - iRightMargin, 0, iRightMargin, iTopMargin)

            G.DrawImage(PaintImage, UpperRightDestination, UpperRightSource, GraphicsUnit.Pixel)


        End Sub

        Public Sub New()
            bInside = False
        End Sub

        Private Sub sknButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseLeave
            bInside = False
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub

        Private Sub sknButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseEnter
            bInside = True
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub
    End Class
    0
     
    LVL 25

    Accepted Solution

    by:
    I saw I left some hard coded numbers in there (for testing). This is without them

    Option Explicit On
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Color

    Public Class cSkinObject

        Dim WithEvents sknButton As Button
        Dim ia1 As New ImageAttributes
        Dim iLeftMargin As Integer
        Dim iRightMargin As Integer
        Dim iTopMargin As Integer
        Dim iBottomMargin As Integer

        Dim imgNormal As Image
        Dim imgMouseover As Image
        Dim imgPressed As Image

        Dim bInside As Boolean

        Dim bLeftTile As Boolean
        Dim bRightTile As Boolean
        Dim bTopTile As Boolean
        Dim bBottomTile As Boolean
        Dim bMiddleTile As Boolean

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property NormalImage() As Image
            Get
                NormalImage = imgNormal
            End Get
            Set(ByVal Value As Image)
                imgNormal = Value
            End Set
        End Property

        Public Property MouseoverImage() As Image
            Get
                MouseoverImage = imgMouseover
            End Get
            Set(ByVal Value As Image)
                imgMouseover = Value
            End Set
        End Property

        Public Property PressedImage() As Image
            Get
                PressedImage = imgPressed
            End Get
            Set(ByVal Value As Image)
                imgPressed = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property SkinButton() As Button
            Get
                SkinButton = sknButton
            End Get
            Set(ByVal Value As Button)
                sknButton = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftMargin() As Integer
            Get
                LeftMargin = iLeftMargin
            End Get
            Set(ByVal Value As Integer)
                iLeftMargin = Value
            End Set
        End Property

        Public Property RightMargin() As Integer
            Get
                RightMargin = iRightMargin
            End Get
            Set(ByVal Value As Integer)
                iRightMargin = Value
            End Set
        End Property

        Public Property TopMargin() As Integer
            Get
                TopMargin = iTopMargin
            End Get
            Set(ByVal Value As Integer)
                iTopMargin = Value
            End Set
        End Property

        Public Property BottomMargin() As Integer
            Get
                TopMargin = iBottomMargin
            End Get
            Set(ByVal Value As Integer)
                iBottomMargin = Value
            End Set
        End Property

        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        Public Property LeftTile() As Boolean
            Get
                LeftTile = bLeftTile
            End Get
            Set(ByVal Value As Boolean)
                bLeftTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property RightTile() As Boolean
            Get
                RightTile = bRightTile
            End Get
            Set(ByVal Value As Boolean)
                bRightTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property TopTile() As Boolean
            Get
                TopTile = bTopTile
            End Get
            Set(ByVal Value As Boolean)
                bTopTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property BottomTile() As Boolean
            Get
                BottomTile = bBottomTile
            End Get
            Set(ByVal Value As Boolean)
                bBottomTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Public Property MiddleTile() As Boolean
            Get
                MiddleTile = bMiddleTile
            End Get
            Set(ByVal Value As Boolean)
                bMiddleTile = Value
                sknButton.Invalidate()
            End Set
        End Property

        Private Sub sknButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles sknButton.Paint
            ia1.SetWrapMode(Drawing2D.WrapMode.Tile)
            Call SkinMe(e.Graphics)

        End Sub

        Private Sub SkinMe(ByVal G As Graphics)
            If imgNormal Is Nothing Then Exit Sub 'If the user hasn't assigned an skinning image yet, just exit

            Dim PaintImage As Image
            If bInside = True And Not imgMouseover Is Nothing Then 'The the user isn't using a Mouseover image.
                PaintImage = imgMouseover
            Else
                PaintImage = imgNormal
            End If
            If bInside = False Then PaintImage = imgNormal

            ':::::UPPER LEFT:::::
            Dim UpperLeftSource As New Rectangle(0, 0, iLeftMargin, iTopMargin)
            Dim UpperLeftDestination As New Rectangle(0, 0, iLeftMargin, iTopMargin)

            G.FillRegion(New TextureBrush(PaintImage, UpperLeftSource), New Region(UpperLeftDestination))

            ':::::LEFT MIDDLE:::::
            Dim MiddleLeftSource As New Rectangle(0, iTopMargin, iLeftMargin, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleLeftDestination As New Rectangle(0, iTopMargin, iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bLeftTile = True Then
                Dim dd As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Height - iBottomMargin - iTopMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, 0, iTopMargin + CInt((xx * (PaintImage.Height - iBottomMargin - iTopMargin + 10))), MiddleLeftSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, MiddleLeftDestination, MiddleLeftSource, GraphicsUnit.Pixel)
            End If

            ':::::MIDDLE MIDDLE:::::
            Dim MiddleMiddleSource As New Rectangle(iLeftMargin, iTopMargin, PaintImage.Width - iRightMargin - iLeftMargin + 10, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleMiddleDestination As New Rectangle(iLeftMargin, iTopMargin, sknButton.Width - iRightMargin - iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bMiddleTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))
                Dim dddD As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddddD As Integer = CInt(dddD / (PaintImage.Height - iBottomMargin - iTopMargin))

                For yy As Integer = 0 To ddddD
                    For xx As Integer = 0 To ddd
                        G.DrawImage(PaintImage, iLeftMargin + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), 25 + (yy * (PaintImage.Height - iBottomMargin - iTopMargin + 10)), MiddleMiddleSource, GraphicsUnit.Pixel)
                    Next
                Next
            Else
                    G.DrawImage(PaintImage, MiddleMiddleDestination, MiddleMiddleSource, GraphicsUnit.Pixel)
            End If

            ':::::RIGHT MIDDLE:::::
            Dim MiddleRightSource As New RectangleF(PaintImage.Width - iRightMargin, iTopMargin, iRightMargin, PaintImage.Height - iBottomMargin - iTopMargin + 10)
            Dim MiddleRightDestination As New RectangleF(sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin)

            If bRightTile = True Then
                Dim dd As Integer = sknButton.Height - iBottomMargin - iTopMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Height - iBottomMargin - iTopMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, sknButton.Width - iRightMargin, iTopMargin + CInt((xx * (PaintImage.Height - iBottomMargin - iTopMargin + 10))), MiddleRightSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, MiddleRightDestination, MiddleRightSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER LEFT:::::
            Dim LowerLeftSource As New Rectangle(0, PaintImage.Height - iBottomMargin, iLeftMargin, iBottomMargin)
            Dim LowerLeftDestination As New Rectangle(0, sknButton.Height - iBottomMargin, iLeftMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerLeftDestination, LowerLeftSource, GraphicsUnit.Pixel)

            ':::::: LOWER(MIDDLE)
            Dim LowerMiddleSource As New Rectangle(iLeftMargin, PaintImage.Height - iBottomMargin, PaintImage.Width - iRightMargin - iLeftMargin + 10, iBottomMargin)
            Dim LowerMiddleDestination As New Rectangle(iLeftMargin, sknButton.Height - iBottomMargin, sknButton.Width - iRightMargin - iLeftMargin, iBottomMargin)

            If bBottomTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, iLeftMargin + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), sknButton.Height - iBottomMargin, LowerMiddleSource, GraphicsUnit.Pixel)
                Next

            Else
                G.DrawImage(PaintImage, LowerMiddleDestination, LowerMiddleSource, GraphicsUnit.Pixel)
            End If


            ':::::UPPER MIDDLE:::::
            Dim UpperMiddleSource As New RectangleF(iLeftMargin, 0, PaintImage.Width - iRightMargin - iLeftMargin + 10, iTopMargin)
            Dim UpperMiddleDestination As New RectangleF(iLeftMargin, 0, sknButton.Width - iRightMargin - iLeftMargin, iTopMargin)
            Dim t As New Region(UpperMiddleDestination)
            If bTopTile = True Then
                Dim dd As Integer = sknButton.Width - iRightMargin - iLeftMargin
                Dim ddd As Integer = CInt(dd / (PaintImage.Width - iRightMargin - iLeftMargin + 10))

                For xx As Integer = 0 To ddd
                    G.DrawImage(PaintImage, iLeftMargin + (xx * (PaintImage.Width - iRightMargin - iLeftMargin + 10)), 0, UpperMiddleSource, GraphicsUnit.Pixel)
                Next
            Else
                G.DrawImage(PaintImage, UpperMiddleDestination, UpperMiddleSource, GraphicsUnit.Pixel)
            End If

            ':::::LOWER RIGHT:::::
            Dim LowerRightSource As New Rectangle(PaintImage.Width - iRightMargin, PaintImage.Height - iBottomMargin, iRightMargin, iBottomMargin)
            Dim LowerRightDestination As New Rectangle(sknButton.Width - iRightMargin, sknButton.Height - iBottomMargin, iRightMargin, iBottomMargin)

            G.DrawImage(PaintImage, LowerRightDestination, LowerRightSource, GraphicsUnit.Pixel)

            ': UPPER(Right)
            Dim UpperRightSource As New Rectangle(PaintImage.Width - iRightMargin, 0, iRightMargin, iTopMargin)
            Dim UpperRightDestination As New Rectangle(sknButton.Width - iRightMargin, 0, iRightMargin, iTopMargin)

            G.DrawImage(PaintImage, UpperRightDestination, UpperRightSource, GraphicsUnit.Pixel)


        End Sub

        Public Sub New()
            bInside = False
        End Sub

        Private Sub sknButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseLeave
            bInside = False
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub

        Private Sub sknButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles sknButton.MouseEnter
            bInside = True
            If Not imgMouseover Is Nothing Then sknButton.Invalidate() 'If the user is using a mouseover image, draw it.
        End Sub
    End Class
    0
     

    Author Comment

    by:rdavis101
    Hi Ron,

    That was the approach I finally settled on, too...basically just writing the code to tile in sections of the original image. Bravo! You and I came to nearly identical solutions. :)

    Roger
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    Hi Rdavies,

    Yeah, I seemed the only way possible to make it work :-)

    Good luck with your program.

    Ronald
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now