Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow
Picturebox Refreshing slow Picturebox Refreshing slow
Picturebox Refreshing slow
Go Back  Xtreme Visual Basic Talk > > > Picturebox Refreshing slow


Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2013, 02:21 AM
Patpop01 Patpop01 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 4
Question Picturebox Refreshing slow


Hi All,

This is my first post here on the forum, so I hope you can help me out.

I have a problem with a picturebox. The update is rather slow, especially when on higher resolutions (1920x1080)...

The main goal of the tool would be have a pannable grid drawn as a background image on a picturebox. I would like to get it on the background, since I will be adding an xyz arrow indicator and watermark later on.
The image layer on the picturebox I would like to use for the actual doodling.

The grid should move when moving the mouse with the middle mouse button pressed.

What I did so far was create a class, that generates a bitmap (only on resize and mousewheel events (for changing grid size)) that is picturebox + gridsize in size, (ex 800x600 picturebox with 100 grid -> 900x600). (cCanvasGraphics.GridBitmap)

I then, in the mousemove event, draw an image based on the Gridbitmap with a offset determined by my cursor position and with the picturebox size.

The idea was to reuse the gridbitmap as long as there is no resize or gridchange, in order to minimize compute overhead.

In this example the grid size is fixed at 100 thus the gMouseCurrent_X = e.X Mod 100...

Code:
   
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Middle Then
            gMouseCurrent_X = -(e.X Mod 100)
            gMouseCurrent_Y = -(e.Y Mod 100)

            Dim lGridBitmap As New Bitmap(PictureBox1.Size.Width, PictureBox1.Size.Height)

            Dim lGridGraphics As Graphics = Graphics.FromImage(lGridBitmap)
            lGridGraphics.DrawImageUnscaled(cCanvasGraphics.GridBitmap, gMouseCurrent_X, gMouseCurrent_Y)

            PictureBox1.BackgroundImage = lGridBitmap.Clone

            lGridBitmap.Dispose()
            lGridGraphics.Dispose()

            GC.Collect()
        End If
    End Sub
But when I run this, it gets awfully slow on higher resolutions. Guess at lower resolutions the same thing happends, but is not visible to me.

Any ideas on how to fix this?
Thanks
Reply With Quote
  #2  
Old 08-26-2013, 08:52 AM
passel's Avatar
passelPicturebox Refreshing slow passel is offline
Sinecure Expert

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

I've found using the built in images of a picturebox are going to refresh too slowly to be useful for rapid screen updates.
Drawing the grid each time might be faster than using an image of the grid anyway, although you will want your doodles in a separate bitmap to be drawn on top of the grid.

I don't have time to go into details at the moment got a meeting in a few minutes.
But in the mean time, perhaps look at the attachment to this post.
It is a stretchable image, which you probably don't want, but one concept included as part of that example is the use of a texture brush to rapidly draw a "complex" image repeatedly.
In the case of this example, you use the left mouse button to doodle over the background map. You can then use the right mouse button to drag that doodle back and forth over the map, demonstrating the use of the texture brush to provide that rapid scrolling of one image over another.
Got to go.

p.s. The meeting is over, I downloaded the example from the link above, and added drawing a grid and to make it more representative of what you want, I made sure that the drawing of the grid itself was not stretched as the background and the "doodle" image are.
That way, if you maximize the form and drag the grid around with the middle mouse button, the speed should be representative to what you want to do since the spacing of the grid lines is constant at 100x100 pixels, so will draw more squares as the form increases in size.

