?
Solved

C# changing size of picturebox at runtime

Posted on 2004-10-18
4
Medium Priority
?
1,295 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 900 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

770 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