web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Magno's Blog / Resize Image using DotNet

Resize Image using DotNet

Community Member Profile Picture Community Member

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:

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:

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.

Comments

*This post is locked for comments