Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Out of Memory" exception when drawing picture to form

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I'm making a class to handle custom forms. To make things easier and consistent I use a brush to draw to the form. Everything works great until I try to draw a picture to the form that is smaller than the form if I try to clip to center it. If it is bigger than the form it clips to center correctly. The picture I'm working with right now is taller than the forms height, but shorter than its width. If I try to clip to center horizontally it comes up with the error. If I set the x axis to 0 I get no error and it clips to center vertically fine with the rest of the form the default background color. Here is the code for the picture draw:
Code:
    Dim orgCenter As New Point(_Image.Picture.Width / 2, _Image.Picture.Height / 2)
    Dim myCenter As New Point(Me.Width / 2, Me.Height / 2)
    Dim x As Integer
    Dim y As Integer
    Dim width As Integer
    Dim height As Integer

    If Me.Width > _Image.Picture.Width Then
        x = myCenter.X - orgCenter.X
        width = _Image.Picture.Width
    Else
        x = orgCenter.X - myCenter.X
        width = Me.Width
    End If

    If Me.Height > _Image.Picture.Height Then
        y = myCenter.Y - orgCenter.Y
        height = _Image.Picture.Height
    Else
        y = orgCenter.Y - myCenter.Y
        height = Me.Height
    End If

    nImageWrap = WrapMode.Clamp
    bmp = _Image.Picture
    nImage = bmp.Clone(New Rectangle(x, y, width, height), bmp.PixelFormat)
    CurrentBrush = New System.Drawing.TextureBrush(nImage, nImageWrap)

To paint to the screen I use:
Code:
e.Graphics.FillPath(CurrentBrush, _Shape)
where _Shape is the preconfigured region of the form (for rounded corners/odd shaped forms).

x asxis 0:
x_axis_0.png


I clone the image as that is the easiest way to clip the image to use in a brush. I use .FillPath rather than .DrawImage so I can consistently use a brush to draw to the screen as the other options for the form are various gradients and such.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
So, I figured out a way to fix the problem. I stumbled on a way to merge two pictures which gave me the idea on how I could fix my problem. To fix the problem I allow it to clone with x or y as 0. I then check if the image is the correct size. If not I then create an image of the correct size paint it the current BackColor and then paint the image in the correct location.

Like so:
Code:
                            If nImage.Width < Me.Width Or nImage.Height < Me.Height Then
                                Dim newCenter As New Point(nImage.Width / 2, nImage.Height / 2)
                                If x <> Me.Width Then x = myCenter.X - newCenter.X
                                If y <> Me.Height Then y = myCenter.Y - newCenter.Y

                                Dim ImageFill As New Bitmap(Me.Width, Me.Height)
                                Dim g As Graphics = Graphics.FromImage(ImageFill)
                                g.FillRectangle(New SolidBrush(Me.BackColor), 0, 0, ImageFill.Width, ImageFill.Height)
                                g.DrawImage(nImage, New Point(x, y))
                                g.Dispose()
                                g = Nothing

                                nImage = ImageFill
                            End If

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I didn't think about what I was typing. If you notice this is just stupid.

Code:
Dim newCenter As New Point(nImage.Width / 2, nImage.Height / 2)
If x <> Me.Width Then x = myCenter.X - newCenter.X
If y <> Me.Height Then y = myCenter.Y - newCenter.Y

It should just be:

Code:
Dim newCenter As New Point(nImage.Width / 2, nImage.Height / 2)
x = myCenter.X - newCenter.X
y = myCenter.Y - newCenter.Y

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top