先前的答案虽然是出于好意,但只是部分正确。
什么是正确的?PictureBox 不公开 InterpolationMode。
出了什么问题?
1) 虽然您可以轻松地从图片框、其父级或通过派生类中的覆盖在 Paint 事件中设置该属性。. . 无论哪种方式都有效,两者都一样容易。但是,除非设置 SmoothingMode,否则 InterpolationMode 将被忽略。如果将 SmoothingMode 设置为 SmoothingMode.AnitAlias,您将不会获得任何抗锯齿。
2) 当您明确表示有兴趣使用 PictureBox 的功能时使用面板是错误的方向。您将无法直接加载、保存或分配图像,而无需显式编码这些属性。. . 为什么要重新发明轮子? 通过从 PictureBox 派生,您可以免费获得所有这些。
消息变得更好,因为我已经为您完成了艰苦的工作,而且我花费的时间比写此消息要少。
我提供了两个版本,它们都来自 PictureBox。首先是一个简单的示例,它始终使用可能的最佳质量渲染。这也是最慢的渲染。第二个是允许任何人通过派生类的属性设置各种渲染参数的类。一旦设置,这些将在 OnPaint 覆盖中使用。
public class HighQualitySmoothPictureBox : PictureBox
{
protected override void OnPaint(PaintEventArgs pe)
{
// This is the only line needed for anti-aliasing to be turned on.
pe.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// the next two lines of code (not comments) are needed to get the highest
// possible quiality of anti-aliasing. Remove them if you want the image to render faster.
pe.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
pe.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// this line is needed for .net to draw the contents.
base.OnPaint(pe);
}
}
...
public class ConfigurableQualityPictureBox : PictureBox
{
// Note: the use of the "?" indicates the value type is "nullable."
// If the property is unset, it doesn't have a value, and therefore isn't
// used when the OnPaint method executes.
System.Drawing.Drawing2D.SmoothingMode? smoothingMode;
System.Drawing.Drawing2D.CompositingQuality? compositingQuality;
System.Drawing.Drawing2D.InterpolationMode? interpolationMode;
public System.Drawing.Drawing2D.SmoothingMode? SmoothingMode
{
get { return smoothingMode; }
set { smoothingMode = value; }
}
public System.Drawing.Drawing2D.CompositingQuality? CompositingQuality
{
get { return compositingQuality; }
set { compositingQuality = value; }
}
public System.Drawing.Drawing2D.InterpolationMode? InterpolationMode
{
get { return interpolationMode; }
set { interpolationMode = value; }
}
protected override void OnPaint(PaintEventArgs pe)
{
if (smoothingMode.HasValue)
pe.Graphics.SmoothingMode = smoothingMode.Value;
if (compositingQuality.HasValue)
pe.Graphics.CompositingQuality = compositingQuality.Value;
if (interpolationMode.HasValue)
pe.Graphics.InterpolationMode = interpolationMode.Value;
// this line is needed for .net to draw the contents.
base.OnPaint(pe);
}
}