473,507 Members | 9,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Time Tick consumes 100% CPU

Hi,

I am using the following code to the update a section of the image
which is drawn on a panel.
this.m_Timer = new System.Windows.Forms.Timer(this.components);
this.m_Timer.Tick += new System.EventHandler(this.m_Timer_Tick);
this.m_Timer.Enalbled = true;

private void m_Timer_Tick(object sender, System.EventArgs e)
{
RepaintMagnifier();
}

private void RepaintMagnifier()
{
Graphics g = this.pnlBody.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;
g.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

if(img != null)
g.DrawImage(img, new Rectangle(0, 0, this.pnlBody.Width,
this.pnlBody.Height), new
Rectangle(iXpos,iYpos,(int)(this.pnlBody.Width*((f loat)this.iZoomFactor/100)),(int)(this.pnlBody.Height*((float)this.iZoom Factor/100))),
GraphicsUnit.Pixel);
}

The image file size is around 0.5 MB which is being drawn on the panel
control.

Above code is taking 100% CPU cycle.

I am using the above approach for repainting the screen to avoid the
flickering.

It there any other way to perform this action or should i use the a
secondary thread instead of timer to repaint the screen.
Thanks,
Utkarsh

Nov 17 '05 #1
7 5927
I am not good at graphics but once done, you should Dispose "g"

HTH
Kalpesh

Nov 17 '05 #2
How often does the time fire?

Normally, to prevent flickering when repainting, you would set your
forms so they use double buffering.

Check this specific article:

http://www.bobpowell.net/doublebuffer.htm

Also check out all the articles on his site, they're very good:

http://www.bobpowell.net
good luck

Nov 17 '05 #3
Hi utkarsh,
if you want to avoid flickering and you are using a panel to display an
image I would override the OnPaintBackground method to do nothing - to reduce
flickering caused by painting the background, then override the OnPaint
method to paint a bitmap image you have stored internally in the panel, on
the paint method don't do anything apart from painting. you would then have
another method you woud call to update the image properties outside of the on
paint method. Something like:
private Image _bufferedImage;
private int _x = 0;
private int _y = 0;
private float _scaleFactor = 1.0f;

//You call this explicitly to update the buffered image
public void ReDraw()
{
//assume there is another method like LoadImage which will
load
//an image into the _bufferedImage member variable
Graphics g = Graphics.FromImage(_bufferedImage);

GraphicsContainer gcOrig = g.BeginContainer();

//do some processing, i.e. change x and y and scale
//of the image
g.TranslateTransform(_x, _y);
g.ScaleTransform(_scaleFactor, _scaleFactor);
g.DrawImage(_image,5,5);

g.EndContainer(gcOrig);
this.Refresh();
}

protected override void OnPaint(PaintEventArgs e)
{
//simply paint your buffered image to the control - do not
do any
//processing
if(_bufferedImage != null)
{
e.Graphics.DrawImage(_bufferedImage, 0 ,0);
}
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do nothing here so that image does not have to repaint the background
}

Hope that helps
Mark R Dawson
http://www.markdawson.org

"utkarsh" wrote:
Hi,

I am using the following code to the update a section of the image
which is drawn on a panel.
this.m_Timer = new System.Windows.Forms.Timer(this.components);
this.m_Timer.Tick += new System.EventHandler(this.m_Timer_Tick);
this.m_Timer.Enalbled = true;

private void m_Timer_Tick(object sender, System.EventArgs e)
{
RepaintMagnifier();
}

private void RepaintMagnifier()
{
Graphics g = this.pnlBody.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality ;
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;
g.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

if(img != null)
g.DrawImage(img, new Rectangle(0, 0, this.pnlBody.Width,
this.pnlBody.Height), new
Rectangle(iXpos,iYpos,(int)(this.pnlBody.Width*((f loat)this.iZoomFactor/100)),(int)(this.pnlBody.Height*((float)this.iZoom Factor/100))),
GraphicsUnit.Pixel);
}

The image file size is around 0.5 MB which is being drawn on the panel
control.

Above code is taking 100% CPU cycle.

I am using the above approach for repainting the screen to avoid the
flickering.

It there any other way to perform this action or should i use the a
secondary thread instead of timer to repaint the screen.
Thanks,
Utkarsh

Nov 17 '05 #4
Thanks Mark for your code and so much help!! :)