Use the left mouse to doodle, middle mouse to scroll the grid, right mouse to scroll the doodle. In this case the doodle was defined to map to the map originally, so only scrolls left and right, where as the grid can scroll in both X and Y directions.
The attached example draws a stretchable, but fixed, background image, then draws the grid (unstretched), then draws the doodle (using a texturebrush), but again stretched to match the background map.
That Paint event procedure looks like this
Code:
  Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.ScaleTransform(1 / xsf, 1 / ysf)    'xsf,ysf scales the mouse inputs to the drawing, the inverse scales the drawing to the picturebox
    e.Graphics.DrawImage(mapCache, 0, 0)           'draw the "lower" map background first
    e.Graphics.ResetTransform()                    'reset the transform so the grid is not stretched
    drawGrid(e.Graphics)                           'draw the grid
    e.Graphics.ScaleTransform(1 / xsf, 1 / ysf)    'xsf,ysf scales the mouse inputs to the drawing, the inverse scales the drawing to the picturebox

    If leftBtn Then                                'If we are currently drawing with the left button then
      e.Graphics.DrawImage(drawOverlay, 0, 0)        'draw the overlay bitmap "on top"
    Else                                           'else
      If tb IsNot Nothing Then                     ' If our texturebrush version of the overlay bitmap exists
        e.Graphics.FillRectangle(tb, PictureBox1.ClientRectangle)  'Fill the rectangle with our texturebrush copy of the overlay bitmap
      End If
    End If
  End Sub
Since you don't want to stretch your image, and you may not need the complication of creating a texture brush from your drawing, you could simply draw the grid, then draw your doodle image using .DrawImage, from a memory bitmap.
Attached Files
File Type: zip AutoStretchExample2withGrid.zip (159.0 KB, 3 views)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-26-2013 at 10:29 AM.
Reply With Quote
  #3  
Old 08-26-2013, 11:50 AM
Patpop01 Patpop01 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 4
Default

Hi Passel,

First of ll thanks for the quick reply...
I will try your code tomorrow and will give you feedback on how it works.

I will keep you up to date.
Reply With Quote
  #4  
Old 08-31-2013, 12:34 AM
Patpop01 Patpop01 is offline
Newcomer
 
Join Date: Aug 2013
Posts: 4
Default

Hi Passel,

I tried your code and it seems to work fine...

But I discussed what I wanted to do in the long run and a colleague of mine suggested to use WPF. Since the app will be rather graphical, even perhaps 3D models in the long run, he said GDI just won't cut it.

Thanks for your help and keep up the good work on this forum.

Regards
Reply With Quote
  #5  
Old 08-31-2013, 10:05 AM
AtmaWeapon's Avatar
AtmaWeaponPicturebox Refreshing slow AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Whether WPF can help depends on what you are doing. It is not a magic high-performance panacea for all things graphics.

WPF shines at several things WinForms doesn't, primarily animation and alpha blending. It has no significant advantage and can actually be worse at something WinForms handles well: display of bitmapped graphics re-rendered every frame. Understanding the guts of WPF reveals why.

WPF is a "retained mode" graphics system. That means it keeps a representation of what it is displaying in memory and operates on that memory model. In Windows Forms, animating a red circle involves a lot of effort; you have to redraw the pixels where the circle was, then draw the new circle over it. WPF does this for you as it animates an element across its surface. It's even speedy at alpha blending because it's very good at understanding exactly which pixels need to be recalculated. So long as what you are rendering translates directly to one of the things WPF can easily understand and represent in memory, performance will beat the heck out of WinForms.

But WPF is not impressive at "immediate mode" graphics, at least not much more impressive than WinForms. When you give WPF a big bitmap to render, there's not much it can do but blit that to the screen. If there are transparent pixels, it can remember those pixels, but if every frame you provide a new bitmap it has no way to know which pixels changed so it has to recalculate every pixel. This is an area where WinForms can be "better" because it doesn't do full alpha blending; you'll get inaccurate results but you'll get them faster.

In short, WPF performs orders of magnitude better than WinForms when you are doing things that can be represented by "change some property of this object in the scene". In WinForms, that always translates to "redraw every pixel" (unless you make the effort to optimize) and WPF can often save a ton of its own rendering effort by calculating this on its own. But if you're making significant updates to a large bitmap there's really few advantages WPF has over WinForms, and my experience is squeezing performance out of WPF can be very, very frustrating in this case.

Experienced developers seeking high performance seem to be using DirectX. I think there is some method by which WPF and DirectX can cooperate in the same application; I've never studied it because it's never been relevant to my work.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
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
Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow Picturebox Refreshing slow
Picturebox Refreshing slow
Picturebox Refreshing slow
 
Picturebox Refreshing slow
Picturebox Refreshing slow
 
-->