1
內存泄漏
我得到了內存泄漏 - OOM例外 - 在using (var ergebnis = ResizeImage(Bitmap.FromFile(f), 1, 1))
發現在ResizeImage
哪裏是內存泄漏,是一個dispose丟失在哪裏?
SQLiteConnection.CreateFile("datenbank.sqlite");
m_dbConnection =
new SQLiteConnection("Data Source=datenbank.sqlite;Version=3;");
m_dbConnection.Open();
string sql = "CREATE TABLE verbindungen (farbeR NUMERIC, farbeG NUMERIC, farbeB NUMERIC, Dateiname TEXT)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
int k = 0;
foreach (var f in files)
{
k++;
this.Invoke((MethodInvoker)delegate
{
this.Text = "Datei "+k+ " von "+files.Length+", Fehler: " + fehler;
});
using (FileStream file = new FileStream(f, FileMode.Open, FileAccess.Read))
{
using (var ergebnis = ResizeImage(Bitmap.FromFile(f), 1, 1))
{
Color ds = ergebnis.GetPixel(0, 0);
//
using (var command2 = new SQLiteCommand())
{
sql = "INSERT INTO verbindungen VALUES(" + ds.R + ", " + ds.G + ", " + ds.B + ", '" + f + "')";
command2.CommandText = sql;
command2.Connection = m_dbConnection;
command2.ExecuteNonQuery();
}
}
}
try
{
}
catch (Exception)
{
this.Invoke((MethodInvoker)delegate
{
fehler++;
});
//
}
//MessageBox.Show(f + ": " + abstand(ds,vergleichsfarbe));
}
與
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.Low;
graphics.SmoothingMode = SmoothingMode.None;
graphics.PixelOffsetMode = PixelOffsetMode.None;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
你的源圖像有多大?你有多少內存?什麼行會拋出錯誤? –
@KirkWoll程序本身越來越大。圖像範圍從100KB到4MB。我有8GB。 '使用(var ergebnis = ResizeImage(Bitmap.FromFile(f),1,1))'拋出錯誤。 –
看起來好像你沒有處理你傳遞給'ResizeImage'的位圖,不是? –