Main Topics
Browse All TopicsI have a large background image (3500x2400 3.5mb jpeg) that I placed as the background image for a panel
Then I sized the panel to 3500x2400 and placed it on a form with AutoScroll and ran the application.
The image displays, and I can scroll, but when I scroll I can see the image breakup as the screen is being redrawn.
IOW, it lags on my high performance pc. If you make a new windows forms project with the above mentioned setup and use a very large image and you'll see the same effect.
How can I scroll around the image smoothly? It doesn't have to be the same setup as above with a panel in a form, it just has to work.
I also tried just overriding the OnPaint method of the form, and drawing the portion of the image depending on the value of the scrollbars. I then called Invalidate() in the on scroll event handler. That was a terrible idea and is about ten times slower because now the entire form flashes when I try to draw the image, but at least I ruled it out as a viable alternative.
I even tried a directx implementation of the panel. It had no effect. I'm sure if I spend enough time playing with directx I can get the image scrolling smoothly, but I have a bad feeling I would have to abandon the whole windows forms interface altogether for a directx interface.
I really want to avoid that if I can.
I'm running out of ideas, can anybody think of a way to make the scrolling smooth without visible redraw effects (like when you scroll on a form with a solid color background)
Thanks,
-Sandra
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi!
I'm not sure weather this will show better performance but it's worth trying.
You can load the background image into picturebox and display a portion of it on the form in the Paint event.
//------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication4
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Pictu
private System.Windows.Forms.VScro
private System.Windows.Forms.HScro
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceM
this.Pic1 = new System.Windows.Forms.Pictu
this.vScrollBar1 = new System.Windows.Forms.VScro
this.hScrollBar1 = new System.Windows.Forms.HScro
this.SuspendLayout();
//
// Pic1
//
this.Pic1.Image = ((System.Drawing.Bitmap)(r
this.Pic1.Location = new System.Drawing.Point(248, 104);
this.Pic1.Name = "Pic1";
this.Pic1.Size = new System.Drawing.Size(208, 168);
this.Pic1.TabIndex = 0;
this.Pic1.TabStop = false;
this.Pic1.Visible = false;
//
// vScrollBar1
//
this.vScrollBar1.Location = new System.Drawing.Point(480, 0);
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(16, 304);
this.vScrollBar1.TabIndex = 1;
this.vScrollBar1.Scroll += new System.Windows.Forms.Scrol
//
// hScrollBar1
//
this.hScrollBar1.Location = new System.Drawing.Point(0, 304);
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(480, 16);
this.hScrollBar1.TabIndex = 2;
this.hScrollBar1.Scroll += new System.Windows.Forms.Scrol
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(498, 320);
this.Controls.AddRange(new
this.hScrollBar1,
this.vScrollBar1,
this.Pic1});
this.FormBorderStyle = System.Windows.Forms.FormB
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.F
this.Paint += new System.Windows.Forms.Paint
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
if (Pic1.Image.Width >= this.Width)
{
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = Pic1.Image.Width - Pic1.Width;
}
else
hScrollBar1.Enabled = false;
if (Pic1.Image.Height >= this.Height)
{
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = Pic1.Image.Height - Pic1.Height;
}
else
vScrollBar1.Enabled = false;
}
private void Form1_Paint(object sender, System.Windows.Forms.Paint
{
System.Drawing.Graphics background = this.CreateGraphics();
System.Drawing.Rectangle DestRect = new System.Drawing.Rectangle(0
background.DrawImage(Pic1.
}
private void hScrollBar1_Scroll(object sender, System.Windows.Forms.Scrol
{
Form1_Paint(hScrollBar1,ne
}
private void vScrollBar1_Scroll(object sender, System.Windows.Forms.Scrol
{
Form1_Paint(hScrollBar1,ne
}
}
}
//------------------------
Load an image into Pic1 at design time and run it
dbrckovi - this is exactly what I mentioned trying in my question above, and it was about 10 times worse :)
I'll play around today with alternatives, and I'll try the double buffering, as well as the LockWindowUpdate. I'll report back as I try things, if you think of something else for me to try, just post it here.
Thanks for your help,
-Sandra
Ok LockWindowUpdate seems only to be for preventing the control from drawing, it doesn't help with anything in the OnPaint method.
I set up a bitmap buffer, and I drew the section of the image to the buffer, and this works great. I tried drawing the image in the OnPaint method but this flickers. So I set the BackgroundImage property to the buffer in the constructor and call Invalidate() once I'm done drawing to the buffer. The flicker is gone. But it lags so badly when scrolling (visible jerkiness and 100% cpu utilization) that it's even worse.
I think it's time for me to break out directx.
-Sandra
For anybody who might read this later, if you have flickering or lag when painting a window and you can't get rid of it in .NET you can use C++ and draw to an off screen buffer then Blt the buffer to the screen in response to WM_PAINT message.
The process is described here:
http://msdn.microsoft.com/
Then you can create a managed C++ wrapper around your C++ window to bring it into C# or VB.NET.
Try this for a starting point:
http://www.thecodeproject.
-Sandra
Business Accounts
Answer for Membership
by: TheAvengerPosted on 2005-08-23 at 23:58:05ID: 14740171
I think it gets a little better if you use a picture box with SizeMode set to AutoSize instead of the panel. However it still flickers. I tried setting auto double buffering to the form and to the panel, but this does not help either. You can try playing a little more with double buffering but you may need to write more code youself. Here is an example:
oublebuffe r.htm
http://www.bobpowell.net/d