469,569 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,569 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. public class FadePanel : Control
  2.     {
  3.  
  4.         Image _imageA;
  5.  
  6.         public Image ImageA
  7.         {
  8.             get { return _imageA; }
  9.             set { _imageA = value; }
  10.         }
  11.  
  12.         Image _imageB;
  13.  
  14.         public Image ImageB
  15.         {
  16.             get { return _imageB; }
  17.             set { _imageB = value; }
  18.         }
  19.  
  20.         int _fade = 0;
  21.         float _fadeTime;
  22.  
  23.         public float FadeTime
  24.         {
  25.             get { return _fadeTime; }
  26.             set { _fadeTime = value; }
  27.         }
  28.  
  29.         Timer t = new Timer();
  30.  
  31.         public FadePanel()
  32.         {
  33.  
  34.             SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
  35.                 ControlStyles.DoubleBuffer, true);
  36.  
  37.             t.Tick += new EventHandler(t_Tick);
  38.  
  39.         }
  40.  
  41.         protected override void OnPaint(PaintEventArgs e)
  42.         {
  43.             if (_imageA == null)
  44.                 return;
  45.  
  46.             e.Graphics.DrawImage(_imageA, this.ClientRectangle, 0, 0, _imageA.Width, 
  47.                 _imageA.Height, GraphicsUnit.Pixel);
  48.  
  49.             if (_imageB == null)
  50.                 return;
  51.  
  52.             ImageAttributes ia = new ImageAttributes();
  53.  
  54.             ColorMatrix cm = new ColorMatrix();
  55.  
  56.             cm.Matrix33 = 1.0f / 255 * _fade;
  57.  
  58.             ia.SetColorMatrix(cm);
  59.  
  60.             e.Graphics.DrawImage(_imageB, this.ClientRectangle, 0, 0, _imageA.Width,
  61.                 _imageA.Height, GraphicsUnit.Pixel, ia);
  62.  
  63.             //base.OnPaint(e);
  64.         }
  65.  
  66.         public void Fade()
  67.         {
  68.             _fade = 1;
  69.             this.t.Interval = (int)(1000f * _fadeTime / 32);
  70.             this.t.Enabled = true;
  71.         }
  72.  
  73.         private void t_Tick(object sender, EventArgs e)
  74.         {
  75.             _fade += 8;
  76.             if (_fade >= 255)
  77.             {
  78.                 _fade = 255;
  79.                 t.Enabled = false;
  80.             }
  81.             Invalidate();
  82.         }
  83.  
  84.         protected override void OnPaintBackground(PaintEventArgs pevent)
  85.         {
  86.  
  87.         }
  88.     }
  89.  
The form is of proper size for the images and this is the constructor for the form.

Expand|Select|Wrap|Line Numbers
  1. public Form1()
  2.         {
  3.             InitializeComponent();
  4.  
  5.             fpPictureBox.ImageA = Image.FromFile(@"images\forest01.jpg");
  6.             fpPictureBox.ImageB = Image.FromFile(@"images\winter01.jpg");
  7.             fpPictureBox.Fade();
  8.         }
  9.  
Any help is appreciated.
Mar 22 '07 #1
2 1911
I've worked on this some more but have not found a solution. No ideas from anyone?
Mar 27 '07 #2
RedSon
5,000 Expert 4TB
You can try setting the thread priority level of your processes worker thread to a lower level.
Mar 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jeroen Ceuppens | last post: by
8 posts views Thread by Mark Johnson | last post: by
1 post views Thread by James Dean | last post: by
4 posts views Thread by Darren Clark | last post: by
2 posts views Thread by Tamer Abdalla via DotNetMonster.com | last post: by
3 posts views Thread by perspolis | last post: by
9 posts views Thread by DaveL | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.