Solved

C# changing size of picturebox at runtime

Posted on 2004-10-18
4
1,278 Views
Last Modified: 2008-01-09
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
Comment
Question by:brdrok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 225 total points
ID: 12340314
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
 
LVL 3

Expert Comment

by:aacool
ID: 12340481
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
 
LVL 7

Author Comment

by:brdrok
ID: 12340510
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
 
LVL 8

Expert Comment

by:AaronReams
ID: 12340630
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question