图形变换缩放后,第一个像素列的一半丢失了。

7
我注意到,在OnPaint事件中进行图形变换缩放后,图像的第一个像素列的一半没有绘制出来。
要复现这个问题所需的所有代码都在帖子的末尾。基本上,我创建了一个从PictureBox派生的类,名为PictureBox2,并重写了OnPaint方法以执行缩放变换。它还将InterpolationMode更改为NearestNeighbor,以防止Graphics改变像素的外观。
PictureBox控件被添加到一个名为Form6_GraphicsTest的窗体中。该控件在所有边上都有锚定。PictureBox2的背景颜色被更改为蓝色,窗体的背景颜色被更改为深灰色。
正如您在下面的图像中所看到的,只有图像的第一个像素列的1/2被绘制出来。为什么会这样呢?我有什么遗漏吗?
以下是原始的10x10 8bpp图像:
编辑-解决方案 由于某种奇怪的原因,PixelOffsetMode.Default会消耗0.5个像素。解决方案:使用PixelOffsetMode.Half或HighQuality!

代码 PictureBox2.cs

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace GraphicsTest
{
    public class PictureBox2 : PictureBox
    {
        public PointF Zoom = new PointF(20, 20);
        private InterpolationMode interpolationMode = InterpolationMode.NearestNeighbor;

        /// <summary>
        /// Paint the image
        /// </summary>
        /// <param name="e">The paint event</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (IsDisposed)
                return;

            if (Image != null)
            {
                if (e.Graphics.InterpolationMode != interpolationMode)
                    e.Graphics.InterpolationMode = interpolationMode;

                using (Matrix transform = e.Graphics.Transform)
                {
                    //e.Graphics.ResetTransform();

                    if (Zoom.X != 1.0 || Zoom.Y != 1.0)
                        transform.Scale(Zoom.X, Zoom.Y, MatrixOrder.Append);

                    //if (ImageDisplayLocation.X != 0 || ImageDisplayLocation.Y != 0) //Convert translation back to display pixel unit.
                    //    transform.Translate(ImageDisplayLocation.X / Zoom.X, ImageDisplayLocation.Y / Zoom.Y);

                    e.Graphics.Transform = transform;
                }
            }

            base.OnPaint(e);

            //If you want to draw something over the control in control coordinate, you must first reset the transformation! :D
            //e.Graphics.ResetTransform();
            //Draw your stuff
        }
    }
}

代码 Form6_GraphicsTest.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicsTest
{
    public class Form6_GraphicsTest : Form
    {
        public Form6_GraphicsTest()
        {
        InitializeComponent();
        Bitmap bmp = new Bitmap(@"D:\Test 10x10.8bpp.png");
        this.pictureBox21.Image = bmp;

        this.pictureBox21.Zoom = new PointF(20,20);

        this.ClientSize = new Size(Convert.ToInt32(this.pictureBox21.Zoom.X * bmp.Width) + 30, Convert.ToInt32(this.pictureBox21.Zoom.Y * bmp.Height) + 30);
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox21 = new GraphicsTest.PictureBox2();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox21)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox21
            // 
            this.pictureBox21.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox21.BackColor = System.Drawing.SystemColors.Highlight;
            this.pictureBox21.Location = new System.Drawing.Point(12, 12);
            this.pictureBox21.Name = "pictureBox21";
            this.pictureBox21.Size = new System.Drawing.Size(260, 238);
            this.pictureBox21.TabIndex = 0;
            this.pictureBox21.TabStop = false;
            // 
            // Form6_GraphicsTest
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.pictureBox21);
            this.Name = "Form6_GraphicsTest";
            this.Text = "Form6_GraphicsTest";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox21)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private PictureBox2 pictureBox21;
    }
}
1个回答

10

是的,我只是在测试那个人的解决方案,它有效了(在你发帖之前哈哈)!谢谢! - Pedro77
1
因为某些奇怪的原因 PixelOffsetMode.Default 会吞噬 0.5 像素。解决方案是选择 PixelOffsetMode.Half 或 HighQuality。 - Pedro77
很高兴听到这个问题解决了。这绝对是其中一个图形上的惊喜,“难道它不能像我说的那样绘制我的像素吗??” - J Trana
PixelOffsetMode是“由方形、硬边缘像素组成的图形”(类似乐高积木)开始过渡到“由方形、软边缘的采样颜色(和alpha通道)组成的图形”的地方。当你把像素想象成可以套在一起的乐高积木时,定义缩放和混合就变得困难。而使用像素作为样本,更容易定义如何结合背景和前景,并按比例缩放图像以获得最终显示值。但这意味着你必须处理像素的中心(采样点)而不是边缘。 - Jason Harrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接