正文  电脑教程 > 服务器教程 >

WinForm中实现picturebox自适应图片大小教程

本文实例讲述了WinForm中实现picturebox自适应图片大小的方法。分享给大家供大家参考,具体如下: picturebox控件共有两种载入图片...

本文实例讲述了WinForm中实现picturebox自适应图片大小的方法。分享给大家供大家参考,具体如下:

picturebox控件共有两种载入图片方式,分别为:

pictureBox1.BackgroundImage = ImagepictureBox1.load(url)

为使加载的图片自使用控件尺寸,可以分别对pictureBox控件设置BackGroundImageLayout=StretchSizeMode=StretchImagewinform中picturebox自适应图片大小

做了一个示例,代码如下,后面附完整项目源码下载:

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("何问起",ex.Message); }
    }
    private void button_hovertreeZoom_Click(object sender, EventArgs e)
    {
      //图像大小按其原有的大小比例被增加或减少 by 何问起
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.Zoom;
    }
    private void button_HovertreeStretch_Click(object sender, EventArgs e)
    {
      //PictureBox 中的图像被拉伸或收缩,以适应PictureBox的大小。 by 何问起
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.StretchImage;
    }
    private void s_Click(object sender, EventArgs e)
    {
      //调整PictureBox的大小,使其等于所包含图像的大小 by 何问起
      pictureBox_HoverTree.SizeMode = PictureBoxSizeMode.AutoSize;
    }
  }
}