Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 157105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:26:42+00:00 2026-05-11T10:26:42+00:00

I wrote a small application to use as a sandbox for testing ideas that

  • 0

I wrote a small application to use as a sandbox for testing ideas that will end up in a production product. The idea is simple; use a TrackBar to change the overall brightness of an image. Because this process takes a fair amount of time, I need to do it in another thread (unless you have a better way). I cannot use the ColorMatrix class because it allows color component values to overflow and I cannot figure out how to clamp them at 255. Anyway, here is the problem:

When I move the TrackBar slowly, everything works great. If I run this on the GUI thread, everything works great (there is too much lag). When I move the TrackBar quickly, the image becomes distorted. Horizontal bands appear across it, as if the image was processed using different scale factors.

I don’t know how this could be as I am drawing into a display bitmap using a base bitmap to get the color values, and I think that I am blocking the operation from beginning again until it completes, but I have very little experience with threading. I’m stuck at this point, so any help you guys could give would be much appreciated. The project is below (had to use filefront as it is too large for a forum attachment).

Here is a link to the project, the entire program can be read below.
http://files.filefront.com/13453973

public partial class Form1 : Form {            public Form1( )     {         InitializeComponent( );         testPBox1.Image = Properties.Resources.test;         trackBar1.Value = 100;         trackBar1.ValueChanged += trackBar1_ValueChanged;     }           void trackBar1_ValueChanged( object sender, EventArgs e )     {         testPBox1.IntensityScale = (float) trackBar1.Value / 100;     } }  class TestPBox : Control {     private const int MIN_SAT_WARNING = 240;             private Bitmap m_srcBitmap;     private Bitmap m_dispBitmap;     private float m_scale;             BackgroundWorker worker;      public TestPBox( )     {         this.DoubleBuffered = true;                                 worker = new BackgroundWorker( );         worker.DoWork += new DoWorkEventHandler( worker_DoWork );         IntensityScale = 1.0f;     }             public Bitmap Image     {         get         {             return m_dispBitmap;         }         set         {             if ( value != null )             {                 m_srcBitmap = value;                 m_dispBitmap = (Bitmap) value.Clone( );                 Invalidate( );                 OnImageChanged( EventArgs.Empty );             }         }     }      [DefaultValue( 1.0f )]     public float IntensityScale     {         get         {             return m_scale;         }         set         {             if ( value == 0.0 || m_scale == value )             {                 return;             }              m_scale = value;             if ( !this.DesignMode && StartImageProcThread( ) )             {                 OnIntensityscaleChanged( EventArgs.Empty );             }                         }     }      private bool StartImageProcThread( )     {                                 if ( !worker.IsBusy )         {                                  worker.RunWorkerAsync( );             return true;         }          return false;     }      private void worker_DoWork( object sender, DoWorkEventArgs e )     {         ChangeIntensity( );     }      private unsafe void ChangeIntensity( )     {         if ( Image != null )         {                             BitmapData srcData = m_srcBitmap.LockBits                     ( new Rectangle( new Point( 0, 0 ), m_srcBitmap.Size ), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );             BitmapData dspData = m_dispBitmap.LockBits                     ( new Rectangle( new Point( 0, 0 ), m_dispBitmap.Size ), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );             byte* pSrc = (byte*) srcData.Scan0;             byte* pDsp = (byte*) dspData.Scan0;              for ( int y = 0; y < m_dispBitmap.Height; ++y )             {                 for ( int x = 0; x < m_dispBitmap.Width; ++x )                 {                     // we are dealing with a monochrome image, so r = g = b.                     // We only need to get one value to use for all r, g, and b.                     byte b = (byte) CompMinMax( 0, 255, (int) ( pSrc[0] * m_scale ) );                     Color c = ( b > MIN_SAT_WARNING ) ? Color.FromArgb( b, Color.Red ) : Color.FromArgb( 255, b, b, b );                      pDsp[3] = (byte) c.A;                     pDsp[2] = (byte) c.R;                     pDsp[1] = (byte) c.G;                     pDsp[0] = (byte) c.B;                      pSrc += 4;                     pDsp += 4;                 }             }              m_srcBitmap.UnlockBits( srcData );             m_dispBitmap.UnlockBits( dspData );             this.Invalidate( );                    }     }      private int CompMinMax( int min, int max, int value )     {         if ( value > max ) return max;         if ( value < min ) return min;         return value;     }      protected override void OnPaint( PaintEventArgs e )     {         if ( Image != null )         {                            Graphics g = e.Graphics;             Rectangle drawingRect = PaintUtils.CenterInRect( ClientRectangle, PaintUtils.ScaleRect( ClientRectangle, Image.Size ).Size );             g.DrawImage( Image, drawingRect, 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel );                  }          base.OnPaint( e );     } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:26:42+00:00Added an answer on May 11, 2026 at 10:26 am

    It looks to me like you can change the value of m_scale in the middle of a ChangeIntensity being run.

        // ...     m_scale = value;      if ( !this.DesignMode && StartImageProcThread( ) )     {         OnIntensityscaleChanged( EventArgs.Empty );     }        // ...     private bool StartImageProcThread( )     {                                 if ( !worker.IsBusy )         {                                  worker.RunWorkerAsync( );             return true;         }          return false;     } 

    If your worker is in the middle of processing, the value that it is using to scale gets changed, and only applies to the pixels that still have to be processed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer > I hear that an application can be tailored such… May 11, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Try using following Replace overload. public static string Replace( string… May 11, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer One way you could change the sample to use lambda… May 11, 2026 at 5:03 pm

Related Questions

I wrote a small PHP application that I'd like to distribute. I'm looking for
There are several topics about what questions will be raised in an interview for
In a past interview, I was asked how would I write a mission critical
We have a large (about 580,000 loc) application which in Delphi 2006 builds (on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.