Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-26
10
Medium Priority
?
234 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
Comment
Question by:rdavis101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12422492
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
ID: 12422707
Hmmm, I just realised maybe this wasn't what you meant
0
 

Author Comment

by:rdavis101
ID: 12424406
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12431390
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
ID: 12431961
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
ID: 12432035
Ok, I'll be back, hopefully with a solution ;-)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12433989
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:
RonaldBiemans earned 2000 total points
ID: 12434059
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
ID: 12434094
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
ID: 12434174
Hi Rdavies,

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

Good luck with your program.

Ronald
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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