Frage

I'm facing problems with a custom written plotter class. This class is for drawing on a given PictureBox's Image.

Basic usage is something like this:

        double[] signal = StaticSignalGenerator.Sinus(10.0, 0.0, 1000, 5);
        using (Plot plotter = new Plot(pictureBox1, Color.White))
        {
            plotter.Draw(signal, Pens.Black);
        }

The constructor uses pictureBox1 as reference and White background color will be made for the image. The drawing is a complex transformation in length and in amplitude, it uses graphics to draw in a using block, than the reference pictureBox image will be updated automatically.

I think somewhere there is the problem, because the class is disposed before the pictureBox image could be refreshed?

this.pictureBoxRef.Image = this.drawBitmap;

Will be better to use a Clone of the bitmap? (if that would stay in the same scope, it is not gonna help I fear)

If I use this class with using block, when reaching the dispose phase I got ArgumentException. The class implements IDisposable, with the following lines:

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this.pictureBoxRef != null)
            {
                this.pictureBoxRef.MouseMove -= new MouseEventHandler(PictureBoxRef_MouseMove);
            }
            if (this.drawBitmap != null)
            {
                this.drawBitmap.Dispose();
            }
            if (this.initializedBitmap != null)
            {
                this.initializedBitmap.Dispose();
            }
        }
    }

Invalid parameter: null, ArgumentException was not handled. StackTrace is the following:

System.Drawing.Image.get_Width() System.Drawing.Image.get_Size() System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode) System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) System.Windows.Forms.Control.WmPaint(Message& m) System.Windows.Forms.Control.WndProc(Message& m) System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Any idea how to fix this problem? Thanks in advance!

War es hilfreich?

Lösung

It's hard to tell as you've given us very little information, but it looks like it's still trying to draw a PictureBox. It's not clear whether these have any relation to the values you're disposing, but if the PictureBox contains one of the Bitmap values you're disposing, that could easily be the problem. You probably want to remove the PictureBox from the form first, or maybe set its Image property to null.

(If you could create a short but complete program demonstrating the problem, it would make it considerably easier to help you...)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top