Windows Forms C#:禁用PictureBox的图片混合/抗锯齿效果
Windows Forms PictureBox取消低分辨率图片混合/抗锯齿方案
你之前的代码未生效,核心原因是PictureBox默认会自动绘制Image属性的图片,不会复用你在Paint事件中设置的Graphics参数。要实现无混合/抗锯齿的像素风格显示,需要手动接管图片绘制流程,具体步骤如下:
关键设置与代码实现
调整PictureBox属性
- 将
SizeMode设置为PictureBoxSizeMode.Normal,避免控件自动拉伸绘制图片 - 开启
DoubleBuffered(可通过代码设置,避免绘制时闪烁)
- 将
修改Paint事件代码
完全手动绘制图片,同时强制关闭插值平滑:
private void pb_Image_Paint(object sender, PaintEventArgs e) { if (pb_Image.Image == null) return; // 关闭所有平滑/混合效果 e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; // 手动将图片拉伸至PictureBox尺寸 e.Graphics.DrawImage(pb_Image.Image, pb_Image.ClientRectangle); }
- 可选:确保整数倍缩放
如果需要缩放图片,尽量让PictureBox的宽高是原图宽高的整数倍,避免半像素偏移导致的模糊感。
额外说明
如果你的PictureBox需要响应大小变化,可在Resize事件中触发重绘:
private void pb_Image_Resize(object sender, EventArgs e) { pb_Image.Invalidate(); }
内容的提问来源于stack exchange,提问作者justanotheruser
相关产品推荐
相关产品推荐

