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 146425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:36:03+00:00 2026-05-11T08:36:03+00:00

I am trying to quickly process large images (about 2000×2000). I am using a

  • 0

I am trying to quickly process large images (about 2000×2000). I am using a TrackBar to change the overall brightness of an image. The problem is that the TrackBar (as you all know) can be moved very quickly. This is ok as I do not need to process the image for every tick of the TrackBar, but it does need to be reasonably responsive. Think of the brightness TrackBars found in image editors.

I have tried doing all of the processing in another thread, which works, but it is still too slow (even using LockBits on the Bitmap). I can’t use a ColorMatrix because it allows component overflows and I need values to clip at 255. So, I am thinking that I can achieve good performance by using the ThreadPool and splitting the image up into sections.

The problem with this approach is that I just don’t have much experience with multi-trheaded applications and I don’t know how to avoid the race conditions that crop up (calling LockBits on an already locked image, etc.). Could anyone give me an example of how to do this?

Here is the code that I have so far. I know that it is far from good, but I am just working it out and trying a bunch of different things at this point. The basic concept is to use a base image as a source, perform some operation on each pixel, then draw the processed pixel into a display Bitmap. Any help would be awesome.

    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;                 public TestPBox( )     {         this.DoubleBuffered = true;                                                       IntensityScale = 1.0f;     }            public Bitmap Image     {         get         {             return m_dispBitmap;         }         set         {             if ( value != null )             {                 m_srcBitmap = value;                 m_dispBitmap = (Bitmap) value.Clone( );                 Invalidate( );             }         }     }      [DefaultValue( 1.0f )]     public float IntensityScale     {         get         {             return m_scale;         }         set         {             if ( value == 0.0 || m_scale == value )             {                                    return;             }              if ( !this.DesignMode )             {                 m_scale = value;                 ProcessImage( );                                 }         }     }         private void ProcessImage( )     {         if ( Image != null )         {             int sections = 10;             int sectionHeight = (int) m_srcBitmap.Height / sections;             for ( int i = 0; i < sections; ++i )             {                 Rectangle next = new Rectangle( 0, i * sectionHeight, m_srcBitmap.Width, sectionHeight );                 ThreadPool.QueueUserWorkItem( new WaitCallback( delegate { ChangeIntensity( next ); } ) );             }         }     }      private unsafe void ChangeIntensity( Rectangle rect )     {                     BitmapData srcData = m_srcBitmap.LockBits( rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );         BitmapData dspData = m_dispBitmap.LockBits( rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );                     byte* pSrc = (byte*) srcData.Scan0;         byte* pDsp = (byte*) dspData.Scan0;          for ( int y = 0; y < rect.Height; ++y )         {             for ( int x = 0; x < rect.Width; ++x )             {                 // we are dealing with a monochrome image, so r = g = b.                 // We only need to get one component 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 );                                       // windows stores images internally in                  // reverse byte order, i.e., Bgra, not Argb.                 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-11T08:36:04+00:00Added an answer on May 11, 2026 at 8:36 am

    Have you considered generating a scaled-down version of the image and doing the image manipulation on that while the user is dragging the trackbar? That would give you responsiveness while still giving the user useful feedback until they settle on a final value.

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

Sidebar

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.