WinForm中实现picturebox自适应图片大小的方法

时间:2021-11-25 17:02:55

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

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

pictureBox1.BackgroundImage = ImagepictureBox1.load(url)

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
    }
  }
}

附:完整实例代码点击此处本站下载

希望本文所述对大家C#程序设计有所帮助。