Solved

C# changing size of picturebox at runtime

Posted on 2004-10-18
4
1,245 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now