C#pic加载图片并填满控件显示
时间: 2023-12-13 20:09:13 浏览: 47
在 C# 中,可以使用 `PictureBox` 控件来加载和显示图片。以下是一些示例代码,以将图片加载到 `PictureBox` 控件中并使其填充控件显示区域。
1. 使用 `Image.FromFile` 方法加载图片文件,然后将其设置为 `PictureBox` 控件的 `Image` 属性。
```csharp
string imagePath = @"C:\path\to\image.jpg";
Image image = Image.FromFile(imagePath);
pictureBox1.Image = image;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; // 图片填充控件显示区域
```
2. 使用 `Image.FromStream` 方法加载图片流,然后将其设置为 `PictureBox` 控件的 `Image` 属性。
```csharp
string imagePath = @"C:\path\to\image.jpg";
using (FileStream stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
Image image = Image.FromStream(stream);
pictureBox1.Image = image;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; // 图片填充控件显示区域
}
```
在上述示例代码中, `pictureBox1` 是 `PictureBox` 控件的实例,`SizeMode` 属性设置为 `PictureBoxSizeMode.StretchImage`,可以使图片填充控件显示区域。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)