views:

31

answers:

1

I can not find a way to stop the out of memory error. The error occurs when I move the brightness or contrast track bar a few times.

Here is the code from the main form design for the button_click event that that opens the brightness and contrast form and send the value of "foto".

private void menuItemBrightness_Click(object sender, EventArgs e)
{
    BrightnessContrast bFrm = new BrightnessContrast();
    bFrm.Foto = imageHandler.CurrentBitmap;

    if (bFrm.ShowDialog() == DialogResult.OK)
    {
        this.Cursor = Cursors.WaitCursor;
        imageHandler.RestorePrevious();// this is for a undo or redo ... ignore this part
        imageHandler.CurrentBitmap = bFrm.Foto1; 
        pictureBox1.Refresh();
        this.Cursor = Cursors.Default;
    }
}

Here is the complete code for the brightness and contras form

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging;

namespace SMART { public partial class BrightnessContrast : Form { ImageHandler imageHandler = new ImageHandler();

public int B = 0;
private int C = 0;
private static Bitmap foto;
private static Bitmap foto1;
private static Bitmap NewBitmap;

public BrightnessContrast()
{
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
    InitializeComponent();
    btnOK.DialogResult = DialogResult.OK;
    btnCancel.DialogResult = DialogResult.Cancel;
    domainUpDownB.SelectedIndex = 255;
    domainUpDownC.SelectedIndex = 75;

}

private void BrightnessForm_Load(object sender, EventArgs e)
{    pictureBox2.Image = foto;  }


public Bitmap Foto
{
    get   {  return foto; }

    set  { foto = value;  }
}

public Bitmap Foto1
{
    get { return foto1; }
    set { foto1 = value; }
}




public void brightnesstrackBar1_ValueChanged(object sender, EventArgs e)
{
   domainUpDownB.Text = ((int)brightnessTrackBar.Value).ToString();
    B = ((int)brightnessTrackBar.Value);
    pictureBox2.Image = AdjustBrightness(foto, B);
    foto1 = (Bitmap)pictureBox2.Image;
}

private void domainUpDown_TextChanged(object sender, EventArgs e)
{
    try
    { 
        brightnessTrackBar.Value = Convert.ToInt32(domainUpDownB.Text);
        brightnesstrackBar1_ValueChanged(sender, e);
    }
    catch (Exception)
    {
        MessageBox.Show("Incorect Input Value Format");
    }
}


public static Bitmap AdjustBrightness(Bitmap Image, int Value)
{
    Bitmap TempBitmap = Image;
    float FinalValue = (float)Value / 255.0f;
     NewBitmap  = new Bitmap(TempBitmap.Width, TempBitmap.Height);
    Graphics NewGraphics = Graphics.FromImage(NewBitmap);

    float[][] FloatColorMatrix ={
                                    new float[] {1, 0, 0, 0, 0},
                                    new float[] {0, 1, 0, 0, 0},
                                    new float[] {0, 0, 1, 0, 0},
                                    new float[] {0, 0, 0, 1, 0},
                                    new float[] {FinalValue, FinalValue, FinalValue, 1, 1
                                }
        };

    ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
    ImageAttributes Attributes = new ImageAttributes();
    Attributes.SetColorMatrix(NewColorMatrix);
    NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
    Attributes.Dispose(); 
    NewGraphics.Dispose();

    return NewBitmap;
}



public void contrastrackBar1_ValueChanged(object sender, EventArgs e)
{

    domainUpDownC.Text = ((int)contrastTrackBar.Value).ToString();
    C = ((int)contrastTrackBar.Value);
    pictureBox2.Image = AdjustContrast(foto, C);
    foto1 = (Bitmap)pictureBox2.Image;

}


public static Bitmap AdjustContrast(Bitmap Image, int Value)
{
    Bitmap TempBitmap = Image;
    float FinalValue = (float)Value*0.04f;

  NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
    Graphics NewGraphics = Graphics.FromImage(NewBitmap);
    ImageAttributes Attributes = new ImageAttributes();

         ColorMatrix NewColorMatrix = new ColorMatrix (new float[][] {
            new float[] {FinalValue, 0f, 0f, 0f, 0f},
            new float[] {0f, FinalValue, 0f, 0f, 0f},
            new float[] {0f, 0f, FinalValue, 0f, 0},
            new float[] {0f, 0f, 0f, 1f, 0f},
            new float[] {0.001f, 0.001f, 0.001f, 0f, 1f}
        });


   Attributes.SetColorMatrix(NewColorMatrix);
    NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
    Attributes.Dispose();
    NewGraphics.Dispose();
    return NewBitmap;
}

OK here is the problem ... if I load a big image(as in pixels for example) and start moving the track Bar after a few moves it will show the famous "Out of memory exception was unhanded" and the error points to this line

NewGraphics.DrawImage(TempBitmap, 
        new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, 
        TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, 
        Attributes);

I am using Attributes.Dispose(); NewGraphics.Dispose(); way i still have the error what I am missing?

A: 

Dispose gets rid of the unmanged resources (handles, unmanaged memory). There is probably a managed memory footprint here as well and that only gets free when the garbage collect occurs. My guess is that you need to reuse some of these bigger memory objects rather than having to recreate them each time.

Swanny