English 中文(简体)
Strange flickering using ImageAttributes ColorMatrix in .NET on Win7 x64
原标题:
  • 时间:2009-11-09 16:09:35
  •  标签:
  • .net
  • drawing

When alpha blending some greyscale images on Win7 x64 I m getting strange tearing and flicker. It s fine on XP & Vista (both x64 and x86) and Win7 x86. I ve tried it on several Win7 machines, and they all show the problem.

The code that shows up the problem is:

public partial class Form1 : Form
{
    Image image;
    float alpha = 0;
    float delta = 0.1f;
    Timer timer;

    public Form1()
    {
        InitializeComponent();
        DoubleBuffered = true;
        Paint += Form1_Paint;

        image = Image.FromFile(@"image.jpg");

        timer = new Timer {Interval = 50};
        timer.Tick += timer_Tick;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        if (alpha > 1 || alpha < 0)
        {
            delta = -delta;
        }

        alpha += delta;
        Invalidate();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, image.Width, image.Height));
        float[][] matrix = {
                                    new float[] {1, 0, 0, 0, 0},
                                    new float[] {0, 1, 0, 0, 0},
                                    new float[] {0, 0, 1, 0, 0},
                                    new float[] {0, 0, 0, alpha, 0},
                                    new float[] {0, 0, 0, 0, 1}
                                 };
        var colorMatrix = new ColorMatrix(matrix);

        using (var attributes = new ImageAttributes())
        {
            attributes.SetColorMatrix(colorMatrix,
                                         ColorMatrixFlag.Default,
                                         ColorAdjustType.Bitmap);
            e.Graphics.DrawImage(image,
                               new Rectangle(0, 0, image.Width, image.Height),
                               0, 0, image.Width, image.Height,
                               GraphicsUnit.Pixel, attributes);
        }   
    }


}

(this is part of default Windows Form project)

The image that shows the problem is:

problem image

Am I doing something wrong with my ColorMatrix code? Or is this one of those rare cases where I may have found a problem in the Framework/OS/whatever I m building on top of?

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签