作者:执信电影频道 | 来源:互联网 | 2023-05-17 23:33
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.
这可能是一个奇怪的问题,但是当我在C#中扩展我的图像时,我需要将其设置为像素化而不是消除锯齿。就像在缩放时的MSpaint一样。
I hope images anti-alias by default in C#, or else I changed something I didn't want to.
我希望在C#中默认图像反别名,否则我改变了我不想要的东西。
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:
我试过玩Graphics.InterpolationMode但没有运气。我正在使用Bitmap对象来保存图像,它的构造如下:
// 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.
有任何想法吗?谢谢。
2 个解决方案