using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp.Size));
}
或创建一个后继PictureBox-a 并覆盖OnPaint:
public class CustomPictureBox : PictureBox
{
private System.Drawing.Drawing2D.InterpolationMode interpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
public System.Drawing.Drawing2D.InterpolationMode InterpolationMode
{
get => interpolationMode;
set
{
interpolationMode = value;
this.Invalidate(); //перерисовываем
}
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.InterpolationMode = interpolationMode;
base.OnPaint(pe);
}
}
为了使缩放时图像看起来不模糊,您需要更改插值模式:
Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;您可以在渲染图像时手动执行此操作:
或创建一个后继
PictureBox-a 并覆盖OnPaint: