Invalidate and Paint
Invalidate and Paint
Invalidate and Paint
Invalidate and Paint
Invalidate and Paint
Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint
Invalidate and Paint Invalidate and Paint
Invalidate and Paint
Go Back  Xtreme Visual Basic Talk > > > Invalidate and Paint


Reply
 
Thread Tools Display Modes
  #1  
Old 09-15-2012, 06:33 AM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default Invalidate and Paint


I am so confused, Ive used this function set up quite a few times before and its never given me any problems... aside from one issue ive always had with it but ill get into that later. First things first. My "Me.Invalidate()" isnt redrawing the page and idk why. Also my "Private Sub PaintForm" event seems to be running when the form is loaded. Ive never had that happen before, it will try and read the code before the Public Sub in my module assigns my images to their variables causing a variable NULL error. Ive been missing around with it for a few hours now and finally decided to ask for help. My original codes is all mangled from so many attempts to fix it so ill try and post what I thought was the correct way I had it from the start.

Code:
   

 Private Sub frm4Play_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        Load_Areas() 'Runs the event in the module to set imgs to variables 

    End Sub


Private Sub PaintForm(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint


        e.Graphics.DrawImage(Areas(intMapBgX, intMapBgy), 72, 80, 932, 604)
End Sub

'Once Load_Areas() event happens and Invalidate, this should paint the Image to the from'

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        intMapBgX = intMapBgX + 1
        intMapBgy = intMapBgy + 1
        Me. Invalidate
'Button click changes the x & y in the Areas(x,y) img array to display a different image after Me.Invalidate '
    End Sub


------- Code in the Module --------------

Public Sub Load_Areas()
'This part of the code I got help with from you guys, but it basically takes
'one image and breaks it down into a bunch of small ones to be displayed 
'one at a time though the paint event. Assigning each new image to different 
'cord in the array'

        If xxx <> 50 Then
            Dim src_rect As New Rectangle(intleft, inttop, 100, 100)
            Dim dst_rect As New Rectangle(0, 0, 100, 100)
            For xxx = 1 To 50
                Areas(a, b) = New Bitmap(100, 100)
                Using gr As Graphics = Graphics.FromImage(Areas(a, b))

                    ' Copy that part of the image.
                    gr.DrawImage(Mapps, dst_rect, src_rect, GraphicsUnit.Pixel)

                End Using

                If xxx = 10 Then
                    a = 1
                    b = 2
                    intleft = 1
                    inttop = 100
                    src_rect.X = intleft
                    src_rect.Y = inttop
                ElseIf xxx = 20 Then
                    a = 1
                    b = 3
                    intleft = 1
                    inttop = 200
                    src_rect.X = intleft
                    src_rect.Y = inttop
                ElseIf xxx = 30 Then
                    a = 1
                    b = 4
                    intleft = 1
                    inttop = 300
                    src_rect.X = intleft
                    src_rect.Y = inttop
                ElseIf xxx = 40 Then
                    a = 1
                    b = 5
                    intleft = 1
                    inttop = 400
                    src_rect.X = intleft
                    src_rect.Y = inttop
                ElseIf xxx <> 10 Or xxx <> 20 Or xxx <> 30 Or xxx <> 40 Then
                    intleft = intleft + 100
                    src_rect.X = intleft
                    a = a + 1

                End If
            Next
            xxx = 50
        End If

So like I said before the one issue i was expecting was that when i have parts of my Paint Even linked to an event in the Module, like i have it in the above code, it has never worked for me. But restricting it to just that event has given my problems in the past with my ability to change the image i want to display.

Anyway, if there is anyway around that, i would love to know about it. If not, what is the general thing im miscodeing and how can i fix it?

Im in vb2008

Thanks in advance!

Last edited by passel; 09-15-2012 at 07:48 PM. Reason: Wrapped long single line comment in code block so code window wasn't 1600 pixels wide
Reply With Quote
  #2  
Old 09-15-2012, 07:03 AM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default So something happened...

I moved where i was calling the event in my module to the previous form and it seems to have resolved all my issues but one. It still draws my from automatically from the Paint event before i have that event called. Any ideas on how to change that? In other programs where ive used the paint event it hasn't done that... not sure why it is now... or am i mistaken and thats just a function of loading the form?

Thanks!
Reply With Quote
  #3  
Old 09-15-2012, 09:04 AM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default Well it seems like if it isnt one thing, its another.

So as i progressed i found i needed to up my for/next loop in my module, see code in first post, from 500 loops to like ... hum... 100,000 ... but its a total ram hog and keeps making my program non responsive.... anyways to fix that or another way prepossessing my images?

Basically taking one very large image and breaking it down into segments of now 10x10 pixels and displaying those 10x10 pixel images one at a time.

Thanks!
Reply With Quote
  #4  
Old 09-15-2012, 09:04 PM
passel's Avatar
passelInvalidate and Paint passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Looking at the code in the first post, I don't see a loop of 500.
I see a loop of 50 when you divide the large image into smaller bitmaps.
Is there a reason the large bitmap has to be broken into smaller bitmaps?
If you're only showing one at a time, why can't you just use DrawImage to display the computed portion of the big bitmap to the display when selected.
If you were displaying a lot of objects, and scrolling, then there would be a reason to break the image up so you wouldn't have to access the whole big picture to display a portion of it, but right now your example is just displaying one small portion (100x100) pixels, blown up to 932 x 604 pixels.

You say it is now 10x10 pixels, but still displaying it one at a time (are you still blowing it up to 932 x 604 pixels?).

If you only need to display one at a time, I wouldn't waste the time and memory to divide the large image up. Just display the portion you want in the paint event.
Code:
Private Sub PaintForm(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim srcX as Integer = (intMapBgX-1)*10
    Dim srcY as Integer = (intMapBgY-1)*10
    Dim src_rect As New Rectangle(srcX, srcY, 10, 10)
    Dim dst_rect As New Rectangle(72, 80, 932, 604)

    e.Graphics.DrawImage(Mapps, dst_rect, src_rect, GraphicsUnit.Pixel)
End Sub
Yes, the paint event will fire when you show the form (it can't display the form without painting it).

If your initialization is somehow broken up such that the form can load and execute the Paint event before you've initialized the data the paint event is to use, then you will need to take some preventive measures.
Such as adding a variable you can check that is set to indicate when initialization is done, so if somehow the paint event is triggered, and you haven't finished initialization, the code in the paint event skip trying to access the data, until later when you trigger it yourself.

Or you could just test the object before trying to use it.
Code:
If Mapps IsNot Nothing Then
    e.Graphics.DrawImage(Mapps, dst_rect, src_rect, GraphicsUnit.Pixel)
End If
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Invalidate and Paint
Invalidate and Paint
Invalidate and Paint Invalidate and Paint
Invalidate and Paint
Invalidate and Paint
Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint Invalidate and Paint
Invalidate and Paint
Invalidate and Paint
 
Invalidate and Paint
Invalidate and Paint
 
-->