CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    Unhappy Problem with DrawLine, SetPixel, Graphics

    Hi,

    I have a problem with Graphics which am not able to understand how to solve it at all.

    - I am receiving data constantly from a file and drawing it on my bmp using following method

    Code:
    public Bitmap bmp = new Bitmap(800, 600);
    
            public void DrawOnGraph()
            {
                Graphics aGPlot = Graphics.FromImage(bmp);           
                aGPlot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                Color c = pen.Color;
                aGPlot.DrawLine(pen, currentPoint, lastPoint);
                aGPlot.Dispose();
            }
    - Now this bmp is refreshed after every 200 ms using OnPaint method
    Code:
            protected override void OnPaint(PaintEventArgs e)
            {
                Graphics gPlot = e.Graphics;
                 gPlot.DrawImage(bmp, 0, 0);
             }
    - To analyze the bmp, user can also do a couple of thing and here my program crashes:
    Code:
            OnSomeMouseClick(Object sender, Event e)
           {               
                            bmp.SetPixel(x, y, Color.Empty);
           }
    The error message is:
    InvalidOperationException :
    If you are using the Graphics object after GetHdc method, call the ReleaseHdc method.

    I do not know how to resolve this. I am working with C# and wont prefer to go into C++ world, use unsafe IntPtr, Handles, import dlls etc. There must be someway, I guess.

    Anyone with some idea?

    Thank you,
    Ricky

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with DrawLine, SetPixel, Graphics

    There must be more going on than what you have shown because this program works exactly as expected.

    Code:
    public partial class Form1 : Form
        {
            Timer _timer;
            public Form1( )
            {
                InitializeComponent( );
                _timer = new Timer( );
                _timer.Interval = 200;
                _timer.Tick += new EventHandler( _timer_Tick );
            }
    
            private void _timer_Tick( object sender, EventArgs e )
            {
                this.Invalidate( );
            }
    
            private Bitmap Bitmap = new Bitmap( 100, 100 );
    
            private void button1_Click( object sender, EventArgs e )
            {
                using ( Graphics g = Graphics.FromImage( Bitmap ) )
                {
                    g.FillRectangle( Brushes.Black, new Rectangle( new Point( 0, 0 ), Bitmap.Size ) );
                }            
                
                _timer.Start( );
            }               
    
            protected override void OnPaint( PaintEventArgs e )
            {
                base.OnPaint( e );
                e.Graphics.DrawImage( Bitmap, new Point( 0, 0 ) );
            }
    
            private void button2_Click( object sender, EventArgs e )
            {
                Bitmap.SetPixel( 50, 50, Color.Yellow );
            }        
        }

  3. #3
    Join Date
    Aug 2008
    Posts
    78

    Re: Problem with DrawLine, SetPixel, Graphics

    Thanks for small simumlation. Now, I at least know that the reason is not what I am thinking can be.

    I researched more about the issue. It says, "Object is in use somewhere else"

    It is arriving due to 2 threads in my program trying to work on same bmp. But it is unfortunately the application requirement :-(

    Do you think if there is some cleaner way to do this, other than using Get, release function or working with handle?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with DrawLine, SetPixel, Graphics

    You can lock the Bitmap while it is in use. That would be the most simple solution, but I don't know if that will give you the results that you are after. I am not sure if there is a better way to do it because I don't really know the structure of your program or what you requirements are.

  5. #5
    Join Date
    Aug 2008
    Posts
    78

    Re: Problem with DrawLine, SetPixel, Graphics

    To avoid conflict, I am now using 2 different images, one overlapping the other.

    The image above is transparent, so that the image below can be seen. It is working fine except one thing. Since they are overlapping, so I need to adjust them in order. Do you know how can I simply send one image to back or bring other to front. Like in Controls.SendToBack(). I have searched for it but havent got any useful answer till now.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with DrawLine, SetPixel, Graphics

    Have you tried simply locking the Bitmap to synchronize access to it? This is a fundamental concept in threading and it would be a good thing to understand.

  7. #7
    Join Date
    Feb 2012
    Posts
    1

    Re: Problem with DrawLine, SetPixel, Graphics

    I have the same problem and got same error like:
    System.InvalidOperationException' occurred in System.Drawing.dll

    I am doing color detection but when i apply euclidean filter on the bitmap image it give me error, but only on camera, if i use a single jpeg or other format image it will work fine. I don't know wht's happening please help me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured