From: ELandry on
I am trying to convert an image to a bitonal image and am getting am
OutOfMemory exception. Below is the method that I am using. I am passing in
a byte array as an image, attempting to convert it to a B&W bitonal image or
bitmap, then return the byte array of the converted image. Error is occuring
on the line with the "TODO" comment.

private byte[] createBlackAndWhiteImageFromBuffer(byte[] buffer)
{
string tempfilename = Path.Combine(Path.GetTempPath(),
String.Format("{0}.tif", Guid.NewGuid()));

Bitmap image = (Bitmap)Bitmap.FromStream(new
MemoryStream(buffer, 0, buffer.Length));

buffer = null;

GC.Collect();

Guid objGuid = image.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);

System.Drawing.Imaging.Encoder encoder =
System.Drawing.Imaging.Encoder.SaveFlag;

ImageCodecInfo info = GetEncoderInfo("image/tiff");

Bitmap pagebitmap = null;

using (MemoryStream str = new MemoryStream())
{
try
{
EncoderParameters ep = new EncoderParameters(2);

ep.Param[1] = new EncoderParameter(encoder,
(long)EncoderValue.CompressionCCITT4);

for (int i = 0; i < image.GetFrameCount(objDimension);
i++)
{
image.SelectActiveFrame(FrameDimension.Page, i);

if (i == 0)
{
pagebitmap = image.Clone(new Rectangle(0, 0,
image.Width, image.Height), PixelFormat.Format1bppIndexed); //TODO: Out of
Memory exception

ep.Param[0] = new EncoderParameter(encoder,
(long)EncoderValue.MultiFrame);
pagebitmap.Save(tempfilename, info, ep);
}
else
{
using (MemoryStream mstream = new MemoryStream())
{
ep.Param[0] = new EncoderParameter(encoder,
(long)EncoderValue.FrameDimensionPage);
image.Save(mstream, ImageFormat.Bmp);

using (Bitmap tempbmp =
((Bitmap)Bitmap.FromStream(mstream)).Clone(new Rectangle(0, 0, image.Width,
image.Height), PixelFormat.Format1bppIndexed))
{
pagebitmap.SaveAdd(tempbmp, ep);
}
}
}

if (i == (image.GetFrameCount(objDimension) - 1))
{
ep.Param[0] = new EncoderParameter(encoder,
(long)EncoderValue.Flush);
pagebitmap.SaveAdd(ep);
}
}



using (FileStream reader = new FileStream(tempfilename,
FileMode.Open, FileAccess.Read))
{

buffer = new byte[reader.Length];

reader.Read(buffer, 0, (int)reader.Length);

reader.Close();
}
}
finally
{
GC.Collect();

if (pagebitmap != null)
{
pagebitmap.Dispose();
}

if (image != null)
{
image.Dispose();
}

if (File.Exists(tempfilename))
{
File.Delete(tempfilename);
}

GC.Collect();
}
}
return buffer;
}

From: Peter Duniho on
ELandry wrote:
> I am trying to convert an image to a bitonal image and am getting am
> OutOfMemory exception. Below is the method that I am using. I am passing in
> a byte array as an image, attempting to convert it to a B&W bitonal image or
> bitmap, then return the byte array of the converted image. Error is occuring
> on the line with the "TODO" comment.

I didn't bother to look at the code closely. It doesn't look like you
have posted the minimal code example required to reproduce the problem,
and in any case without indentation, it's just not worth the
trouble...too hard to read.

But, I can tell you that due to the dependency on GDI+ for image
handling, unfortunately often the error returned is "out of memory" even
when the real problem is something else (usually, for example,
attempting to process an image using a format that's not supported by
GDI+ for that operation).

Pete