Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Server

Microsoft Dynamics AX 2009 : Working with Data in Forms - Adding images to records

8/26/2012 5:40:48 PM
Dynamics AX can store images to be used along with data. Images could be used for various purposes, like a company logo that is displayed in every printed document, employee photos, inventory images, and so on. One of the most convenient ways to attach images to any record is to use the Document handling feature of Dynamics AX. This does not require any changes in the application. But Document handling is a very generic way of attaching files to any record and might not be suitable for specific circumstances.

Another way of attaching images to records could be to utilize standard application objects, though minor application changes are required. The company logo in the Company information form is one of the places where an image is stored in exactly this way.

In this recipe, we will explore the latter option. As an example, we will add the ability to store an image for each inventory item. Although inventory items already have a similar option, which is a part of the Enterprise Portal Framework, it is very inventory specific and unlike our example cannot be used anywhere else.

How to do it...

  1. 1. Open the InventTable form in AOT and add a new MenuItemButton control to the bottom of its ButtonGroup with the following properties:

    Property Value
    Name CompanyImage
    MenuItemType Display
    MenuItemName CompanyImage
    Text Item image
    DataSource InventTable

  1. 2. Open Inventory management | Item Details and notice a new Item image button on the right:

  1. 3. Click on the button and then use the Change button to upload a new image for the selected item:

  2. 4. The Remove button could be used to delete an existing image.

How it works...

There are only three standard Dynamics AX objects involved here:

  1. 1. The CompanyImage table, which holds image data and the information about the record the image is attached to. The separate table allows easily hooking image functionality to any other existing table without modifying it or decreasing its performance.

  2. 2. The CompanyImage form, which shows an image and allows modifying it.

  3. 3. The Display menu item CompanyImage, which opens the form.

We only need to add the menu item to any form, like InventTable in our example and set its DataSource property against which we want to store an image. The rest is done by the application.

There's more...

The following two topics will explain how a stored image could be displayed as a new tab page on the main form and how a stored image could be saved back to file.

Displaying an image as a part of a form

Attaching an image to a record sometimes is not enough. I noticed that sometimes users find that clicking the button to see an image is not as convenient as having it on the same form. So here we will extend our recipe by displaying the stored image on a new tab page.

First, we need to add a new tab page to the end of the InventTable form's Tab control. This is where our image will be displayed. Set the tab page properties:

Property Value
Name Image
Caption Image

Add a new Window type control to the tab page. This control will be used for displaying the image. Set its properties:

Property Value
Name ItemImage
AutoDeclaration Yes
Height Column height
Width Column width

By setting Height and Width to Column height and Column width respectively, we ensure that the image control takes all possible space.

Add a new method to the InventTable form:

void loadImage()

{
Image img;
CompanyImage companyImage;
;
companyImage = CompanyImage::find(
InventTable.dataAreaId,
InventTable.TableId,
InventTable.RecId);
if (companyImage.Image)
imagesimagesdisplaying, as part of form{
img = new Image();
img.setData(companyImage.Image);
ItemImage.image(img);
}
else
{
ItemImage.image(null);
}
}

This method first finds a CompanyImage record, which is attached to the current record and then displays the binary data using the ItemImage control. If no image is attached the Window control is cleared to display empty space.

Override pageActivated() of the newly created tab page with:

public void pageActivated()

{;
super();
element.loadImage();
}

In here, we simply call the previously created method once the user selects the image tab page.

The form should look like following in AOT:

Now open Item Details from Inventory management and note the new tab page with the same image displayed:

Saving a stored image as a file

A couple of times in my practice it so happened that people lost images loaded into the system. Standard Dynamics AX does not provide a way of exporting loaded images. The images could be saved as files by creating and running a small job, which uses the Image class for image processing. In this topic, we will go a little beyond that and we will add a new button Save as to the Image form, which will allow us to save the loaded image as a file.

Let's add a new Button control to the CompanyImage form's ButtonGroup with the following properties:

Property Value
Name SaveAs
Text Save as

Create a new method on the form:

void saveImage()

