本课学习目标
- 认识 Windows Forms 的 PictureBox 控件。
- 使用 Image 属性显示图片。
- 理解 SizeMode 的 Normal、StretchImage、AutoSize、CenterImage 与 Zoom。
- 使用 Image.FromFile 从外部文件加载图片。
- 使用 File.Exists 检查图片路径。
- 认识 Visual Studio 项目的 Resources。
- 通过 My.Resources 使用嵌入图片。
- 理解外部图片与嵌入资源的差别。
- 正确替换和释放旧 Image,避免文件被长期占用。
- 完成学生照片查看器。
18.1 PictureBox 是什么?
PictureBox 是 Windows Forms 中专门用于显示图片的控件。
从 Toolbox 把 PictureBox 拖到 Form 后,可以在 Properties Window 设置:
- Name
- Image
- Size
- BorderStyle
- SizeMode
18.2 PictureBox.Image
PictureBox 的 Image 属性保存当前显示的图片。
picStudent.Image = My.Resources.StudentPlaceholder
要清除图片:
picStudent.Image = Nothing
18.3 SizeMode:图片如何适应 PictureBox?
例如:
picStudent.SizeMode =
PictureBoxSizeMode.Zoom
18.4 从图片文件加载
如果已经知道图片路径,可以使用:
Dim imagePath As String =
txtImagePath.Text.Trim()
picStudent.Image =
Image.FromFile(imagePath)
但在真正加载前应该先确认文件存在:
If Not File.Exists(imagePath) Then
MessageBox.Show("找不到图片文件。")
Exit Sub
End If
因此 Form1 顶部需要:
Imports System.IO
18.5 常见图片文件
Windows Forms 的 Image / Bitmap 可以处理常见位图格式,例如:
.jpg / .jpeg
常用于照片,压缩率高。
.png
适合 Logo、图标和带透明背景的图片。
.bmp
传统位图格式,文件通常较大。
18.6 什么是项目 Resources?
项目资源可以把图片、字符串等内容嵌入应用中。对于固定素材,例如 Logo、默认头像和工具栏图标,这通常比依赖外部文件路径更方便。
18.7 把图片加入项目资源
在 Visual Studio 中,可以通过项目的资源管理界面加入图片资源。加入后为图片设一个清楚的资源名称,例如:
StudentPlaceholder
之后即可在代码中使用:
picStudent.Image =
My.Resources.StudentPlaceholder
18.8 外部文件还是项目资源?
适合用户照片、产品上传图片、可替换内容。文件可以独立于应用更新。
适合 Logo、默认图片、按钮图标等固定素材,会随应用一起发布。
| 需求 | 外部文件 | Resources |
|---|---|---|
| 用户头像 | 很适合 | 通常不适合 |
| 公司 Logo | 可以 | 很适合 |
| 用户可替换图片 | 很适合 | 不方便 |
| 发布时不想依赖额外图片文件 | 较麻烦 | 很适合 |
18.9 替换图片时释放旧 Image
如果程序不断从文件加载不同图片,建议在替换前释放旧图片:
If picStudent.Image IsNot Nothing Then
picStudent.Image.Dispose()
picStudent.Image = Nothing
End If
然后再加载新图片。
18.10 更稳妥地从文件复制图片
一种更稳妥的做法是先读取图片,再建立独立 Bitmap:
Private Function LoadImageCopy(
imagePath As String) As Image
Using sourceImage As Image =
Image.FromFile(imagePath)
Return New Bitmap(sourceImage)
End Using
End Function
调用:
picStudent.Image =
LoadImageCopy(imagePath)
这样返回的新 Bitmap 不再直接依赖原文件对象。
18.11 实作:学生照片查看器
建立新的 Windows Forms 项目:
StudentPhotoViewer2026
本项目让用户输入学生姓名和图片文件路径,然后加载照片;还可以恢复项目内置的默认头像。
SizeMode = Zoom
学生照片显示区域
加入以下控件:
| 控件 | Name | Text / 设置 |
|---|---|---|
| TextBox | txtStudentName | 输入学生姓名 |
| TextBox | txtImagePath | 输入图片完整路径 |
| PictureBox | picStudent | SizeMode = Zoom;BorderStyle = FixedSingle |
| Button | btnLoadImage | Text = 加载图片 |
| Button | btnDefaultImage | Text = 默认头像 |
| Button | btnClear | Text = 清除 |
| Label | lblStatus | 显示当前状态 |
先加入默认头像资源
把一张默认头像加入项目 Resources,并命名为:
StudentPlaceholder
Form1.vb 顶部
Imports System.IO
Public Class Form1
图片复制方法
Private Function LoadImageCopy(
imagePath As String) As Image
Using sourceImage As Image =
Image.FromFile(imagePath)
Return New Bitmap(sourceImage)
End Using
End Function
释放 PictureBox 旧图片
Private Sub ClearCurrentImage()
If picStudent.Image IsNot Nothing Then
picStudent.Image.Dispose()
picStudent.Image = Nothing
End If
End Sub
加载图片按钮
Private Sub btnLoadImage_Click(
sender As Object,
e As EventArgs) Handles btnLoadImage.Click
Dim studentName As String =
txtStudentName.Text.Trim()
Dim imagePath As String =
txtImagePath.Text.Trim()
If String.IsNullOrWhiteSpace(studentName) Then
MessageBox.Show("请输入学生姓名。")
txtStudentName.Focus()
Exit Sub
End If
If String.IsNullOrWhiteSpace(imagePath) Then
MessageBox.Show("请输入图片路径。")
txtImagePath.Focus()
Exit Sub
End If
If Not File.Exists(imagePath) Then
MessageBox.Show("找不到图片文件。")
txtImagePath.Focus()
Exit Sub
End If
ClearCurrentImage()
picStudent.Image =
LoadImageCopy(imagePath)
picStudent.SizeMode =
PictureBoxSizeMode.Zoom
lblStatus.Text =
$"已加载 {studentName} 的照片。"
End Sub
显示默认头像
Private Sub btnDefaultImage_Click(
sender As Object,
e As EventArgs) Handles btnDefaultImage.Click
ClearCurrentImage()
picStudent.Image =
New Bitmap(
My.Resources.StudentPlaceholder)
picStudent.SizeMode =
PictureBoxSizeMode.Zoom
lblStatus.Text =
"已显示默认头像。"
End Sub
清除按钮
Private Sub btnClear_Click(
sender As Object,
e As EventArgs) Handles btnClear.Click
ClearCurrentImage()
txtStudentName.Clear()
txtImagePath.Clear()
lblStatus.Text = ""
txtStudentName.Focus()
End Sub
18.12 程序执行流程
Form1 先做空白检查。
确认图片真的存在。
避免不断替换时留下旧 Image 对象。
LoadImageCopy 从文件生成独立图片对象。
保持图片比例并适应显示区域。
18.13 也可以在设计阶段设置 Image
如果图片固定不变,例如顶部 Logo,可以在 Designer 的 PictureBox Image 属性中直接选择项目资源,而不一定需要写代码。
18.14 初学者常见错误
图片被拉伸变形
检查 SizeMode。照片通常使用 Zoom 比 StretchImage 更自然。
图片路径不存在
从外部文件加载前先使用 File.Exists。
资源名称写错
My.Resources.StudentPlaceholder 必须与项目资源名称一致。
替换图片时没有释放旧图片
长期运行并频繁换图时,应 Dispose 不再使用的 Image。
18.15 小练习
- 把 PictureBox 的 SizeMode 分别改成 Normal、StretchImage、CenterImage、Zoom,比较效果。
- 加入第二张项目资源图片,例如 CompanyLogo,并显示在另一个 PictureBox。
- 新增“清除照片”按钮,只清除 PictureBox,不清除姓名。
- 新增 Label 显示图片原始宽度与高度:picStudent.Image.Width 和 Height。
- 让默认头像在 Form.Load 时自动出现。
- 测试加载 JPG 与 PNG,并比较透明背景显示效果。
本课复习
- PictureBox 的主要用途是什么?
- Image 属性保存的是什么?
- 为什么照片通常适合使用 PictureBoxSizeMode.Zoom?
- Image.FromFile 的作用是什么?
- 为什么加载图片前要检查 File.Exists?
- 什么类型的图片适合放入项目 Resources?
- My.Resources 用来做什么?
- 外部图片与嵌入资源最大的区别是什么?
- 为什么替换 PictureBox.Image 前应考虑 Dispose 旧图片?
- LoadImageCopy 为什么建立一个新的 Bitmap?