But my problem is little bit different.
I have two screen left and right. In left panel I have loaded a Image
in picture box. When user move the mouse on the left panel, a
maginified view of a image section, near by the cursor, should be
visible in the right panel.

I have to do the similar work like the windows magnifier ( Programs ->
Accessories -> Accessibility -> Magnifier)

So I have loaded the same image in both the screens and I have to
update the right screen as mouse move in left. I have removed the
flickering by using the timer tick event. But only problem left is,
timer is consuming the 100% CPU.

Thanks again,
Utkarsh Panwar

Nov 17 '05 #5
Hi Utkarsh,
I made a little app that mimics the magnifier behaviour you talked about,
with an image on the left and a magnified version on the right. I loaded a
3MB file and it seems to take about 50% of the CPU on my home computer when I
move the mouse very rapidly, but with no flickering.

I have 3 files:
1. ZoomSelectorPictureBox.cs -> displays an image with a red rectangle
around the mouse indicating where the zoomed in portion of the image will be
displayed. This inherits from PictureBox (although it doesn't really need to)

2. ZoomMagnifierPictureBox which shows a zoomed in version of the selected
region in ZoomSelectorPictureBox, this also inherits from picturebox

3. Form1.cs -> creates instances of the above classes. you will need to
modify the Form1_Load method to load an image on your computer.

I have pasted all three files below, but the formatting will be messed up in
these windows. If you like you can download the entire source from
http://www.markdawson.org/software/csharp/magnifier.zip which may be easier
than getting the text code out from below.
//FORM1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(800, 469);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
ZoomSelectorPictureBox selectorPictureBox = new ZoomSelectorPictureBox();
selectorPictureBox.Image = Image.FromFile(@"c:\test.bmp");

this.Controls.Add(selectorPictureBox);
selectorPictureBox.Left = 0;
selectorPictureBox.Top = 0;
selectorPictureBox.Width = 400;
selectorPictureBox.Height = 400;

ZoomMagnifierPictureBox zoomMagnifier = new
ZoomMagnifierPictureBox(selectorPictureBox);
this.Controls.Add(zoomMagnifier);
zoomMagnifier.Left = 400;
zoomMagnifier.Top = 0;
zoomMagnifier.Width = 400;
zoomMagnifier.Height = 400;
}
}
}



//ZoomMagnifierPictureBox.cs

using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication4
{
public class ZoomMagnifierPictureBox : PictureBox
{
private Rectangle _zoomRegion;
private Image _originalImage;

public ZoomMagnifierPictureBox(ZoomSelectorPictureBox zoomSelector) : base()
{
_originalImage = zoomSelector.Image;
zoomSelector.ZoomRectangleChanged += new
WindowsApplication4.ZoomSelectorPictureBox.ZoomRec tangleLocationChangedEventHandler(zoomSelector_Zoo mRectangleChanged);
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
//base.OnPaintBackground (pevent);
}

protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint (e);
e.Graphics.DrawImage(_originalImage, new Rectangle(0,0, this.Width,
this.Height), _zoomRegion.X, _zoomRegion.Y, _zoomRegion.Width,
_zoomRegion.Height, GraphicsUnit.Pixel);
}

private void zoomSelector_ZoomRectangleChanged(Rectangle zoomRectangle)
{
_zoomRegion = zoomRectangle;
this.Refresh();
}
}
}


//ZoomSelectorPictureBox.cs

