A generic error occurred in GDI+.

  • Thread starter Thread starter Guest1993
  • Start date Start date
G

Guest1993

Guest
I have a water mark method that use bitmap and is fast in performance (if it matters). when I added Dispose it just delayed the error (processed more files the throw the exception) therefor I feel it is memory allocation issue. Debugging doesn't give me much details. Here is my method, I hope someone can improve it or help me figure out how to delete GDI+ objects after done using, I ran out of ideas and enhancement. If i sacrifice little of the speed that is ok.

public void waterMark(string euroPrefix, string resultedTifFolders, string waterMarkedTif)
{

//try
//{
using (Bitmap bitmap = new Bitmap(resultedTifFolders))
{

using (Brush brush = new SolidBrush(Color.Black))
{
using (Font font = new Font("Arial", 50, GraphicsUnit.Pixel))
{
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);

using (Graphics tempGraphics = Graphics.FromImage(tempBitmap))
{
SizeF euroPrefixSize = tempGraphics.MeasureString(euroPrefix, font);

tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)euroPrefixSize.Height + 10);

tempBitmap.SetResolution(300, 300);

using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);

graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);

Point position = new Point(bitmap.Width - ((int)euroPrefixSize.Width + 200), bitmap.Height + 5);

graphics.DrawString((euroPrefix), font, brush, position);

tempBitmap.Save(waterMarkedTif, ImageFormat.Tiff);

tempBitmap.Dispose();

graphics.Dispose();
}

tempGraphics.Dispose();
}

font.Dispose();
}
// DeleteObject(brush);
brush.Dispose();
}

bitmap.Dispose();

}
//}
//catch { };
}


If anyone wants to see the stack trace I can provide.

EDIT: when I kill tasks that use GDI+ , my program process more files(watermark more images). Also, the visual studio app dev .exe use same number of GDI objects all the time 137.

Continue reading...
 
Back
Top