PictureBox
1.Bitmap
Bitmap是位图对象, 用于处理由像素数据定义的图像的对象。Bitmap继承于Image
c#
public sealed class Bitmap : System.Drawing.Image继承关系
Object=>MarshalByRefObject=>Image=>Bitmap
1.1 构造函数
| 构造函数重载 | 描述 |
|---|---|
| Bitmap(Image) | 从指定的现有图像初始化 Bitmap 类的新实例。 |
| Bitmap(Image, Int32, Int32) | 从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。 |
| Bitmap(Image, Size) | 从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。 |
| Bitmap(Int32, Int32) | 用指定的大小初始化 Bitmap 类的新实例。 |
| Bitmap(Int32, Int32, Graphics) | 用指定的大小和指定的 Graphics 对象的分辨率初始化 Bitmap 类的新实例。 |
| Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) | 用指定的大小、像素格式和像素数据初始化 Bitmap 类的新实例。 |
| Bitmap(Int32, Int32, PixelFormat) | 用指定的大小和格式初始化 Bitmap 类的新实例。 |
| Bitmap(Stream) | 从指定的数据流初始化 Bitmap 类的新实例。 |
| Bitmap(Stream, Boolean) | 从指定的数据流初始化 Bitmap 类的新实例。 |
| Bitmap(String) | 从指定的文件初始化 Bitmap 类的新实例。 |
| Bitmap(String, Boolean) | 从指定的文件初始化 Bitmap 类的新实例。 |
| Bitmap(Type, String) | 从指定的资源初始化 Bitmap 类的新实例。 |
1.2 属性
| 属性 | 描述 |
|---|---|
| Flags | 获取该 Image 的像素数据的特性标志。(继承自 Image) |
| FrameDimensionsList | 获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数。(继承自 Image) |
| Height | 获取此 Image 的高度(以像素为单位)。(继承自 Image) |
| HorizontalResolution | 获取此 Image 的水平分辨率(以“像素/英寸”为单位)。(继承自 Image) |
| Palette | 获取或设置用于此 Image 的调色板。(继承自 Image) |
| PhysicalDimension | 获取此图像的宽度和高度。(继承自 Image) |
| PixelFormat | 获取此 Image 的像素格式。(继承自 Image) |
| PropertyIdList | 获取存储于该 Image 中的属性项的 ID。(继承自 Image) |
| PropertyItems | 获取存储于该 Image 中的所有属性项(元数据片)。(继承自 Image) |
| RawFormat | 获取此 Image 的文件格式。(继承自 Image) |
| Size | 获取此图像的宽度和高度(以像素为单位)。(继承自 Image) |
| Tag | 获取或设置提供有关图像的附加数据的对象。(继承自 Image) |
| VerticalResolution | 获取此 Image 的垂直分辨率(以“像素/英寸”为单位)。(继承自 Image) |
| Width | 获取此 Image 的宽度(以像素为单位)。(继承自 Image) |
1.3 方法
2.PictureBox
PictureBox是图像框控件,用来显示一个图像,如下所示

2.1 属性
| 属性名 | 数据类型 | 可选值 | 默认值 | 描述 |
|---|---|---|---|---|
| SizeMode | enum | PictureBoxSizeMode.Normal | ||
| PictureBoxSizeMode.StretchImage | ||||
| PictureBoxSizeMode. AutoSize | ||||
| PictureBoxSizeMode.CenterImage | ||||
| PictureBoxSizeMode.Zoom | Normal | 设置图片的显示尺寸模式 Normal:调整 PictureBox 大小,使其等于所包含的图像大小。 StretchImage:如果 PictureBox 比图像大,则图像将居中显示。 如果图像比 PictureBox 大,则图片将居于 PictureBox 中心,而外边缘将被剪裁掉。 AutoSize:图像被置于 PictureBox 的左上角。 如果图像比包含它的 PictureBox 大,则该图像将被剪裁掉。 CenterImage:PictureBox 中的图像被拉伸或收缩,以适合 PictureBox 的大小。 Zoom:图像大小按其原有的大小比例被增加或减小。 |
3.显示图像
3.1 使用Bitmap
使用Bitmap加载一个图片,赋值给pictureBox1的Image属性
c#
Bitmap img = new Bitmap("D:\\img.png");
pictureBox1.Image = img;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;3.2 使用Load方法
c#
pictureBox1.Load("D:\\img.png");
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;WARNING
注意图片的路径,分隔符要用反斜杠\,而且要用两个,第一个用来转义
txt
"D:\\img\\img.png" √
"D:/img/img.png" ×3.3 编辑器添加
在编辑器中找到Image属性,打开之后可以导入图片

