Skip to content

Commit

Permalink
Dispose SKBitmap after Decode calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Dec 29, 2023
1 parent 3cf18a4 commit 83c9500
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
24 changes: 14 additions & 10 deletions PDFtoZPL/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ public static async IAsyncEnumerable<string> ConvertPdfAsync(Stream pdfStream, b
/// <returns>The converted <see cref="SKBitmap"/> as ZPL code.</returns>
public static string ConvertBitmap(string bitmapPath, BitmapEncodingKind encodingKind = BitmapEncodingKind.HexadecimalCompressed, bool graphicFieldOnly = false, bool setLabelLength = false, byte threshold = 128, DitheringKind ditheringKind = DitheringKind.None)
{
return ConvertBitmap(SKBitmap.Decode(bitmapPath), encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
using var bitmap = SKBitmap.Decode(bitmapPath);
return ConvertBitmap(bitmap, encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
}

/// <summary>
Expand Down Expand Up @@ -527,11 +528,13 @@ public static string ConvertBitmap(Stream bitmapAsStream, bool leaveOpen, Bitmap
using var memoryStream = new MemoryStream();
bitmapAsStream.CopyTo(memoryStream);
memoryStream.Position = 0;
return ConvertBitmap(SKBitmap.Decode(memoryStream), encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
using var bitmap = SKBitmap.Decode(memoryStream);
return ConvertBitmap(bitmap, encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
}

bitmapAsStream.Position = 0;
return ConvertBitmap(SKBitmap.Decode(bitmapAsStream), encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
using var bitmap2 = SKBitmap.Decode(bitmapAsStream);
return ConvertBitmap(bitmap2, encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
}

/// <summary>
Expand All @@ -546,7 +549,8 @@ public static string ConvertBitmap(Stream bitmapAsStream, bool leaveOpen, Bitmap
/// <returns>The converted <see cref="SKBitmap"/> as ZPL code.</returns>
public static string ConvertBitmap(byte[] bitmapAsByteArray, BitmapEncodingKind encodingKind = BitmapEncodingKind.HexadecimalCompressed, bool graphicFieldOnly = false, bool setLabelLength = false, byte threshold = 128, DitheringKind ditheringKind = DitheringKind.None)
{
return ConvertBitmap(SKBitmap.Decode(bitmapAsByteArray), encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
using var bitmap = SKBitmap.Decode(bitmapAsByteArray);
return ConvertBitmap(bitmap, encodingKind, graphicFieldOnly, setLabelLength, threshold, ditheringKind);
}

/// <summary>
Expand All @@ -572,14 +576,14 @@ private static string ConvertBitmapImpl(SKBitmap pdfBitmap, BitmapEncodingKind e
SKBitmap inputBitmap = pdfBitmap;
SKBitmap? bitmapReplacement = null;

if (ditheringKind != DitheringKind.None)
{
bitmapReplacement = pdfBitmap.ToMonochrome(threshold, ditheringKind);
inputBitmap = bitmapReplacement;
}

try
{
if (ditheringKind != DitheringKind.None)
{
bitmapReplacement = pdfBitmap.ToMonochrome(threshold, ditheringKind);
inputBitmap = bitmapReplacement;
}

// first convert the bitmap into ZPL hex values (representing the bitmap)
string bitmapAsHex = ConvertBitmapToHex(inputBitmap, threshold, out int binaryByteCount, out int bytesPerRow);
string bitmapPayload;
Expand Down
65 changes: 34 additions & 31 deletions WebConverter/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,37 @@ private async Task Submit()

await Task.Factory.StartNew(() =>
{
SKBitmap inputToConvert;
SKBitmap? inputToConvert = null;

if (Model.File?.ContentType == "application/pdf")
try
{
inputToConvert = PDFtoImage.Conversion.ToImage(
Model.Input,
leaveOpen: true,
password: !string.IsNullOrEmpty(Model.Password) ? Model.Password : null,
page: Model.Page,
dpi: Model.Dpi,
width: Model.Width,
height: Model.Height,
withAnnotations: Model.WithAnnotations,
withFormFill: Model.WithFormFill,
withAspectRatio: Model.WithAspectRatio,
rotation: Model.Rotation,
antiAliasing: antiAliasing,
backgroundColor: backgroundColor
);
}
else
{
using var memoryStream = new MemoryStream();
Model.Input.CopyTo(memoryStream);
memoryStream.Position = 0;
inputToConvert = SKBitmap.Decode(memoryStream);
}
if (Model.File?.ContentType == "application/pdf")
{
inputToConvert = PDFtoImage.Conversion.ToImage(
Model.Input,
leaveOpen: true,
password: !string.IsNullOrEmpty(Model.Password) ? Model.Password : null,
page: Model.Page,
dpi: Model.Dpi,
width: Model.Width,
height: Model.Height,
withAnnotations: Model.WithAnnotations,
withFormFill: Model.WithFormFill,
withAspectRatio: Model.WithAspectRatio,
rotation: Model.Rotation,
antiAliasing: antiAliasing,
backgroundColor: backgroundColor
);
}
else
{
using var memoryStream = new MemoryStream();
Model.Input.CopyTo(memoryStream);
memoryStream.Position = 0;
inputToConvert = SKBitmap.Decode(memoryStream);
}


using (inputToConvert)
{
Model.Output = PDFtoZPL.Conversion.ConvertBitmap(
inputToConvert,
encodingKind: Model.Encoding,
Expand All @@ -178,10 +179,12 @@ await Task.Factory.StartNew(() =>

Model.OutputPreviewImage = new MemoryStream();

using (var monochromeBitmap = inputToConvert.ToMonochrome(Model.Threshold, Model.Dithering))
{
encodeSuccess = monochromeBitmap.Encode(Model.OutputPreviewImage, SKEncodedImageFormat.Png, 100);
}
using var monochromeBitmap = inputToConvert.ToMonochrome(Model.Threshold, Model.Dithering);
encodeSuccess = monochromeBitmap.Encode(Model.OutputPreviewImage, SKEncodedImageFormat.Png, 100);
}
finally
{
inputToConvert?.Dispose();
}
}, TaskCreationOptions.LongRunning);

Expand Down

0 comments on commit 83c9500

Please sign in to comment.