473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2078
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

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

Similar topics

3
by: Jeroen Ceuppens | last post by:
Hi, When i do this in my programma, in the contstructor of the form: g.DrawImage(i,new Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel); it doens't paint it ;( but if...
8
by: Mark Johnson | last post by:
Using: VS 2003 NET C# for Framework and Framework Compact Trying : Moving a Card (Bitmap) as in Solitare (PC + WinCe) Version on OnMouseMove Problem : The affected drawing Area by Invalidate (or...
1
by: James Dean | last post by:
I have tried a few different methods for scaling an image down but all seem a bit on the slow side. Is there any way i can make this scaling faster....... Here is how i do it now. To display my...
4
by: Darren Clark | last post by:
I am trying to simply embed a opaque image over another image... if i use a windows form it works.. e.g. Graphics g = this.pictureBox1.CreateGraphics(); g.Clear(this.BackColor);
2
by: Tamer Abdalla via DotNetMonster.com | last post by:
Hello, everyone! I DO need some help in order to understand how to create graphics in VB.NET. I'm a little bit confused... I once knew a time when using Point & PSet was almost the only way to...
3
by: perspolis | last post by:
Hi all is it possible in C# to copy content of a graphics object to another like BitBlt API?? thanks
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
9
by: DaveL | last post by:
hello I have a Bit map 1367 wide 32 high this bitmap contains like 40 separate Images 32x32 I tell it the id*32 to get the approiate Image from the source Bitmap When i CreateGraphics()...
2
by: drsmooth | last post by:
Hi, i have made several different things using a double buffer and a jframe before but never had this problem: if you look thrugh the code, you can see that i draw a whole bunch of stuff to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.