相关疑难解决方法(0)

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

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

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

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

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

这是原始的10x10 8bpp图像: 10x10 8bpp测试图像

编辑-解决方案 对于某些ODD原因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(); …
Run Code Online (Sandbox Code Playgroud)

c# graphics transformation image

7
推荐指数
1
解决办法
1242
查看次数

标签 统计

c# ×1

graphics ×1

image ×1

transformation ×1