using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication4
{
public class ZoomSelectorPictureBox : PictureBox
{
private Rectangle _zoomRegion;
private bool _mouseIsOver = false;

public delegate void ZoomRectangleLocationChangedEventHandler(Rectangle
zoomRectangle);
public event ZoomRectangleLocationChangedEventHandler ZoomRectangleChanged;

public ZoomSelectorPictureBox() : base()
{
//create zoom region rectangle
_zoomRegion = new Rectangle(0, 0, 50, 50);

//set up event handlers
this.MouseMove += new MouseEventHandler(ZoomSelectorPictureBox_MouseMove );
this.MouseLeave += new EventHandler(ZoomSelectorPictureBox_MouseLeave);
this.MouseEnter += new EventHandler(ZoomSelectorPictureBox_MouseEnter);
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
//base.OnPaintBackground (pevent);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);

//only do this if the mouse is over the control
if(_mouseIsOver)
{
using(Pen p = new Pen(Color.Red, 5))
{
e.Graphics.DrawRectangle(p, _zoomRegion);
}
}
}

private void ZoomSelectorPictureBox_MouseMove(object sender,
MouseEventArgs e)
{
//make the mouse in the center of the zoom region
_zoomRegion.X = e.X - _zoomRegion.Width / 2;
_zoomRegion.Y = e.Y - _zoomRegion.Width / 2;

//raise the event to listeners to refresh their
//zoomed view
if(ZoomRectangleChanged != null)
{
ZoomRectangleChanged(_zoomRegion);
}

//force image to be redrawn
this.Refresh();
}

private void ZoomSelectorPictureBox_MouseLeave(object sender, EventArgs e)
{
_mouseIsOver = false;

//force image to redraw without the rectangle
this.Refresh();
}

private void ZoomSelectorPictureBox_MouseEnter(object sender, EventArgs e)
{
_mouseIsOver = true;
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org


"utkarsh" wrote:
Thanks Mark for your code and so much help!! :)

But my problem is little bit different.
I have two screen left and right. In left panel I have loaded a Image
in picture box. When user move the mouse on the left panel, a
maginified view of a image section, near by the cursor, should be
visible in the right panel.

I have to do the similar work like the windows magnifier ( Programs ->
Accessories -> Accessibility -> Magnifier)

So I have loaded the same image in both the screens and I have to
update the right screen as mouse move in left. I have removed the
flickering by using the timer tick event. But only problem left is,
timer is consuming the 100% CPU.

Thanks again,
Utkarsh Panwar

Nov 17 '05 #6
A timer is really not a good idea, take Marks advice on the
OnPaintBackground and OnPaint methods and add an OnMouseMove event
handler to the left panel. In that handler add something like
rightPanel.Invalidate(). This will force the right panel to be repainted
when the mouse moves over the left panel.

Nick Z.

utkarsh wrote:
Thanks Mark for your code and so much help!! :)

But my problem is little bit different.
I have two screen left and right. In left panel I have loaded a Image
in picture box. When user move the mouse on the left panel, a
maginified view of a image section, near by the cursor, should be
visible in the right panel.

I have to do the similar work like the windows magnifier ( Programs ->
Accessories -> Accessibility -> Magnifier)

So I have loaded the same image in both the screens and I have to
update the right screen as mouse move in left. I have removed the
flickering by using the timer tick event. But only problem left is,
timer is consuming the 100% CPU.

Thanks again,
Utkarsh Panwar

Nov 17 '05 #7
Thank you very much Mark for such a great help !!!

:)

I looked at the code it is perfectly fine. CPU goes high till 50%, if
mouse movement is faster. But there is absolutly no flickering.
Thanks Again,
Utkarsh Panwar

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4341
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
9
7246
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
3
3849
by: Steve | last post by:
I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in my VC++ 6.0 projects to measure correct time spans, for example for timeouts, how much time takes to execute certain...
6
4681
by: Rich | last post by:
Hello, I have a label in my app where I display time from a Timer contol like this: Private Sub Timer1_Tick(...) Handles Timer1.Tick lbl_Time.Text = TimeOfDay.ToString End Sub The display...
8
1335
by: Dean | last post by:
Hi, I am using Vb.net and I want to know how to put the Time, Date which ticks?? Please reply as soon as possible Dean PS: Please make the answer simple because I am only 11 Yrs old and...
3
1702
by: Tim_Mac | last post by:
hi, i'm generating PDF files from crystal reports in my .Net 1.1 web site, and saving them to disk for the user to download in their own time. the format i'm currently using is: ...
0
3251
hqprog
by: hqprog | last post by:
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard. I need to use these two functions in myprog.c (on pc - ...
3
1498
w33nie
by: w33nie | last post by:
I'd like to put a clock on my web site, that shows the time where I am, not where the user is. I found a regular clock with google, that shows the time of the user's system clock, so I thought it...
11
5556
by: xenoix | last post by:
hey there, im reasonably new to C# and im currently writing a backup application which im using as a learning resource. My PC :- Visual Studio 2005 .NET Framework 2 Component Factory Krypton...
0
7110
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
7314
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,...
0
7372
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
7482
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...
1
5041
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.