Не могу найти способ остановить ошибку нехватки памяти. Ошибка возникает, когда я несколько раз перемещаю дорожку яркости или контрастности.
Вот код из основного дизайна формы для события button_click, который открывает форму яркости и контраста и отправляет значение «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;
}
}
Вот полный код формы яркости и контраста
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;
}
Хорошо, вот в чем проблема ... если я загружу большое изображение (как, например, в пикселях) и начну перемещать трек-бар после нескольких ходов, он покажет знаменитое «Исключение из памяти не было обработано» и ошибка указывает на эту линию
NewGraphics.DrawImage(TempBitmap,
new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0,
TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel,
Attributes);
Я использую Attributes.Dispose (); NewGraphics.Dispose (); как у меня все еще ошибка что я пропускаю?