473,473 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Optimizing Repeated PictureBox.Paints

I'm trying to develop a graphically-simple, gameplay-complex RPG, and I
decided to create a graphical Proof of Concept just to confirm that
graphics would be... well, simple.

Predictably, they've proven otherwise.

This is my PictureBoxMap_Paint() Sub, which a tiles a PictureBox with 19*
17 40*40-pixel images, two rows and two columns of which are painted
beyond the current bounds of the picturebox to facilitate scrolling. The
If/Else statements handle the bounds of the map... At the moment they
simply 'roll over' and display the graphics on the other side.

Dim A, B As Integer
For A = -9 To 9
For B = -8 To 8
Dim X, Y As Integer
If A + CurrentCoords.East < 0 Then
X = Cell.GetUpperBound(0) + (A + CurrentCoords.East) + 1
ElseIf A + CurrentCoords.East > Cell.GetUpperBound(0) Then
X = (A + CurrentCoords.East) - Cell.GetUpperBound(0) - 1
Else
X = A + CurrentCoords.East
End If
If B + CurrentCoords.North < 0 Then
Y = Cell.GetUpperBound(1) + (B + CurrentCoords.North) +
ElseIf B + CurrentCoords.North > Cell.GetUpperBound(1) Then
Y = (B + CurrentCoords.North) - Cell.GetUpperBound(1) - 1
Else
Y = B + CurrentCoords.North
End If
e.Graphics.DrawImage(CellImages(Cell(X, Y,
CurrentCoords.Up).Image), ((A + 9) * 40) - 40 + XOffSet, (520 - ((B + 6)
* 40) + YOffSet))
Next
Next

The XOffSet and YOffSet are used to produce the scrolling effect, as seen
in this code from my Form_KeyDown procedure.

If e.KeyCode = System.Windows.Forms.Keys.Left Then
For XOffSet = 0 To 40 Step 4
PictureBox_Map.Refresh()
Next
XOffSet = 0
If CurrentCoords.East > 0 Then
CurrentCoords.East = CurrentCoords.East - 1
Else
CurrentCoords.East = Cell.GetUpperBound(0)
End If
PictureBox_Map.Refresh()
End If

Unfortunately, this produces a very slow scroll on my Pentium III 933MHZ;
I've had to add the equivalent of frame-skipping using 'Step 4' just to
make it comparable to the speed in, say, Final Fantasy.

My theory for the reason behind this slowness was the reassembly of the
map with each PictureBox_Map.Refresh(), and I therefore searched for a
way to offset a preassembled graphic, reassembling only when necessary.

Unfortunately, I could not then force PictureBox_Map to display this
preassembled graphic in such a way that it could be offset as necessary
to produce scrolling.

An IRC acquaintance who otherwise has been very helpful suggested that
calling PictureBox_Map.Refresh might be producing some unintended
overhead, and that calling the PictureBox_Paint() routine directly from
KeyDown might be better.

Unfortunately, I could not call it in such a way that my entry for the
PaintEventArgs argument would not completely break the PictureBox_Paint
routine.

Does anybody have any suggests as to where, exactly, most of my overhead
is coming from and how I might reduce or eliminate it?

Are repliers have my sincere gratitude.
Mar 7 '06 #1
1 2548

The Confessor wrote:
I'm trying to develop a graphically-simple, gameplay-complex RPG, and I
decided to create a graphical Proof of Concept just to confirm that
graphics would be... well, simple.

[snip]

I won't go into details, because I think your performance problems can
be best addressed by conceptual changes rather than particular code
changes.

The way to handle a picturebox that displays a complex graphics is not
to build the graphics in the Paint event, but rather to maintain a
Bitmap, which holds the actual image, and in the Paint event just
DrawImage the bitmap onto the Graphics provided. When the image is
required to change, draw the changes onto the Bitmap, then Invalidate
the picturebox to force it to paint. Here is a short example:

' VS2003/2005
' create a new Windows Forms app
' drop a picturebox and two buttons on a form
' add this at the top of the code file
Imports System.Drawing

'add this in the form, before the End Class line
Private myImage As Bitmap
Private myImageGraphics As Graphics
'probably more correct to create the Graphics
'whenever we want to draw
'but this is quicker

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myImage = New Bitmap(PictureBox1.Width, PictureBox1.Height,
Imaging.PixelFormat.Format32bppArgb)
myImageGraphics = Graphics.FromImage(myImage)

myImageGraphics.Clear(Color.White)
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'all we do in here is draw myImage
e.Graphics.DrawImage(myImage, e.ClipRectangle, e.ClipRectangle,
GraphicsUnit.Pixel)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'create a complex image
Dim x As Integer, y As Integer
Dim b As Brush, c As Color
Dim r As New Random

For x = 0 To myImage.Width Step 10
For y = 0 To myImage.Height Step 8
c = Color.FromArgb(r.Next(0, 255), r.Next(0, 255),
r.Next(0, 255))
b = New SolidBrush(c)
Try
myImageGraphics.FillRectangle(b, x, y, 10, 8)
Finally
b.Dispose()
End Try
Next
Next

'and force painting
PictureBox1.Invalidate()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'make some change to the image
Dim oldImage As Bitmap = myImage
myImage = New Bitmap(PictureBox1.Width, PictureBox1.Height,
Imaging.PixelFormat.Format32bppArgb)
myImageGraphics = Graphics.FromImage(myImage)

myImageGraphics.ResetTransform()
myImageGraphics.TranslateTransform(-myImage.Width \ 2,
-myImage.Height \ 2, Drawing2D.MatrixOrder.Append)
myImageGraphics.RotateTransform(10,
Drawing2D.MatrixOrder.Append)
myImageGraphics.TranslateTransform(myImage.Width \ 2,
myImage.Height \ 2, Drawing2D.MatrixOrder.Append)
myImageGraphics.DrawImage(oldImage, 0, 0)

PictureBox1.Invalidate()
End Sub

Your situation is going to involve creating the main image with an
extra strip of cells, as currently, and some error-prone transformation
stuff working out exactly which portion of the image to draw in the
Paint event, so have fun with that :) The key point is that we only do
the 'hard' work (that nested loop that creates the image) when we have
to, ie when we scrol to a region we haven't seen before. While
examining a portion of the image we *have* created, all we do is just
DrawImage.

--
Larry Lard
Replies to group please

Mar 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Smith | last post by:
I am making a program that needs to put a copy of a certain small image wherever the user clicks with the mouse. I am using a pictureBox as my main area (although I can change this if necessary). ...
1
by: Frank Rizzo | last post by:
How would I go about adding a drop shadow to a picture box? Thanks
0
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
2
by: Limin | last post by:
I add some picturebox in the listbox as controls like following. Dim myPic as PictureBox 'Init myPic from here '... '... for i=0 to 30 'New picturebox '... 'set mypic property '....
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
4
by: munibe | last post by:
Hi, i have a problem about picturebox control. if you may help me, i will be so happy. i have a picturebox named pic_map, and i added a button named customer_button, my wish is to add a new small...
3
by: kirk | last post by:
I have a form with a PictureBox control on it. The .Image property is set to a PNG file(which shows the picture of the US map) with some transparency in it. The .BackColor property is set to...
8
by: Syoam4ka | last post by:
Hi, I have a WinApp in which I have a pictureBox. I can draw in it like in the paint of windows - I accomplish that with an arrayList of Points and DrawLines method of the Graphics. So it...
5
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.