I am running a cron job in C# that takes 200,000 images and convert into 1 bit image. During running this job, sometimes the process crashes (even though I have global try catch), sometimes for some images(not all) it throws OutOfMemoryException and sometimes for some images it it throws A generic error occurred in GDI.
int pageSize = 1000;
for (int pageNumber = 0; pageNumber < 200; pageNumber++)
{
var imageUrls = allIMageUrls.Skip(pageSize * pageNumber).Take(pageSize).ToList();
var counter = 0;
var total = imageUrls.Count;
Logger.Log($"Page Number : {pageNumber}");
var failedImageUrls = new System.Collections.Concurrent.ConcurrentBag<string>();
Parallel.ForEach(imageUrls, imageUrl =>
{
try
{
Interlocked.Increment(ref counter);
var image = _httpService.DownloadImage(imageUrl);
if (image != null && image.Length > 0)
{
var oneBitImage = ConvertToOnebitFaxGroup4(contract);
_httpService.UploadImage(image, oneBitImage);
oneBitImage = null;
image = null;
}
}
catch (Exception ex)
{
failedImageUrls.Add(imageUrl);
Logger.Log(ex);
}
});
This is one time process. I added paging so that when it crashes I can restart from that page instead of start at beginning.
public static class ImageProcessor
{
static ImageCodecInfo _codecInfo;
static EncoderParameters _encoderParameters;
static ImageProcessor()
{
foreach (var codec in ImageCodecInfo.GetImageEncoders())
{
if (codec.MimeType == "image/tiff")
{
_codecInfo = codec;
break;
}
}
_encoderParameters = new EncoderParameters(2);
_encoderParameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
_encoderParameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)1);
}
public static byte[] ConvertToOnebitFaxGroup4(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
var image = Image.FromStream(memoryStream);
var pData = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, pData, bytes.Length);
var bytesPerLine = (image.Width + 31) / 32 * 4;
var img = new Bitmap(image.Width, image.Height, bytesPerLine, PixelFormat.Format1bppIndexed, pData);
using (var ms = new MemoryStream())
{
image.Save(ms, _codecInfo, _encoderParameters);
img.Dispose();
Marshal.FreeHGlobal(pData);
return ms.ToArray();
}
}
}
Updated:
public static byte[] ConvertToOnebitFaxGroup4(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var image = Image.FromStream(memoryStream))
{
var pData = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, pData, bytes.Length);
var bytesPerLine = (image.Width + 31) / 32 * 4;
using (var img = new Bitmap(image.Width, image.Height, bytesPerLine, PixelFormat.Format1bppIndexed, pData))
{
using (var ms = new MemoryStream())
{
img.Save(ms, _codecInfo, _encoderParameters);
Marshal.FreeHGlobal(pData);
return ms.ToArray();
}
}
}
}
}
Update2
public static byte[] ConvertToOnebitFaxGroup4(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var image = Image.FromStream(memoryStream))
{
using (var ms = new MemoryStream())
{
image.Save(ms, _codecInfo, _encoderParameters);
return ms.ToArray();
}
}
}
}
Related
I used this code but not getting correct format Image.
give better way to convert pdf to Image using iTextsharp.
FileStream fs = File.OpenRead(ImagePath);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
List<System.Drawing.Image> ImgList = new List<System.Drawing.Image>();
iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null;
iTextSharp.text.pdf.PdfReader PDFReaderObj = null;
iTextSharp.text.pdf.PdfObject PDFObj = null;
iTextSharp.text.pdf.PdfStream PDFStremObj = null;
try
{
RAFObj = new iTextSharp.text.pdf.RandomAccessFileOrArray(data);
PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj, null);
for (int k = 0; k <= PDFReaderObj.XrefSize - 1; k++)
{
PDFObj = PDFReaderObj.GetPdfObject(k);
if ((PDFObj != null) && PDFObj.IsStream())
{
PDFStremObj = (iTextSharp.text.pdf.PdfStream)PDFObj;
iTextSharp.text.pdf.PdfObject subtype = PDFStremObj.Get(iTextSharp.text.pdf.PdfName.SUBTYPE);
if ((subtype != null) && subtype.ToString() == iTextSharp.text.pdf.PdfName.IMAGE.ToString())
{
byte[] bytes = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw((iTextSharp.text.pdf.PRStream)PDFStremObj);
if ((bytes != null))
{
try
{
System.IO.MemoryStream MS = new System.IO.MemoryStream(bytes);
MS.Position = 0;
System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS);
ImgList.Add(ImgPDF);
}
catch (Exception ex)
{
}
}
}
}
}
PDFReaderObj.Close();
}
catch (Exception ex) { throw ex; }
}
above coded getting file from ImagePath using FileStream to pdfobject and then memorystream to Image.
When I am trying to deflate one string to XML getting the error. can anyone help me out, if the string is wrong or any other thing I need to consider?
string output = DecompressString("H4sIAAAAAAAAAO1ay3LbIBTd9ys83hOcZrrxKP6CZpUuusUStulIoAFkxfn6XpCQ0NvWTDt+ZBUJ7vtwEQcneHv7ReN3Ko8spGrxkcRcrT8Ue10etE7XGOd5/pS/PAm5x99Xq2f8++3ne3igCUGMK014SJeFVqWRSfZEtWJWh+85TokkicIghRVLFPiCl5Cky823xSIIRZLGVFMkJNszTjTje8QiyjXTJ5RKquARRgU34qAwKbcgoWZH+rrUMqNL7NRSKokWctrPgmT6AFKfNCptFCbACEhpyUIjhcSRSgkGNu4BcaFR4TvAvZLOTEjimEoUMZXG5IQ4SaiLYGMcBnhMosgHn5+QrTO+rNATuHjpnQuLr1NBtCOxmoNR05iHV2GwqnQioOxgNSEcVANs33vQ3EC9wQE7EsiY8Z0ADTPeAPLyyrfrNIVCW7yShock4ywsrEZQO6nqiAZmm41Qp53FEKF2bZtJvmZU79ZFn65heA2bgLEJxlIRs/BUKZfqCwalNmsUQaVyIiOTRcZDwSNmYiAx2p7QEfISnqqNtJRQ/rABikaMzN9JrCuA11hpWTbxoojaWgC60WwnuJEJ7k8lIBY95QnDGoIQZlWQZBH7TxW0rm6+gttMne596YFhk+YNoHDPy/f6UeACvidp/Aj9AKkSrnIqbwSOO2+MW4ADzjYZOLSHdgk1OJAtVOMRWsXL9xbxufveuR58ijfgCo75jNOQihcN8pFhotZUkcB5eMGdOdCxoyjGN2FMSdt8QwKJXe2vHe+Q1ZoPuvn8QMu4LT+T9A8NAWREdDkKz1rYBbqB2oJQRs0AAdKuPUUjYCjkHLsuKi00kAGeJVvoBj89ZdoFmaXSrMjmeRXgy7UqJEAUqhO5qmmWQDQvYLN/ph/BZidX375CZdVGZlTaD4zD2XK3g5RqQ93RNk0fWbUeIR+TGhDKCTM49rLwcm7qpmNAafAGymyTbmVBlUIKxpXZJNnOGTkwhexuCn9dhNZ7ca00Q3u8oJ7UYDl9mQGRg4ij8eoYiW5p8KhjZ3XI65ZIWafIgIAnpkC9Qv/4PqX0YrrV1r+KBd7UvZ9Mpr5RrSCgJCKfn17ZDtZKM4qOu+ETyyRe93xSuRG8OieY8Q53+0+m9+JKtoE6lq9t4GqW1UV4fW0DV4UX45pKTh7jZwp7pvHy/UL9EX5aeWjUO5/88a95m3JMUYBe+uEbmDo8XMZfvChrCpNQpcieOlblcfJ2MMOS04GMe/EJFQfuTXlIu/TNzYww2oR8eIIIVoRmIUsJ1/YiY2S6S0v9QBolbUxU43Y7Hom/PT8vix8XJdEXk5dK73SdaKY0rN5Pc6UUl5dMWvAuMANyPTd0neU54sILpLxoODugCflzApt0GeDGfzttvv0F8ylWAP4kAAA=");
public static string DecompressString(string compressedString)
{
byte[] decompressedBytes;
var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString));
using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
using (var decompressedStream = new MemoryStream())
{
decompressorStream.CopyTo(decompressedStream);
decompressedBytes = decompressedStream.ToArray();
}
}
return Encoding.UTF8.GetString(decompressedBytes);
}
public static string DecodeGzip(string str)
{
byte[] gzBuffer = Convert.FromBase64String(str);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 0, gzBuffer.Length);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
int length;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
length = zip.Read(buffer, 0, buffer.Length);
}
var data = new byte[length];
Array.Copy(buffer, data, length);
return Encoding.UTF8.GetString(data);
}
}
I am stuck. Currently I am trying to simulate pulling a binary blob from a database that is supposed to be a TIFF image. I use this gist image.tif in the image variable to do so. I am pretty sure I am close to making this happen. It's that the issue probably has to do with how I am converting the string to byte array or something. Basically this application throws an exception stating that it can't create a PDF with 0 frames. At this point I must admit that I may be in over my head on this one. Could someone be so kind and help me the rest of the way with this one?
The code is included below:
using System;
using System.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Text;
namespace ConvertTifToPDFFile
{
class Program
{
static void Main(string[] args)
{
string image = "";
byte[] imageAsByteStream = Encoding.ASCII.GetBytes(image);
int imageByteStreamLength = imageAsByteStream.Length;
string base64EncodedImage = Convert.ToBase64String(imageAsByteStream);
imageAsByteStream = Encoding.ASCII.GetBytes(base64EncodedImage);
Stream imageStream = TiffImageSplitter.ByteArrayToMemoryStream(imageAsByteStream);
// Image splitImage = TiffImageSplitter.getTiffImage(imageStream, 1);
TiffImageSplitter.tiff2PDF(imageStream);
}
}
public class TiffImageSplitter
{
private static TiffImageSplitter tiff = new TiffImageSplitter();
public static void tiff2PDF(Stream imageByteStream)
{
PdfDocument doc = new PdfDocument();
int pageCount = getPageCount(imageByteStream);
for (int i = 0; i < pageCount; i++)
{
PdfPage page = new PdfPage();
Image img = getTiffImage(imageByteStream, 1);
XImage imgFrame = XImage.FromGdiPlusImage(img);
page.Width = imgFrame.PointWidth;
page.Height = imgFrame.PointHeight;
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i]);
xgr.DrawImage(img, 0, 0);
}
doc.Save("C:/temp/test.pdf");
doc.Close();
}
public static Image getTiffImage(Stream imageStream, int pageNumber)
{
MemoryStream ms = null;
Image returnImage = null;
try
{
ms = new MemoryStream();
Image sourceImage = Image.FromStream(imageStream, true, true);
Guid objGuid = sourceImage.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);
sourceImage.SelectActiveFrame(objDimension, pageNumber);
sourceImage.Save(ms, ImageFormat.Tiff);
returnImage = Image.FromStream(ms);
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
returnImage = null;
}
return returnImage;
}
public static MemoryStream ByteArrayToMemoryStream(byte[] bytestream)
{
MemoryStream stream = new MemoryStream();
stream.Write(bytestream, 0, bytestream.Length);
return stream;
}
public static int getPageCount(Stream imageStream)
{
int pageCount = -1;
try
{
Image img = Image.FromStream(imageStream, true, true);
pageCount = img.GetFrameCount(FrameDimension.Page);
img.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
pageCount = 0;
}
return pageCount;
}
}
}
IMPORTANT!!!! First of all, your example tiff isn't valid at all. It can't be read by any file editor. I have had to take these ones for testing.
Then, the code has couple errors:
1) I don't understand what've done with strings but reading files and BLOBs are same:
static void Main(string[] args)
{
//string image = "";
//byte[] imageAsByteStream = Encoding.ASCII.GetBytes(image);
byte[] imageAsByteStream = File.ReadAllBytes("../../../MARBIBM.TIF");
//int imageByteStreamLength = imageAsByteStream.Length;
//string base64EncodedImage = Convert.ToBase64String(imageAsByteStream);
//imageAsByteStream = Encoding.ASCII.GetBytes(base64EncodedImage);
Stream imageStream = TiffImageSplitter.ByteArrayToMemoryStream(imageAsByteStream);
// Image splitImage = TiffImageSplitter.getTiffImage(imageStream, 1);
TiffImageSplitter.tiff2PDF(imageStream);
}
2) method tiff2PDF should be like that
public static void tiff2PDF(Stream imageByteStream)
{
PdfDocument doc = new PdfDocument();
int pageCount = getPageCount(imageByteStream);
for (int i = 0; i < pageCount; i++)
{
PdfPage page = new PdfPage();
Image img = getTiffImage(imageByteStream, i); //<---HERE WAS ANOTHER ERROR, LOOK AT i
XImage imgFrame = XImage.FromGdiPlusImage(img);
3)
public static MemoryStream ByteArrayToMemoryStream(byte[] bytestream)
{
MemoryStream stream = new MemoryStream(bytestream);
//stream.Write(bytestream, 0, bytestream.Length);
return stream;
}
I am trying to create a function that will load a PNG from a URL into memory and then add an iTXt chunk with the keyword "openbadges" and some json data. I have every part working except when I execute
metadata.SetQuery("/iTXt/openbadges", "");
I get an exception:
Value does not fall within the expected range.
Here is the function:
private static byte[] CreateOpenBadge(BadgeAssertionEntity assertion)
{
using (var image = LoadImage(new Uri(assertion.Badge.ImageUrl)))
using (var imageStream = new MemoryStream())
{
image.Save(imageStream, ImageFormat.Png);
var pngDecoder = new PngBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
using (var badgeStream = new MemoryStream())
{
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(pngDecoder.Frames[0]));
var metadata = pngEncoder.Frames[0].Metadata as BitmapMetadata;
if (metadata == null)
throw new ApplicationException();
metadata.SetQuery("/iTXt/openbadges", "");
pngEncoder.Save(badgeStream);
return badgeStream.ToArray();
}
}
}
Any ideas what I am doing wrong?
The trick is to setquery() "/iTXt/Keyword" and "/iTXt/TextEntry", but you must use a char[] for the value of Keyword.
This is because internally "/iTXt/Keyword" wants a VT_LPSTR as the value type, and only a char[] will correctly convert into that.
Here's a sample that uses WIC to write a checkerboard png, including some iTXt metadata.
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace PNGEncoder
{
class Program
{
static void Main(string[] args)
{
var width = 256;
var height = 256;
var pngMetadata = new BitmapMetadata("png");
pngMetadata.SetQuery("/iTXt/Keyword", "keyword0".ToCharArray());
pngMetadata.SetQuery("/iTXt/TextEntry", "textentry0");
pngMetadata.SetQuery("/[1]iTXt/Keyword", "keyword1".ToCharArray());
pngMetadata.SetQuery("/[1]iTXt/TextEntry", "textentry1");
var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, null);
var pixels = new byte[width * height];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
pixels[y * width + x] = (byte)(255 * (((x >> 4) ^ (y >> 4)) & 1));
}
}
bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width, 0);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap, null, pngMetadata, null));
using (var stream = File.Create("checkerBoard.png"))
{
encoder.Save(stream);
}
}
}
}
I gave up on using PngBitmapEncoder. Instead I will just modify the png bytes directly. I have attached a class I made for this purpose incase others find it useful.
This class is heavily inspired by AShelly's response at Using Chunks in a PNG
I also use http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net for the crc hash.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Badger.Libraries.Hashing;
namespace Badger.Libraries.Images
{
public class Png
{
private readonly byte[] _header;
private readonly IList<Chunk> _chunks;
public Png(Uri imageUri)
{
_header = new byte[8];
_chunks = new List<Chunk>();
var webResponse = WebRequest.Create(imageUri).GetResponse();
using (var webResponseStream = webResponse.GetResponseStream())
using (var memoryStream = new MemoryStream())
{
if (webResponseStream == null)
throw new ArgumentException("invalid uri");
webResponseStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.Read(_header, 0, _header.Length);
while (memoryStream.Position < memoryStream.Length)
_chunks.Add(ChunkFromStream(memoryStream));
memoryStream.Close();
}
}
public void AddInternationalText(string keyword, string text)
{
// 1-79 (keyword)
// 1 (null character)
// 1 (compression flag)
// 1 (compression method)
// 0+ (language)
// 1 (null character)
// 0+ (translated keyword)
// 1 (null character)
// 0+ (text)
var typeBytes = Encoding.UTF8.GetBytes("iTXt");
var keywordBytes = Encoding.UTF8.GetBytes(keyword);
var textBytes = Encoding.UTF8.GetBytes(text);
var nullByte = BitConverter.GetBytes('\0')[0];
var zeroByte = BitConverter.GetBytes(0)[0];
var data = new List<byte>();
data.AddRange(keywordBytes);
data.Add(nullByte);
data.Add(zeroByte);
data.Add(zeroByte);
data.Add(nullByte);
data.Add(nullByte);
data.AddRange(textBytes);
var chunk = new Chunk(typeBytes, data.ToArray());
_chunks.Insert(1, chunk);
}
public byte[] ToBytes()
{
using (var stream = new MemoryStream())
{
stream.Write(_header, 0, _header.Length);
foreach (var chunk in _chunks)
chunk.WriteToStream(stream);
var bytes = stream.ToArray();
stream.Close();
return bytes;
}
}
private static Chunk ChunkFromStream(Stream stream)
{
var length = ReadBytes(stream, 4);
var type = ReadBytes(stream, 4);
var data = ReadBytes(stream, Convert.ToInt32(BitConverter.ToUInt32(length.Reverse().ToArray(), 0)));
stream.Seek(4, SeekOrigin.Current);
return new Chunk(type, data);
}
private static byte[] ReadBytes(Stream stream, int n)
{
var buffer = new byte[n];
stream.Read(buffer, 0, n);
return buffer;
}
private static void WriteBytes(Stream stream, byte[] bytes)
{
stream.Write(bytes, 0, bytes.Length);
}
private class Chunk
{
public Chunk(byte[] type, byte[] data)
{
_type = type;
_data = data;
}
public void WriteToStream(Stream stream)
{
WriteBytes(stream, BitConverter.GetBytes(Convert.ToUInt32(_data.Length)).Reverse().ToArray());
WriteBytes(stream, _type);
WriteBytes(stream, _data);
WriteBytes(stream, CalculateCrc(_type, _data));
}
private static byte[] CalculateCrc(IEnumerable<byte> type, IEnumerable<byte> data)
{
var bytes = new List<byte>();
bytes.AddRange(type);
bytes.AddRange(data);
var hasher = new Crc32();
using (var stream = new MemoryStream(bytes.ToArray()))
return hasher.ComputeHash(stream);
}
private readonly byte[] _type;
private readonly byte[] _data;
}
}
}
How can I get rid of the CA2202 warning (CA2202 : Microsoft.Usage : Object 'compressedStream' can be disposed more than once in method 'Compression.InternalDecompress(byte[])'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object) from the following code:
using (var compressedStream = new MemoryStream(inputData))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
I have tried getting rid of the "using" statement and replacing it with try/finally pattern but then I get CA2000 (CA2000 : Microsoft.Reliability : In method 'Compression.InternalDecompress(byte[])', call System.IDisposable.Dispose on object 'stream' before all references to it are out of scope). I have tried replacing the above code like this:
MemoryStream decompressedData = null;
MemoryStream stream = null;
GZipStream decompressor = null;
try
{
decompressedData = new MemoryStream();
stream = new MemoryStream(inputData);
decompressor = new GZipStream(stream, CompressionMode.Decompress, false);
stream = null;
int bytesRead = 1;
int chunkSize = 4096;
byte[] chunk = new byte[chunkSize];
while ((bytesRead = decompressor.Read(chunk, 0, chunkSize)) > 0)
{
decompressedData.Write(chunk, 0, bytesRead);
}
decompressor = null;
return decompressedData.ToArray();
}
finally
{
if (stream != null)
{
stream.Dispose();
}
if (decompressor != null)
{
decompressor.Dispose();
}
if (decompressedData != null)
{
decompressedData.Dispose();
}
}
This is what i use:
public class Compression
{
public Compression()
{
}
public byte[] Compress(byte[] buffer)
{
byte[] gzBuffer;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
zip.Close();
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
}
return gzBuffer;
}
public byte[] Decompress(byte[] gzBuffer)
{
byte[] buffer;
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
}
return buffer;
}
}
Or you can just add a pragma statement to your class
#pragma warning disable 2202
namespace Your.Namespace
{
...
}
#pragma warning restore 2202
This is what I ended up using - gets rid of both CA2000 and CA2202:
private static MemoryStream GetMemoryStream()
{
return new MemoryStream();
}
private static byte[] InternalDecompress(byte[] inputData)
{
Debug.Assert(inputData != null, "inputData cannot be null");
MemoryStream decompressedData = GetMemoryStream();
MemoryStream inputDataMemoryStream = GetMemoryStream();
GZipStream decompressor = null;
try
{
inputDataMemoryStream.Write(inputData, 0, inputData.Length);
inputDataMemoryStream.Position = 0;
decompressor = new GZipStream(inputDataMemoryStream, CompressionMode.Decompress, false);
int bytesRead;
int chunkSize = 4096;
byte[] chunk = new byte[chunkSize];
while ((bytesRead = decompressor.Read(chunk, 0, chunkSize)) > 0)
{
decompressedData.Write(chunk, 0, bytesRead);
}
}
finally
{
if (decompressor != null)
{
decompressor.Dispose();
}
}
return decompressedData.ToArray();
}
This is my attempt. It works and it avoids CA2202
/// <summary>
/// Compresses byte array to new byte array.
/// </summary>
public byte[] Compress(byte[] raw)
{
MemoryStream outStream = null;
GZipStream tinyStream = null;
byte[] retValue = null;
try
{
outStream = new MemoryStream();
tinyStream = new GZipStream(outStream, CompressionMode.Compress);
using (var mStream = new MemoryStream(raw))
mStream.CopyTo(tinyStream);
}
finally
{
if (tinyStream != null)
{
tinyStream.Dispose();
retValue = outStream.ToArray();
}
else if (outStream != null)
{
retValue = outStream.ToArray();
outStream.Dispose();
}
}
return retValue;
}