Method for realizing picturebox self adaptive picture size in WinForm

  • 2021-12-13 09:04:58
  • OfStack

In this paper, an example is given to describe the method of realizing picturebox adaptive picture size in WinForm. Share it for your reference, as follows:

The picturebox control has two ways to load pictures:

pictureBox1.BackgroundImage = Image , pictureBox1.load(url)

To make the loaded picture self-use the control size, you can set the pictureBox control separately BackGroundImageLayout=Stretch , SizeMode=StretchImagewinform picturebox adaptive picture size in

Do an example, the code is as follows, followed by a complete project source code download:


using System;
using System.Windows.Forms;
namespace HoverTreePictureBox
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      WindowState = FormWindowState.Maximized;
    }
    private void button_getPicture_Click(object sender, EventArgs e)
    {
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.StretchImage;
      pictureBox_HoverTree.BackgroundImageLayout = ImageLayout.Stretch;
      try
      {
        pictureBox_HoverTree.Load("http://hovertree.com/hvtimg/bjafjc/rgevo2ea.jpg");
      }
      catch (Exception ex){ MessageBox.Show(" He asked ",ex.Message); }
    }
    private void button_hovertreeZoom_Click(object sender, EventArgs e)
    {
      // The image size is increased or decreased in proportion to its original size  by  He asked 
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.Zoom;
    }
    private void button_HovertreeStretch_Click(object sender, EventArgs e)
    {
      //PictureBox  The image in is stretched or contracted to fit PictureBox The size of.  by  He asked 
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.StretchImage;
    }
    private void s_Click(object sender, EventArgs e)
    {
      // Adjustment PictureBox Make it equal to the size of the included image  by  He asked 
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.AutoSize;
    }
  }
}

Attachment: Click here to download the complete example code.

For more readers interested in C # related content, please check the topics on this site: "WinForm Control Usage Summary", "C # Form Operation Skills Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: