Skip to main content
 首页 » 编程设计

c#之如果图片可以从 10x10 到 500x500,如何在 PictureBox 中显示图像

2024年02月05日11artech

现在我使用下一个代码将图片从 desctop 下载到 PictureBox

        OpenFileDialog openFileDialog = new OpenFileDialog(); 
        openFileDialog.Filter = "BMP|*.bmp"; 
        if (openFileDialog.ShowDialog() != DialogResult.OK) 
            return; 
 
        pictureBox.Load(openFileDialog.FileName); 
        pictureBox.SizeMode=PictureBoxSizeMode.StretchImage; 
        pictureBox.BorderStyle = BorderStyle.Fixed3D; 

如果我使用普通图片 (100x100),它看起来不错(不模糊)。

如果我使用小图片(15x15),它看起来很模糊。

我想知道如何让它们不模糊(它们可以看起来像砖头,但它们需要不模糊);

小图的等待结果需要这样

正常图片的等待结果需要是这样的

请您参考如下方法:

模糊度来自image interpolation .要获得所需的 block 状外观,您需要使用 nearest neighbour interpolation .

值得庆幸的是,.NET 具有此功能。您可以将插值模式设置为呈现您的 PictureBox 的 GDI Graphics 对象。

为此,我建议扩展 PictureBox 用户控件并添加 InterpolationMode范围。然后,您可以覆盖 OnPaint方法,并使用您的插值模式。

public class InterpolatingPictureBox : PictureBox 
{ 
    public InterpolationMode InterpolationMode { get; set; } 
 
    protected override void OnPaint(PaintEventArgs eventArgs) 
    { 
        eventArgs.Graphics.InterpolationMode = InterpolationMode; 
        base.OnPaint(eventArgs); 
    } 
} 

这是一张 16x16 的图片: , 使用

放大到 256x256
  • 最近邻(左)
  • 双线性(默认,中间)
  • HighQualityBilinear(右)

所有内容都呈现在 InterpolatingPictureBox 控件上。