473,804 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance of Graphics.DrawIm age

5 New Member
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 2108
kirkage
5 New Member
I've worked on this some more but have not found a solution. No ideas from anyone?
Mar 27 '07 #2
RedSon
5,000 Recognized Expert Expert
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
4859
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 I make a button with the same, after the click it paints it, why the problem, i can't get out of it.........
8
12283
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 Invalidate(Rectangle)) flickers in a nasty way when repainting. This does not happen in the Solitare (PC + WinCe) Versions a well as in a Card game where I have the C++ Source. The use of an empty OnPaintBackground brings no visable results.
1
2436
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 image at 1:2 zoom i have the following xScaleFactor = 0.5 yScaleFactor = 0.5 public static Bitmap ScaleBitmap(Bitmap inputBmp, double xScaleFactor, double yScaleFactor) { //Create a new bitmap object based on the input
4
1800
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
1679
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 make some interesting apps which could tranform images (i.e. making saturation of colours "heavy", or gradually fade to grayscale, or "erasing" a colour... and so on), while nowadays it seems quite impossible. Now that I got .NET over...
3
9322
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
5067
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 passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
9
2667
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() From the Standard CreateGraphics() function the code below works
2
3341
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 screen and sometimes, i get a frame or two of just blank jframe color public void draw() { BufferStrategy bf = this.getBufferStrategy(); Graphics2D g = null; if(scrollPos>lastCoveredPos) lastCoveredPos =...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.