c# – 在图形变换比例后缺少第一个像素列的一半

我注意到在OnPaint事件上的图形转换比例之后,不会绘制图像的第一个像素列的一半.

重现它所需的所有代码都在帖子的末尾.基本上我已经创建了一个派生自PictureBox的类,名为PictureBox2,它会覆盖OnPaint方法来执行Scale转换.它还将InterpolationMode更改为NearestNeighbor以防止Graphics更改像素外观.

PictureBox控件已添加到名为Form6_GraphicsTest的Form中.控制在所有方面都是固定的. PictureBox2背面颜色更改为蓝色,Form返回颜色为深灰色.

正如您在下面的图像中看到的那样,只绘制了图像第一个像素列的1/2.为什么??我在这里遗漏了什么?

这是原始的10×10 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;
    }
}

解决方法

它可能与 PixelOffsetMode有关吗?这可能与其他 post有关.它与渲染效率有关…

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么