Resize Image using .NET

Resizing of images is often a requirement in Dynamics NAV if you integrate with a Webshop, PIM or importing images in and out of NAV.
Maybe you just want to save a small image on item, and use it on Page or in Reports, or export different sizes to a website etc.

Basic function
Below is an example where we load a image from file and resize it, and save it back to a file. But the input and out can also be a Stream, if coming or going to Blob, Media or and external .NET Api. (see som samples at the end of the post)

Create the following variables:

// Load the image from file
BitmapOriginal := BitmapOriginal.Bitmap('C:\path\original_image.jpg);
// Set the new max width or height in pixels
NewMaxSize := 200;
// Calculating the new dimensions
IF BitmapOriginal.Width > BitmapOriginal.Height THEN BEGIN
 AspectRatio := BitmapOriginal.Width / BitmapOriginal.Height; 
 NewWidth := NewSize;
 NewHeight := ROUND(NewSize / AspectRatio, 1, '='); 
 AspectRatio := BitmapOriginal.Height / BitmapOriginal.Width;
 NewHeight := NewSize;
 NewWidth := ROUND(NewSize / AspectRatio, 1, '=');
// Resizing the Image
BitmapResized := BitmapResized.Bitmap(BitmapOriginal,NewWidth,NewHeight);
// Saving the image to file as jpg
BitmapResized.Save('C:\path\resized_image.jpg', ImageFormat.Jpeg);

Alernative input


// Load from Media (new DataType i 2017
// Initialize .NET MemoryStream
MemoryStream := MemoryStream.MemoryStream();
// Export Stream from a Media field called Image in Item table (custom field)
// Load Stream into BitMap
BitmapOriginal := BitmapOriginal.Bitmap(MemoryStream);

// Load from File to MemoryStream (if image is to be used multiple times)
MemoryStream := SystemFile.OpenRead('C:\path\original_image.jpg');
// Load Stream into BitMap
BitmapOriginal := BitmapOriginal.Bitmap(MemoryStream);

Alternativ outputs

// Save resized image to MemoryStream
BitmapResized.Save(MemoryStream, ImageFormat.Jpeg);
// Set curser back to start, so MemoryStream is ready for read
MemoryStream.Position := 0;

// Save the MemoryStream to Media field
Item.Image.IMPORTSTREAM(MemoryStream, 'Image text', image/jpeg)  

// Save the MemoryStream to a BLOB

I often use .NET Streams (MemoryStream or FileStream) instead of the builtin InStream and OutStream, because .NET Streams have a lot of build in functions, and can integrate into all .NET objects and libraries, like this BitMap example.
And besides, they are fully  interchangeable with NAV streams. So I just start out with a .NET stream instead of having to convert a InStream or OutStream to a .NET Stream when needed.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s