0

I have got problem with an Picturebox(picWorld). When I want to change its size and location like this code, It took much time because I think it is updating twice:

private void lblWorld_MouseEnter(object sender, EventArgs e)
{
    picWorld.Size = new Size(148, 148);
    picWorld.Location = new Point(picWorld.Location.X - 12, picWorld.Location.Y - 12);
}

Is there any way to make it faster?

EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • Why don't you change the .Width, .Height, .Left and .Top properties instead? That said, how slow is it? – ainwood Aug 17 '17 at 08:23
  • That code is not slow. To ensure that *painting* the image does not take so much time that it gets noticeable, you must pay attention to the bitmap you use. It should be exactly the correct size to fit the pbox so it does not need to be interpolated. If necessary keep multiple copies of the bitmap around, each pre-sized to match the Size you assign. And its pixel format is *very* important, only 32bppPArgb is fast. – Hans Passant Aug 17 '17 at 08:57
  • Try "picWorld.SetBounds()" method. – DotNet Developer Aug 17 '17 at 11:13

2 Answers2

0

Try hiding it, moving it, then showing it:

private void lblWorld_MouseEnter(object sender, EventArgs e)
{
    picWorld.Hide();
    picWorld.Size = new Size(148, 148);
    picWorld.Location = new Point(picWorld.Location.X - 12, picWorld.Location.Y - 12);
    picWorld.Show();
}
ainwood
  • 992
  • 7
  • 17
  • That's fast but the PictureBox is just Hiding and Showing when the mouse is over the lblWorld. –  Aug 18 '17 at 10:32
  • Is it not resizing and moving it like you want? What is it not doing? I'm struggling to understand the problem, and perhaps it might help if you provide more information in your post (above). Perhaps look at the comments from @Hans Passant if you want a really high performance bitmap. – ainwood Aug 18 '17 at 19:47
-1

create an outside variable (prefer static) to store when to start and when to end, so:

bool now=false;

private void lblWorld_MouseEnter(object sender, EventArgs e)
{
if (!now)
{
now = true;
    picWorld.Size = new Size(148, 148);
    picWorld.Location = new Point(picWorld.Location.X - 12,     picWorld.Location.Y - 12);
now = false;
}

}

  • The problem is that when I change the Size, the Image invalidate. And when the Location changed, it invalidate again. –  Aug 17 '17 at 08:29