您的位置:首页 - 教程 - WinForm - 正文
winform中picturebox自适应图片大小

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

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

为使加载的图片自使用控件尺寸,可以分别对pictureBox控件设置BackGroundImageLayout=Stretch,SizeMode=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;

}
}
}


源码下载:http://hovertree.com/h/bjaf/4hftwbhv.htm

评论:
  • 2016-11-02 10:34 太麻烦了吧
  • 2016-11-03 15:51 为了方便体验效果,制作了一个小示例,附源码下载