C#调整图像大小而无需插值 [英] C# Resize Image with No Interpolation
问题描述
我知道这个问题经常被问到,但是没有一个典型的答案能给我我所需要的结果.我正在尝试像Paint.exe一样缩放灰度位图.我不希望插值,因此可以观察到原始的单个像素.我已经尝试了经常建议使用的NearestNeighbor方法,这种方法很接近,但并不是我想要的.
I know this question has been asked frequently, but none of the typical answers have given me the result I need. I am attempting to zoom a grayscale bitmap much like Paint.exe. I want no interpolation so the original, individual pixels can be observed. I have tried the oft-suggested NearestNeighbor approach which gets close, but not exactly what I want.
这就是我想要的:
这就是我得到的:
这是我用来缩放和重绘图像的代码.
This is the code I am using to zoom and redraw the image.
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
e.Graphics.SmoothingMode = SmoothingMode.None;
Matrix m = new Matrix();
m.Scale(mScale, mScale, MatrixOrder.Append);
e.Graphics.Transform = m;
e.Graphics.TranslateTransform(this.AutoScrollPosition.X / mScale,this.AutoScrollPosition.Y / mScale);
if (mImage != null)
e.Graphics.DrawImage(mImage, 0, 0);
base.OnPaint(e);
}
当缩放起作用时,代码确实对图像有影响,更改InterpolationMode确实会更改图像.但是,没有任何设置组合可以得到我需要的结果.
The code does have an affect on the image as the zoom works and changing the InterpolationMode does change the image. However, no combination of settings gets the result I need.
有什么想法吗?
推荐答案
我正在尝试使用C#显示16位图像.我遇到了同样的麻烦,即完全相同的像素中的颜色不相同.最后,我用下面的示例代码解决了这一麻烦:
I'm trying to display 16-bit images with C#. I came across the same trouble that the colors in the totally same pixel is not the same. Finally I solved this trouble with the sample code below:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
e.Graphics.Clear(Color.Black); //Clear the background with the black color
if(m_bmp != null) //if m_bmp == null, then do nothing.
{
//To calculate the proper display area's width and height that fit the window.
if (this.Width / (double)this.Height > m_bmp.Width / (double)m_bmp.Height)
{
m_draw_height = (int)(this.Height * m_roomRatio);
m_draw_width = (int)(m_bmp.Width / (double)m_bmp.Height * m_draw_height);
}
else
{
m_draw_width = (int)(this.Width * m_roomRatio);
m_draw_height = (int)(m_bmp.Height / (double)m_bmp.Width * m_draw_width);
}
//To calculate the starting point.
m_draw_x = (int)((this.Width - m_draw_width) / 2.0 + m_offsetX / 2.0);
m_draw_y = (int)((this.Height - m_draw_height) / 2.0 + m_offsetY / 2.0);
e.Graphics.DrawImage(m_bmp, m_draw_x, m_draw_y, m_draw_width, m_draw_height);
//draw some useful information
string window_info = "m_draw_x" + m_draw_x.ToString() + "m_draw_width" + m_draw_width.ToString();
e.Graphics.DrawString(window_info, this.Font, new SolidBrush(Color.Yellow), 0, 20);
}
顺便说一句,为什么不尝试使用双缓冲区来提高绘图图像的性能?
BTW, why don't you try to use double buffer to increase the performance of drawing images?
这里是效果:
希望会有所帮助.
这篇关于C#调整图像大小而无需插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!