我需要在 C# 中为缩放后的图像设置别名
这可能是一个奇怪的问题,但是当我在 C# 中缩放图像时,我需要将其像素化而不是抗锯齿。 就像在 MSpaint 中缩放一样。
我希望 C# 中的图像默认具有抗锯齿功能,否则我会更改一些我不想更改的内容。
我尝试过使用 Graphics.InterpolationMode 但没有运气。 我使用 Bitmap 对象来保存图像,它的构造如下:
// A custom control holds the image
this.m_ZoomPanPicBox.Image = new Bitmap(szImagePath);
以及自定义控件的简要概要:
class ZoomPanPicBox : ScrollableControl
{
Image m_image;
float m_zoom = 1.0f;
InterpolationMode m_interpolationMode;
...
////////////////////////////////////////////////////////
public ZoomPanPicBox()
{
//Double buffer the control
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.AutoScroll=true;
}
////////////////////////////////////////////////////////
protected override void OnPaint(PaintEventArgs e)
{
//if no image, don't bother
if(m_image==null)
{
base.OnPaintBackground(e);
return;
}
//Set up a zoom matrix
Matrix mx = new Matrix(m_zoom,0,0,m_zoom,0,0);
//now translate the matrix into position for the scrollbars
mx.Translate(this.AutoScrollPosition.X / m_zoom, this.AutoScrollPosition.Y / m_zoom);
//use the transform
e.Graphics.Transform = mx;
//and the desired interpolation mode
e.Graphics.InterpolationMode = m_interpolationMode;
//Draw the image ignoring the images resolution settings.
e.Graphics.DrawImage(m_image,new Rectangle(0,0,this.m_image.Width,this.m_image.Height),0,0,m_image.Width, m_image.Height,GraphicsUnit.Pixel);
base.OnPaint(e);
}
有什么想法吗? 谢谢。
This might be an odd question, but when I scale my image in C# I need it to be pixelated and not anti-aliased. Just like in MSpaint when you scale.
I hope images anti-alias by default in C#, or else I changed something I didn't want to.
I've tried playing around with the Graphics.InterpolationMode
but no luck there. I'm using a Bitmap object to hold the image and it's being constructed like so:
// A custom control holds the image
this.m_ZoomPanPicBox.Image = new Bitmap(szImagePath);
And a brief synapsis of the custom control:
class ZoomPanPicBox : ScrollableControl
{
Image m_image;
float m_zoom = 1.0f;
InterpolationMode m_interpolationMode;
...
////////////////////////////////////////////////////////
public ZoomPanPicBox()
{
//Double buffer the control
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.AutoScroll=true;
}
////////////////////////////////////////////////////////
protected override void OnPaint(PaintEventArgs e)
{
//if no image, don't bother
if(m_image==null)
{
base.OnPaintBackground(e);
return;
}
//Set up a zoom matrix
Matrix mx = new Matrix(m_zoom,0,0,m_zoom,0,0);
//now translate the matrix into position for the scrollbars
mx.Translate(this.AutoScrollPosition.X / m_zoom, this.AutoScrollPosition.Y / m_zoom);
//use the transform
e.Graphics.Transform = mx;
//and the desired interpolation mode
e.Graphics.InterpolationMode = m_interpolationMode;
//Draw the image ignoring the images resolution settings.
e.Graphics.DrawImage(m_image,new Rectangle(0,0,this.m_image.Width,this.m_image.Height),0,0,m_image.Width, m_image.Height,GraphicsUnit.Pixel);
base.OnPaint(e);
}
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您对 InterpolationMode 的看法是正确的,正如文档所说的。 只需将其设置为 InterpolationMode.NearestNeighbor 即可。 在您的代码示例中,您从未设置 m_interpolationMode。
Actually, you're right with InterpolationMode, as the docs say. Just set it to InterpolationMode.NearestNeighbor. In your code sample, you never set m_interpolationMode.
好吧,你可以自己实现比例并做一个简单的线性插值(即不做任何像双三次的邻居平均)......那些看起来不错而且块状。
Well, you could implement the scale yourself and do a simple linear interpolation (I.E. don't do any neighbor averaging like bicubic)... Those look nice and blocky.