[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# changing size of picturebox at runtime

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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