Form Resizing Problem.

The problem is i have setup an irregular shaped form using a collection of objects for teh buttons background and labels shown in fig1. And i have a class file behind. The problem I have is when resizing the form I have a method which you select the edge of the form and drag it to the required size the class behind then resizes the image and fills in the diffence in size with the 2 filler images this works perfectley. However I need to be able to specify a size for the form to resize to without the user clicking and dragging the form this is the code I'm trying

Using Rebuildbackground(new size(900,900)) To call the resizing sub but when this is called it keeps saying object reference not set to an instance of the object. I ahve attached the code from form1 to show how im setting up the objects and the classfile to show the rebuildbackground sub please help

fORM 1 SETTING UP THE FORM IMAGES BACKGROUND AND LABELS


Protected mSkinObjects As New Collection

Private _MessageSize As Size
Private _MessagePosition As Point


Private Sub buildbackground(ByVal skinpath As String)

Me.BackgroundImageLayout = ImageLayout.None


'-- Bring Everything into a collection
Dim ds As New DataSet
ds.ReadXml(Application.StartupPath & skinpath)
Dim dt As DataTable = ds.Tables("background")
Dim r As DataRow
Dim ImageObject As SkinObject
Dim PageSize As Size

r = dt.Rows(0)

ImageObject = New SkinObject
ImageObject.ChatForm = Me
ImageObject.ObjectType = SkinObject.ObjectTypes.Background
ImageObject.SetImage(Application.StartupPath & "\skins\default\" & r("Filename"), _
New Point(0, 0), System.Drawing.ColorTranslator.FromHtml(r("TransparentColor")), _
Application.StartupPath & "\skins\default\" & r("HFiller"), _
Application.StartupPath & "\skins\default\" & r("VFiller"))
PageSize = ImageObject.PageSize
_PageSize = PageSize
ImageObject.FillerX = r("FillerX")
ImageObject.FillerY = r("FillerY")
ImageObject.DragArea = New Rectangle(r("DragAreaX"), r("DragAreaY"), r("DragAreaW"), r("DragAreaH"))

ImageObject.ResizeHandleArea = New Rectangle(r("ResizeHandleX"), r("ResizeHandleY"), r("ResizeHandleW"), r("ResizeHandleH"))
ImageObject.HSizeArea = New Rectangle(PageSize.Width - 5, 0, 5, PageSize.Height - ImageObject.ResizeHandleArea.Height)
ImageObject.VSizeArea = New Rectangle(0, PageSize.Height - 5, PageSize.Width - ImageObject.ResizeHandleArea.Width, 5)


mSkinObjects.Add(ImageObject)


dt = ds.Tables("Button")
For Each r In dt.Rows

ImageObject = New SkinObject
With ImageObject
.ObjectType = SkinObject.ObjectTypes.Image
.SetImage(Application.StartupPath & "\skins\default\" & r("Filename"), _
New Point(r("PositionX"), r("PositionY")), _
System.Drawing.ColorTranslator.FromHtml(r("TransparentColor")), "", "")
.PageSize = PageSize
.ChatForm = Me
.CommandArg = r("Command")
.XStationary = r("XStationary")
.YStationary = r("YStationary")
.name = r("name")

AddHandler ImageObject.ButtonClick, AddressOf SkinObjectClick

End With

mSkinObjects.Add(ImageObject)

Next

dt = ds.Tables("Label")
For Each r In dt.Rows

ImageObject = New SkinObject
With ImageObject
.ObjectType = SkinObject.ObjectTypes.Text
.PageSize = PageSize
.ChatForm = Me
.CommandArg = r("Command")
.XStationary = r("XStationary")
.YStationary = r("YStationary")
.CanGrowW = r("CanGrowW")
.CanGrowH = r("CanGrowH")
.TextValue = r("Value")
.TextArea = New Rectangle(r("PositionX"), r("PositionY"), r("PositionW"), r("PositionH"))
.TextColor = Drawing.ColorTranslator.FromHtml(r("FontColor"))
.TextFont = New Font(CStr(r("FontFamily")), CSng(r("FontSize")), GraphicsUnit.Point)
.TextFormat = New StringFormat


.TextFormat.FormatFlags = StringFormatFlags.NoWrap

.TextFormat.Trimming = StringTrimming.EllipsisCharacter

End With

mSkinObjects.Add(ImageObject)

Next


Me.Width = PageSize.Width
Me.Height = PageSize.Height

Dim rect As Rectangle = Screen.GetWorkingArea(Me)
Left = (rect.Width / 2) - (PageSize.Width / 2)
Top = (rect.Height / 2) - (PageSize.Height / 2)

Me.Visible = True
End Sub


' the skinclass

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Public Class SkinObject

Public Event ButtonClick(ByVal Command As String, ByVal oSkinObject As SkinObject)

Public Event mouseover(ByVal Command As String, ByVal oSkinObject As SkinObject)
Public Event EnableTextBoxes()

Public Enum ObjectTypes
Image
Text
Background
FormDrag
End Enum

Public WithEvents ChatForm As Form

Public ImageObject As Bitmap

Public BackgroundImage As Bitmap

Public ImageArea As Rectangle
Public DragArea As Rectangle
Public ImageSize As Size
Public ImagePosition As Point
Public mblnMouseOver As Boolean
Public mblnMouseDown As Boolean
Public CommandArg As String

Public CurrentArea As Rectangle
Public TextArea As Rectangle
Public TextValue As String
Public TextFont As Font
Public TextColor As Color
Public TextFormat As StringFormat
Public name As String


Public ObjectType As ObjectTypes
Public PageSize As Size
Public EnableFormMove As Boolean
Public EnableHSize As Boolean
Public EnableVSize As Boolean
Public EnableHVSize As Boolean

Public HSizeArea As Rectangle
Public VSizeArea As Rectangle
Public ResizeHandleArea As Rectangle

Public StartXY As Point = New Point(0, 0)
Public LastXY As Point = New Point(0, 0)
Private StartYDifference As Integer
Private StartXDifference As Integer

Public XStationary As Boolean
Public YStationary As Boolean
Public CanGrowW As Boolean
Public CanGrowH As Boolean

Public ImageTransparentColor As Color
Public HFiller As Bitmap
Public VFiller As Bitmap
Public FillerX As Integer
Public FillerY As Integer

Public ImageOverRect As Rectangle
Public ImageDownRect As Rectangle
Public ImageNormalRect As Rectangle

Public Sub SetImage(ByVal strFileName As String, ByVal pPosition As Point, _
ByVal oTransparency As Color, ByVal strHFiller As String, ByVal strVFiller As String)

ImageObject = New Bitmap(strFileName)

LastXY = New Point(-1, -1)

If ObjectType = ObjectTypes.Image Then
ImageObject.MakeTransparent(oTransparency)
ImageSize = New Size(CType(ImageObject.Width / 3, Integer), ImageObject.Height)
ImagePosition = pPosition
ImageArea = New Rectangle(ImagePosition.X, ImagePosition.Y, ImageSize.Width, ImageSize.Height)
ImageNormalRect = New Rectangle(New Point(0, 0), ImageSize)
ImageOverRect = New Rectangle(New Point(ImageSize.Width, 0), ImageSize)
ImageDownRect = New Rectangle(New Point(ImageSize.Width * 2, 0), ImageSize)
ElseIf ObjectType = ObjectTypes.Background Then
ImageTransparentColor = oTransparency
PageSize = New Size(ImageObject.Width, ImageObject.Height)
BackgroundImage = ImageObject.Clone
ChatForm.Region = BitmapToRegion(ImageObject, ImageTransparentColor)
ImageSize = New Size(ImageObject.Width, ImageObject.Height)
ImagePosition = pPosition
ImageArea = New Rectangle(ImagePosition.X, ImagePosition.Y, ImageSize.Width, ImageSize.Height)
HFiller = New Bitmap(strHFiller)
VFiller = New Bitmap(strVFiller)
End If

End Sub

Public Sub Invalidate()

Dim rRect As Rectangle = ImageArea

If Not XStationary Then
rRect.Offset(GetXDiff, 0)
End If
If Not YStationary Then
rRect.Offset(0, GetYDiff)
End If

ChatForm.Invalidate(rRect)

End Sub

Private Sub FormOnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ChatForm.MouseDown

If ObjectType = ObjectTypes.Image And CommandArg = "" Then Exit Sub

If ObjectType = ObjectTypes.Image Then

mblnMouseDown = True

Dim rRect As Rectangle = ImageArea

If Not XStationary Then
rRect.Offset(GetXDiff, 0)
End If
If Not YStationary Then
rRect.Offset(0, GetYDiff)
End If

If mblnMouseOver Then
ChatForm.Invalidate(rRect)
End If

ElseIf ObjectType = ObjectTypes.Background Then

Dim mousepoint As New Point(e.X, e.Y)
'Dim rArea As Rectangle = DragArea
'rArea.Inflate(GetXDiff, 0)

'If rArea.Contains(mousepoint) Then
' StartYDifference = ChatForm.PointToScreen(mousepoint).Y - ChatForm.Top
' StartXDifference = ChatForm.PointToScreen(mousepoint).X - ChatForm.Left
' StartXY = New Point(0, 0)
' LastXY = New Point(-1, -1)
' EnableFormMove = True
If HSizeArea.Contains(mousepoint) Then
StartXY = New Point(0, 0)
LastXY = New Point(-1, -1)
EnableHSize = True
ChatForm.Cursor = Cursors.SizeWE
ElseIf VSizeArea.Contains(mousepoint) Then
StartXY = New Point(0, 0)
LastXY = New Point(-1, -1)
EnableVSize = True
ChatForm.Cursor = Cursors.SizeNS
ElseIf ResizeHandleArea.Contains(mousepoint) Then
StartXY = New Point(0, 0)
LastXY = New Point(-1, -1)
EnableHVSize = True
ChatForm.Cursor = Cursors.SizeNWSE
End If

End If

End Sub


Private Sub FormOnMouseMove(ByVal Sender As Object, ByVal e As MouseEventArgs) Handles ChatForm.MouseMove

If ObjectType = ObjectTypes.Image And CommandArg = "" Then Exit Sub

Dim rRect As Rectangle = ImageArea

If Not XStationary Then
rRect.Offset(GetXDiff, 0)
End If
If Not YStationary Then
rRect.Offset(0, GetYDiff)
End If

If ObjectType = ObjectTypes.Image Then

If rRect.Contains(e.X, e.Y) Then
If Not mblnMouseOver Then
mblnMouseOver = True
ChatForm.Invalidate(rRect)
End If
Else
If mblnMouseOver Then
mblnMouseOver = False
ChatForm.Invalidate(rRect)
End If
End If

ElseIf ObjectType = ObjectTypes.Background Then

Dim MousePosition As New Point(e.X, e.Y)

'If EnableFormMove Then

' If (LastXY.X <> -1) Then
' MyDrawReversibleRectangle(StartXY, LastXY)
' End If

' ' Update last point.
' LastXY = MousePosition
' ' Draw new lines.
' MyDrawReversibleRectangle(StartXY, MousePosition)

If EnableHSize Then

ChatForm.Cursor = Cursors.SizeWE

If (LastXY.X <> -1) Then
MyDrawReversibleRectangle(StartXY, LastXY)
End If

' Update last point.
LastXY = MousePosition
' Draw new lines.
MyDrawReversibleRectangle(StartXY, MousePosition)

ElseIf EnableVSize Then

ChatForm.Cursor = Cursors.SizeNS

If (LastXY.X <> -1) Then
MyDrawReversibleRectangle(StartXY, LastXY)
End If

If MousePosition.Y > ImageSize.Height Then
' Update last point.
LastXY = MousePosition
' Draw new lines.
MyDrawReversibleRectangle(StartXY, MousePosition)
Else
LastXY.Y = ImageSize.Height
MyDrawReversibleRectangle(StartXY, New Point(ImageSize.Height, LastXY.Y))
End If

ElseIf EnableHVSize Then

ChatForm.Cursor = Cursors.SizeNWSE

If (LastXY.X <> -1) Then
MyDrawReversibleRectangle(StartXY, LastXY)
End If

' Update last point.
LastXY = MousePosition
' Draw new lines.
MyDrawReversibleRectangle(StartXY, MousePosition)

Else

If HSizeArea.Contains(MousePosition) Then
ChatForm.Cursor = Cursors.SizeWE
ElseIf VSizeArea.Contains(MousePosition) Then
ChatForm.Cursor = Cursors.SizeNS
ElseIf ResizeHandleArea.Contains(MousePosition) Then
ChatForm.Cursor = Cursors.SizeNWSE
Else
ChatForm.Cursor = Cursors.Default
End If

End If

End If

End Sub

Public Function GetXDiff() As Integer
Dim xDiff As Integer = ChatForm.Width - PageSize.Width
Return xDiff
End Function

Public Function GetYDiff() As Integer
Dim YDiff As Integer = ChatForm.Height - PageSize.Height
Return YDiff
End Function

Private Sub FormOnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ChatForm.MouseUp

If ObjectType = ObjectTypes.Image And CommandArg = "" Then Exit Sub

ChatForm.Cursor = Cursors.Default

If (LastXY.X <> -1) Then
Dim ptCurrent As Point
ptCurrent.X = e.X
ptCurrent.Y = e.Y
MyDrawReversibleRectangle(StartXY, LastXY)
End If

If EnableHSize Then

If ImageSize.Width <= e.X Then
RebuildBackground(New Size(e.X, ChatForm.Height))
ChatForm.Width = e.X
Else
RebuildBackground(New Size(ImageSize.Width, ChatForm.Height))
ChatForm.Width = ImageSize.Width
End If

ElseIf EnableVSize Then

If ImageSize.Height <= e.Y Then
RebuildBackground(New Size(ChatForm.Width, e.Y))
ChatForm.Height = e.Y
Else
RebuildBackground(New Size(ChatForm.Width, ImageSize.Height))
ChatForm.Height = ImageSize.Height
End If

ElseIf EnableHVSize Then

Dim newSizeX As Integer
Dim newSizeY As Integer

If PageSize.Width <= e.X Then
'ChatForm.Width = e.X
newSizeX = e.X
Else
'ChatForm.Width = PageSize.Width
newSizeX = PageSize.Width
End If

If PageSize.Height <= e.Y Then
'ChatForm.Height = e.Y
newSizeY = e.Y
Else
'ChatForm.Height = PageSize.Height
newSizeY = PageSize.Height
End If

RebuildBackground(New Size(newSizeX, newSizeY))

ChatForm.Width = newSizeX
ChatForm.Height = newSizeY

'ElseIf EnableFormMove Then

' Dim pt As Point
' pt = ChatForm.PointToScreen(New Point(e.X, e.Y))
' ChatForm.Location = New Point(pt.X - StartXDifference, pt.Y - StartYDifference)

End If

If EnableHSize Or EnableVSize Or EnableHVSize Then RepositionHandles()

StartXY.X = -1
StartXY.Y = -1
LastXY.X = -1
LastXY.Y = -1

mblnMouseDown = False

If ObjectType = ObjectTypes.Image Then

If mblnMouseOver And Not (EnableFormMove Or EnableHSize Or EnableVSize Or EnableHVSize) Then

ChatForm.Invalidate(ImageArea)

RaiseEvent ButtonClick(CommandArg, Me)

mblnMouseOver = False

ChatForm.Invalidate(ImageArea)

End If

End If

EnableFormMove = False
EnableHSize = False
EnableVSize = False
EnableHVSize = False

End Sub

Public Sub RepositionHandles()
On Error Resume Next

HSizeArea.X = ChatForm.Width - 5
HSizeArea.Height = ChatForm.Height - ResizeHandleArea.Height

VSizeArea.Y = ChatForm.Height - 5
VSizeArea.Width = ChatForm.Width - ResizeHandleArea.Width

ResizeHandleArea.X = ChatForm.Width - ResizeHandleArea.Width
ResizeHandleArea.Y = ChatForm.Height - ResizeHandleArea.Height

End Sub

Private Sub FormOnPaint(ByVal Sender As Object, ByVal e As PaintEventArgs) Handles ChatForm.Paint

'-- Determine how much room we now have up the middle
Dim xDiff As Integer = GetXDiff()
Dim yDiff As Integer = GetYDiff()

Select Case ObjectType

Case ObjectTypes.Background

CurrentArea = ImageArea

e.Graphics.DrawImage(BackgroundImage, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel)

'ChatForm.EnableTextBoxes()

Case ObjectTypes.Image

Dim rDest As Rectangle = ImageArea

If Not XStationary Then rDest.Offset(xDiff, 0)
If Not YStationary Then rDest.Offset(0, yDiff)

CurrentArea = rDest

If e.ClipRectangle.IntersectsWith(rDest) Then

If mblnMouseOver Then
If mblnMouseDown Then
e.Graphics.DrawImage(ImageObject, rDest, ImageDownRect, GraphicsUnit.Pixel)
Else
e.Graphics.DrawImage(ImageObject, rDest, ImageOverRect, GraphicsUnit.Pixel)
End If
Else
e.Graphics.DrawImage(ImageObject, rDest, ImageNormalRect, GraphicsUnit.Pixel)
End If

End If

Case ObjectTypes.Text

If mblnMouseDown Then Return

Dim rDest As Rectangle = TextArea

If Not XStationary Then rDest.Offset(xDiff, 0)
If Not YStationary Then rDest.Offset(0, yDiff)

If CanGrowW Then
rDest.Width = rDest.Width + xDiff
End If

If CanGrowH Then
rDest.Height = rDest.Height + yDiff
End If

CurrentArea = rDest

If e.ClipRectangle.IntersectsWith(rDest) Then

e.Graphics.TextRenderingHint = Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.DrawString(TextValue, TextFont, New SolidBrush(TextColor), RectangleF.op_Implicit(rDest), TextFormat)

End If

End Select

End Sub

Private Sub MyDrawReversibleRectangle(ByVal p1 As Point, ByVal p2 As Point)

Dim rc As Rectangle

' Convert the points to screen coordinates.
p1 = ChatForm.PointToScreen(p1)
p2 = ChatForm.PointToScreen(p2)

If EnableHSize Then
rc.Height = ChatForm.Height
rc.X = p1.X
rc.Width = p2.X - p1.X
rc.Y = p1.Y
ElseIf EnableVSize Then
rc.Height = p2.Y - p1.Y
rc.Width = ChatForm.Width
rc.X = p1.X
rc.Y = p1.Y
ElseIf EnableHVSize Then
rc.Height = p2.Y - p1.Y
rc.X = p1.X
rc.Width = p2.X - p1.X
rc.Y = p1.Y
'ElseIf EnableFormMove Then
' rc.X = p2.X - StartXDifference
' rc.Y = p2.Y - StartYDifference
' rc.Width = ChatForm.Width
' rc.Height = ChatForm.Height
End If

' Draw the reversible frame.
ControlPaint.DrawReversibleFrame(rc, Color.Gray, FrameStyle.Dashed)

End Sub

Public Sub RebuildBackground(ByVal newSize As Size)

Dim xDiff As Integer = newSize.Width - ImageSize.Width 'GetXDiff()
Dim yDiff As Integer = newSize.Height - ImageSize.Height 'GetYDiff()

Dim fillerColor As Color = HFiller.GetPixel(0, FillerY)

Dim rTL As New Rectangle(0, 0, FillerX, FillerY)
Dim rTR As New Rectangle(FillerX + xDiff, 0, ImageSize.Width - FillerX, FillerY)
Dim rBL As New Rectangle(0, FillerY + yDiff, FillerX, ImageSize.Height - FillerY)
Dim rBR As New Rectangle(FillerX + xDiff, FillerY + yDiff, ImageSize.Width - FillerX, ImageSize.Height - FillerY)
Dim rFillerX As New Rectangle(FillerX, 0, xDiff, ImageSize.Height + yDiff)
Dim rFillerY As New Rectangle(0, FillerY, ImageSize.Width + xDiff, yDiff)
Dim bBrushX As Brush
Dim bBrushY As Brush

'BackgroundImage = New Bitmap(ChatForm.Width, ChatForm.Height)
BackgroundImage = New Bitmap(newSize.Width, newSize.Height)
Dim gBack As Graphics = Graphics.FromImage(BackgroundImage)

gBack.Clear(fillerColor)

If xDiff > 0 Then

Dim bmpFillerX As New Bitmap(1, ImageSize.Height + yDiff)
Dim g As Graphics = Graphics.FromImage(bmpFillerX)

g.Clear(fillerColor)

g.DrawImage(HFiller, New Rectangle(0, 0, 1, FillerY), New Rectangle(0, 0, 1, FillerY), GraphicsUnit.Pixel)
g.DrawImage(HFiller, New Rectangle(0, FillerY + yDiff, 1, ImageSize.Height - FillerY), New Rectangle(0, FillerY, 1, ImageSize.Height - FillerY), GraphicsUnit.Pixel)
g.Dispose()

bBrushX = New TextureBrush(bmpFillerX)

Else

bBrushX = New TextureBrush(HFiller)

End If

If yDiff > 0 Then

Dim bmpFillerY As New Bitmap(ImageSize.Width + xDiff, 1)
Dim g As Graphics = Graphics.FromImage(bmpFillerY)

g.Clear(fillerColor)

g.DrawImage(VFiller, New Rectangle(0, 0, FillerX, 1), New Rectangle(0, 0, FillerX, 1), GraphicsUnit.Pixel)
g.DrawImage(VFiller, New Rectangle(FillerX + xDiff, 0, ImageSize.Width - FillerX, 1), New Rectangle(FillerX, 0, ImageSize.Width - FillerX, 1), GraphicsUnit.Pixel)
g.Dispose()

bBrushY = New TextureBrush(bmpFillerY)

Else

bBrushY = New TextureBrush(VFiller)

End If

gBack.DrawImage(ImageObject, rTL, rTL, GraphicsUnit.Pixel)
gBack.DrawImage(ImageObject, rBL, 0, FillerY, FillerX, ImageSize.Height - FillerY, GraphicsUnit.Pixel)


'-- Fill the x
If xDiff > 0 Then
gBack.FillRectangle(bBrushX, rFillerX)
End If

If yDiff > 0 Then
'-- Fill the y
gBack.FillRectangle(bBrushY, rFillerY)
End If

gBack.DrawImage(ImageObject, rTR, FillerX, 0, ImageSize.Width - FillerX, FillerY, GraphicsUnit.Pixel)
gBack.DrawImage(ImageObject, rBR, FillerX, FillerY, ImageSize.Width - FillerX, ImageSize.Height - FillerY, GraphicsUnit.Pixel)
gBack.Dispose()
bBrushX.Dispose()
bBrushY.Dispose()

ChatForm.Region = BitmapToRegion(BackgroundImage, ImageTransparentColor)

ChatForm.Invalidate()


End Sub

Private Function BitmapToRegion(ByVal bitmap As Bitmap, ByVal transparencyColor As Color) As Region

If bitmap Is Nothing Then
Throw New ArgumentNullException("Bitmap", "Bitmap cannot be Nothing!")
End If

Dim height As Integer = bitmap.Height
Dim width As Integer = bitmap.Width

Dim path As New GraphicsPath

Dim i As Integer
Dim j As Integer

For j = 0 To (height - 1)
For i = 0 To (width - 1)
If Not bitmap.GetPixel(i, j).Equals(transparencyColor) Then

Dim x0 As Integer = i

Do While ((i < width) AndAlso (Not bitmap.GetPixel(i, j).Equals(transparencyColor)))
i += 1
Loop

path.AddRectangle(New Rectangle(x0, j, i - x0, 1))

End If
Next
Next

Dim region As New Region(path)
path.Dispose()

Return region

End Function

End Class

[22456 byte] By [stavros1979] at [2007-12-23]