Performance of Graphics.DrawImage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirkage
    New Member
    • Mar 2007
    • 5

    Performance of Graphics.DrawImage

    I am attempting to create a program in C# which part of it consists of one image fading to another. The best way I found this to work is from an example by Bob Powell on the thread http://www.thescripts.com/forum/thread106125.html.

    The issue is the performance of this code. A simple program that fades two images at 1280x1024 takes ~100% CPU. This is of course an issue. I'm hoping to see if anyone has a suggestion on how to accomplish this in a much more efficient manner. The code I am using is below,

    The class:
    Code:
    public class FadePanel : Control
        {
    
            Image _imageA;
    
            public Image ImageA
            {
                get { return _imageA; }
                set { _imageA = value; }
            }
    
            Image _imageB;
    
            public Image ImageB
            {
                get { return _imageB; }
                set { _imageB = value; }
            }
    
            int _fade = 0;
            float _fadeTime;
    
            public float FadeTime
            {
                get { return _fadeTime; }
                set { _fadeTime = value; }
            }
    
            Timer t = new Timer();
    
            public FadePanel()
            {
    
                SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
                    ControlStyles.DoubleBuffer, true);
    
                t.Tick += new EventHandler(t_Tick);
    
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                if (_imageA == null)
                    return;
    
                e.Graphics.DrawImage(_imageA, this.ClientRectangle, 0, 0, _imageA.Width, 
                    _imageA.Height, GraphicsUnit.Pixel);
    
                if (_imageB == null)
                    return;
    
                ImageAttributes ia = new ImageAttributes();
    
                ColorMatrix cm = new ColorMatrix();
    
                cm.Matrix33 = 1.0f / 255 * _fade;
    
                ia.SetColorMatrix(cm);
    
                e.Graphics.DrawImage(_imageB, this.ClientRectangle, 0, 0, _imageA.Width,
                    _imageA.Height, GraphicsUnit.Pixel, ia);
    
                //base.OnPaint(e);
            }
    
            public void Fade()
            {
                _fade = 1;
                this.t.Interval = (int)(1000f * _fadeTime / 32);
                this.t.Enabled = true;
            }
    
            private void t_Tick(object sender, EventArgs e)
            {
                _fade += 8;
                if (_fade >= 255)
                {
                    _fade = 255;
                    t.Enabled = false;
                }
                Invalidate();
            }
    
            protected override void OnPaintBackground(PaintEventArgs pevent)
            {
                
            }
        }
    The form is of proper size for the images and this is the constructor for the form.

    Code:
    public Form1()
            {
                InitializeComponent();
    
                fpPictureBox.ImageA = Image.FromFile(@"images\forest01.jpg");
                fpPictureBox.ImageB = Image.FromFile(@"images\winter01.jpg");
                fpPictureBox.Fade();
            }
    Any help is appreciated.
  • kirkage
    New Member
    • Mar 2007
    • 5

    #2
    I've worked on this some more but have not found a solution. No ideas from anyone?

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      You can try setting the thread priority level of your processes worker thread to a lower level.

      Comment

      Working...