We have a function to export items and their images to an Excel file using an RDLC report. However, the user was always forced to export in smaller parts, because the dataset was growing too big.
After some checking, it seems that their images were all very large around 7 mega pixels…
So… I created a function to resize an image.
I’m not going to make this post too long, so here is the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
ResizeImage(VAR TempBlob : TEMPORARY Record TempBlob;UpsizeIfSmaller : Boolean;MaintainAspectRatio : Boolean;NewWidth : Integer;NewHeight : Integer) IF (NewWidth = 0) OR (NewHeight = 0) THEN EXIT; TempBlob.Blob.CREATEINSTREAM(Instr); Bitmap := Bitmap.Bitmap(Instr); IF NOT UpsizeIfSmaller THEN IF (NewWidth >= Bitmap.Width) OR (NewHeight >= Bitmap.Height) THEN EXIT; IF Bitmap.Width / NewWidth < Bitmap.Height / NewHeight THEN AspectRatio := Bitmap.Width / NewWidth ELSE AspectRatio := Bitmap.Height / NewHeight; IF MaintainAspectRatio THEN BEGIN NewHeight := Bitmap.Height / AspectRatio; NewWidth := Bitmap.Width / AspectRatio; END; CLEAR(TempBlob.Blob); TempBlob.Blob.CREATEOUTSTREAM(Outstr); Bitmap := Bitmap.Bitmap(Bitmap,NewWidth,NewHeight); Bitmap.Save(Outstr,ImageFormat.Jpeg); |
These are the variables:
1 2 3 4 5 |
Bitmap DotNet System.Drawing.Bitmap.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ImageFormat DotNet System.Drawing.Imaging.ImageFormat.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Instr InStream Outstr OutStream AspectRatio Decimal |
One more heads-up, but I’ll put an extra blog post for this:
You notice that I put a clear of the blob before creating the outstream to it.
If I don’t clear that, only the first part of the old blob data is overwritten, while the larger part of the previous blob remains in there…
Check this post for more information.
How can I change the compressionlevel of the JPEG?
https://msdn.microsoft.com/de-de/library/bb882583%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Hello Thomas,
I have started on a version with the compression inside NAV before, but then stopped due to some weird results. So far I haven’t needed the extra compression. But I imagine it should be possible.
The code sample you refer to is a good starting point indeed. Maybe take a look at my other blog post: .net to DotNet?