简体   繁体   中英

C# Resize Image with No Interpolation

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 what I want:

这是我需要的

This is what I get:

这是我的应用程序所得到的

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);
}

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.

Any ideas?

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?

Here is the effect:

在此处输入图片说明

Hope that will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM