c#picturebox如何将尺寸较大的图片缩小
时间: 2023-10-27 22:07:21 浏览: 57
你可以使用 `System.Drawing.Image` 类的 `GetThumbnailImage()` 方法来缩小图片。
以下是一个简单的示例:
```csharp
// 加载原始图片
Image originalImage = Image.FromFile("original.jpg");
// 缩小图片
int newWidth = 500; // 新宽度
int newHeight = (int)(((float)newWidth / (float)originalImage.Width) * originalImage.Height); // 根据原始图片比例计算出新高度
Image thumbnailImage = originalImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
// 在 PictureBox 中显示缩小后的图片
pictureBox1.Image = thumbnailImage;
```
这样就可以将尺寸较大的图片缩小,并且显示在 `PictureBox` 控件中。
相关问题
c# PictureBox外形尺寸不变,图像随滚轮放大缩小
你可以通过以下步骤实现:
1. 在 `PictureBox` 的 `MouseWheel` 事件中,获取滚轮的滚动量 `delta`。
2. 根据 `delta` 的正负值,计算缩放比例。
3. 创建一个 `Bitmap` 对象,将 `PictureBox` 中的图像绘制到该对象中,并按照缩放比例进行缩放。
4. 将缩放后的图像设置为 `PictureBox` 的 `Image` 属性。
以下是一个示例代码:
```csharp
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
int delta = e.Delta;
float scaleFactor = delta > 0 ? 1.1f : 0.9f; // 正数放大,负数缩小
Bitmap bitmap = new Bitmap(pictureBox1.Image);
int width = (int)(bitmap.Width * scaleFactor);
int height = (int)(bitmap.Height * scaleFactor);
Bitmap scaledBitmap = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(scaledBitmap))
{
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(bitmap, new Rectangle(0, 0, width, height));
}
pictureBox1.Image = scaledBitmap;
}
```
请注意,此示例代码未进行越界检查,当图像缩放过大或过小时可能会出现异常。另外,为了获得更好的缩放效果,建议使用更高级别的插值算法(例如 `InterpolationMode.HighQualityBicubic`)。
c# picturebox 尺寸
在C#中,可以使用PictureBox的SizeMode属性来设置PictureBox的尺寸。根据提供的引用内容,可以看到在代码中使用了PictureBoxSizeMode.Zoom来设置图片自适应PictureBox的大小。这意味着加载的图片会根据PictureBox的大小进行缩放,以适应PictureBox的尺寸。[1]所以,当图片加载到PictureBox中时,它会自动调整大小以适应PictureBox的尺寸。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](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)