{
Image img;
Filename name;
str type;
#File
;
if (!imageContainer)
{
return;
}
img = new Image();
img.setData(imageContainer);
type = '.'+strlwr(enum2value(img.saveType()));
name = WinAPI::getSaveFileName(
element.hWnd(),
[WinAPI::fileType(type),#AllFilesName+type],
'',
'');
if (name)
{
img.saveImage(name);
imagesimagessaving, as file}
}


					  

This method will present the user with Save As dialog allowing choosing of the desired file name for saving the current image. Note that imageContainer is a form variable, which holds image data. If it is empty, it means there is no image attached and we do not run any of the code. We also check the loaded file type to make sure that our Save As dialog filters only files of that type.

Override the button's clicked() with the following code to make sure saveImage() is executed once the user clicks the button:

void clicked()

{;
super();
element.saveImage();
}

In AOT the form should look like this:

Now when you open the Item image form a new button Save as is available:

Use this button to save the item image as a file:

The CompanyImage form is used system wide, so note that this button is now available system wide too.
Other -----------------
- Microsoft Dynamics AX 2009 : Working with Data in Forms - Coloring records
- SQL Server 2008 : Data Collector and MDW - Custom collection sets
- SQL Server 2008 : Physical server design - Server consolidation and virtualization
- Windows Server 2008 Server Core : Determining File and Directory Status with the ChkDsk Utility, Performing Boot Time Disk Checks with the ChkNTFS Utility
- Windows Server 2008 Server Core : Changing File and Directory Attributes with the Attrib Utility
- SQL Server 2008 R2 : Creating and Managing Databases - Creating Databases
- SQL Server 2008 R2 : Creating and Managing Databases - Database Files
- InfoPath with Microsoft Content Management Server Web Services : Creating the InfoPath Document
- InfoPath with Microsoft Content Management Server Web Services : Creating the ASP.NET Web Service
- Active Directory Domain Services 2008 : Configuring Attributes to Be Indexed for Containerized Searches, Configuring Attributes Not to Be Indexed for Containerized Searches
- Active Directory Domain Services 2008 : Configure Attributes to Be Copied When Duplicating Users, Configure Attributes Not to Be Copied When Duplicating Users
- Windows Server 2008 Server Core : Associating a Folder to a Drive with the Subst Utility, Displaying a Directory Structure with the Tree Utility, Managing the Volume Shadow Service with the VSSAdmin U
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 3) - Inventory Item Setup
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 2) - Item class setup
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 1) - Inventory Control Setup, Inventory sites, Unit of Measure Schedules
- Exchange Server 2007 : Designing Exchange Infrastructure, Integrating Client Access into Exchange Server 2007 Design
- Exchange Server 2007 : Designing Exchange Server Roles in an Exchange Environment
- Microsoft Dynamics CRM 4.0 : Infrastructure Design Considerations - Outlook Infrastructure Optimization
- Microsoft Dynamics CRM 4.0 : Infrastructure Design Considerations - Office Communication Server Setup
- BizTalk 2009 : Implementing a Scatter or Gather Pattern
 
 
Most view of day
- Using Application Deployment Tools : Deploying Applications Using RDS (part 2) - Packaging RemoteApp Applications
- Microsoft Word 2010 : Viewing a Document’s Statistics, Evaluating a Document’s Readability
- System Center Configuration Manager 2007 : Operating System Deployment - Boot Images
- Windows Phone 8 : Working with the Windows Phone Software (part 3) - Adding an Album to Your Phone,Adding a Musical Artist to Your Phone
- Microsoft Project 2010 : Tracking Progress on Tasks (part 1) - Saving a Project Baseline
- Microsoft Dynamics CRM 2011 : Using Advanced Find (part 5) - Using Edit Multiple Records and Assign Multiple Records from Advanced Find
- Configuring Startup and Troubleshooting Startup Issues : Understanding the Startup Process (part 3) - Kernel Loading Phase
- Microsoft Exchange Server 2010 : Completing Transport Server Setup (part 1) - Configuring Transport Limits
- Microsoft Project 2010 : Comparing Costs to Your Budget (part 3) - Associate Resources with Their Budget Type, Compare Budget Resource Values
- Editing Digital Video with Windows Live Movie Maker (part 8) - Sharing Your Videos - Publishing to the Web
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro