[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1313
  • Last Modified:

C# changing size of picturebox at runtime

Hello Experts,

C# beginner here.  I have several picturebox on a form.  What I would like to do is to change the size of the picturebox when the mouse is hovering over it and revert back to its original size when the mouse is leaving the picturebox area.


thanks

0
brdrok
Asked:
brdrok
  • 2
1 Solution
 
AaronReamsCommented:
Hi there,

This should do what you want.  Enjoy - Aaron


this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.MouseHover += new System.EventHandler(this.pictureBox1_MouseHover);
this.pictureBox1.MouseLeave += new System.EventHandler(this.pictureBox1_MouseLeave);


private void pictureBox1_MouseHover(object sender, System.EventArgs e)
{
    pictureBox1.Size=new Size(200,200);
}

private void pictureBox1_MouseLeave(object sender, System.EventArgs e)
{
    pictureBox1.Size=new Size(100,100);
}

0
 
aacoolCommented:
OK - here goes:
Add the following handlers to your pictureBox MouseEnter and MouseLeave events
      private Size m_origSize;
      void MainFrmpictureBox1MouseEnter(object sender, System.EventArgs e)
      {
            Size pbSize = MainFrmpictureBox1.Size;
                                m_origSize = pbSize; // store orig size for resetting size      
            Size pbNewSize = new Size(pbSize.Width+120,pbSize.Height+120);
            Image img = MainFrmpictureBox1.Image;
            helper.resizeImage(img,ref img,pbNewSize); //resize to new size
            MainFrmpictureBox1.Image = img; //reload image
            MainFrmpictureBox1.Size = pbNewSize;  //resize picturebox
      }
      void MainFrmpictureBox1MouseLeave(object sender, System.EventArgs e)
      {
            Size pbSize = MainFrmpictureBox1.Size;            
            Image img = MainFrmpictureBox1.Image;
            helper.resizeImage(img,ref img,m_origSize);//resize to orig size
            MainFrmpictureBox1.Image = img; //reload image
            MainFrmpictureBox1.Size = m_origSize; //resize picturebox                  
      }

helper is my ImageHelper class - an implementation of helper.resizeImage is:
      public void resizeImage(Image src_image,ref Image newImg,Size size)
      {
          Bitmap bitmap = new Bitmap(size.Width, size.Height,src_image.PixelFormat);
          Graphics new_g = Graphics.FromImage(bitmap);
           new_g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality;
            new_g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
               new_g.DrawImage(src_image, 0, 0, bitmap.Width, bitmap.Height);
                newImg = (Image)bitmap;
                new_g.Dispose();                  
      }
0
 
brdrokAuthor Commented:
thanks for such a quick feedback.....

C# is defn. very different from vb.  didn't knw one had to create a new size object to make this work.  one quick question...how is size different from the clientsize property?

thanks

0
 
AaronReamsCommented:
Size is the total area of the control.

ClientSize is the client area of a control which is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus.

Most of the time you will want to use Size not ClientSize.

But say for example,  you wanted your picturebox to take up the entire dialog.  You would do the following.

picturebox.Location = new Point(0,0);
picturebox.Size = form.ClientSize;

because if you did picturebox.Size = form.Size the picture box would be as big as the entire dialog which includes the titlebar when you really only want it to be as big as the client area.

hope that helps. good luck -aaron
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now