Hi,
here is my problem: I have created panel and put picBox in it. AutoScroll for panel is on, so the scrollbars are there. When scrollbars come to the limit, pictureBox is fitted with new image. Image is loaded correctly, but scrollbars rest in last position, i.e. they don't come to position x=0,y=0 as wanted.
Any ideas?
pete

Dani AI

Generated

Symptom and likely cause: replacing the PictureBox image while the Panel has AutoScroll enabled can leave the scrollbars at their previous offset and produce a visible “jump” or lag. That usually happens because image I/O or decoding is running on the UI thread (or the layout/scroll-range update runs before the image is assigned), so the scrollbars repaint earlier than the picture does.

Actionable approach (expand on ’s worker-thread idea, and tied to ’s emulator sample):

  • Load and decode the image off the UI thread. Do all file/stream work and any resizing in the background so the UI thread is free to update scroll state and repaint.
  • When the background work finishes, perform a single, synchronous UI update: suspend layout, assign the ready Image to the PictureBox, set the PictureBox size (or SizeMode) to the final image size, resume layout and force layout/paint, then make the control visible at the top-left of the panel.

Example pattern (BackgroundWorker — compact-framework friendly):

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s,e) => {
    // do file/stream read and Image.FromStream here
    e.Result = Image.FromFile((string)e.Argument);
};
bw.RunWorkerCompleted += (s,e) => {
    if (e.Error != null) return;
    Image img = (Image)e.Result;
    panel.SuspendLayout();
    pictureBox.Image = img;
    pictureBox.Size = img.Size; // or adjust SizeMode
    panel.ResumeLayout();
    panel.PerformLayout();
    panel.ScrollControlIntoView(pictureBox); // bring to top/visible synchronously
};
bw.RunWorkerAsync(pathToImage);

Troubleshooting notes:

  • If loading still blocks, decode to the target display size in the background to reduce cost.
  • Use a small placeholder during loading to avoid visual jumps.
  • Avoid Application.DoEvents for this; prefer SuspendLayout/ResumeLayout + PerformLayout/Invalidate.
  • On the Compact Framework be sure BackgroundWorker (or ThreadPool + Control.Invoke) is used correctly to marshal back to the UI thread.

This preserves ’s concept while ensuring the scroll state and image update happen together, which should eliminate the scrollbar-vs-image delay seen in ’s emulator test.

Recommended Answers

All 8 Replies

Please attach picture to what happened to understand you well.

Reset the scroll bars:

panel1.AutoScrollPosition = new Point(0, 0);

Thanks sknake,
but I still have problem, my scroll bars move faster than my image on picBox, so I have sort of delay ...
Have any ideas on this?

I guess that depends on how large the image is, how fast your computer is, etc etc. Please upload a sample project for that question (be sure we can compile it and see the problem).

sknake,

there is whole sln attached, check it out.

How do you find the application to run it on the emulator .. or can I move this code over to a winforms project and see the same behavior? I haven't used the mobile emulators before

I'have rearranged code and zipped it.
You go with F5 and choose Win mobile 5.0 emulator from the menu, the rest is ok.

It seems to scroll fine for me except when a new picture is about to load. I don't know the capabilities of programming for mobile devices but you could load the surrounding images up in another thread so it lets them scroll to the end of the image instead of hanging up before you get to the end.

I think a worker thread would solve the behavior you're going after.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.