Giant leap forward in reading metafiles for WPF and Silverlight

by abenedik 30. July 2010 23:22

Giant leap forward?

Check out the size of this blog post and you will see that I could not describe the new release of Ab2d.ReaderWmf and Paste2Xaml with any other words.

 

Both Ab2d.ReaderWmf and Paste2Xaml have been greatly improved. Ab2d.ReaderWmf is a library to import Windows metafiles (wmf) and Enhanced Metafiles (emf) into WPF application. Paste2Xaml is an application that is using Ab2d.ReaderWmf libraray and can be used to convert metafiles from files or clipboard into xaml for WPF and Silverlight.

First let me describe the new features of Ab2d.ReaderWmf library. Then I will describe the improvements of Paste2Xaml than besides new things from Ab2d.ReaderWmf also has some other great new features.


New features of Ab2d.ReaderWmf 5.0 at a glance
(major improvements are described below in more details):

Major improvements:

  • Improved reading detailed images from EMF+ records. Reading is now much faster. With added EmbeddedImagesData collection it is possible to get original image byte arrays with image format (png, jpg, etc.). It is also possible to skip reading EMF+ records with new ReadEmfPlusRecords.
  • Optimizing gradients defined in metafiles. Creating one Rectangle or Polygon with real LinearGradientBrush instead of numbers of Polygon that are defined in metafile. Optimizing gradients can be controlled with CombineGradientPolygons property.
  • Reading character spacing information that can be used to position each character in text. This functionality can be enabled or disabled by ProcessCharacterSpacing property.
  • Controlling the size of read metafile with CustomContentWidth, CustomContentHeight properties.



Improvements for Silverlight (when using with Paste2Xaml to create xaml for Silverlight; Ab2d.ReaderWmf  cannot be used in Silverlight projects):

  • Improved GetXaml method for Silverlight: added workaround for displaying Paths in Silverlight in Visual Studio 2010 designer - set Width and Height to Path element.
  • In Silverlight projects it is not possible to define PathGeometry’s data as string that would work in Silverlight and in Visual Studio 2010 designer. Therefore PathGeometry is now written as collection of PathFigures instead of data string.
  • Each PathGeometry is analyzed and if possible it is converted into RectangleGeometry.



Other fixes and improvements:

  • Fixed problems when running on 64-bit OS.
  • Fixed reading images that have width or height value defined as negative numbers - now the images are correctly mirrored.
  • Added IsClipboardMetafileAvailable, GetFromClipboard, GetGeometryFromClipboard methods to simplify getting metafiles from clipboard.
  • Added RemoveEmptyTextBlocks property - useful when copying from office documents to skip lots of empty TextBlock elements.
  • Added ProgressChanged event that can be used to get progress notifications while reading metafile. Please note that in order to show changes with the progress bar, it should be shown on another thread and not on the thread where the ReaderWmf is working. I am going to write a blog post about it based on the solution used in Paste2Xaml.
  • Improved ResourceDictionaryWriter class.  The AddFile and AddStream methods now work correctly. It is also possible to overwrite some of the methods to set some additional settings - for example number of decimals or using SilverlightXamlWriterSettings to create ResourceDictionary for Silverlight.
  • Some other improvements and fixes to improve reading metafiles that were sent as error report or feedback.


Now let me describe each of the major improvements in more detail.

Reading detailed images from EMF+ records:

Because normal metafile records do not enable storing images with transparencies they usually contain only lower quality images. The higher quality images are usually stored in EMF+ part of the metafiles.

The previous version of Ab2d.ReaderWmf library introduced possibility to read images from EMF+ records. But processing of those images could sometimes be very slow and could produce very big images (for example 4000 x 3000).

After a lot of effort I am now proud to say that the new version can much more accurately and much faster read the details images. Actually it is possible to get the original bytes that are used to define the image and the image file format. For example if a PowerPoint slide is created with a jpg image, it is now possible to get the same jpg image from Ab2d.ReaderWmf. Those data can be get from EmbeddedImagesData collection.

 

Optimizing gradients

Because metafiles do not provide a way to define gradients, the linear gradients are usually defined by series of Polygons or Rectangles where each of them has slightly different color. So instead of one Rectangle with LinearGradienBrush we got many Polygons.

And the number of polygons was not the biggest problem. Much bigger problem was when those polygons were rendered by WPF or Silverlight. There were many lines shown between polygons – as there would be an empty space between them. This was caused by the way WPF and Silverlight handle antialiasing. I have reported this issue to Microsoft Connect but it does not look that it will be fixed - see here.

To overcome those problems the new version of Ab2d.ReaderWmf analyses the read polygons and rectagles and if possible convert them into single Rectangle or Polygon with real LinearGradienBrush.

So instead of tens of polygons:

<Polygon Points="496,1494 496,1506 1678,1506 1678,1494" Fill="#FF000082"/>
<Polygon Points="496,1506 496,1517 1678,1517 1678,1506" Fill="#FF020082"/>
<Polygon Points="496,1517 496,1533 1678,1533 1678,1517" Fill="#FF040083"/>
<Polygon Points="496,1533 496,1544 1678,1544 1678,1533" Fill="#FF060083"/>
<Polygon Points="496,1544 496,1560 1678,1560 1678,1544" Fill="#FF080083"/>
<Polygon Points="496,1560 496,1571 1678,1571 1678,1560" Fill="#FF0A0083"/>
<Polygon Points="496,1571 496,1587 1678,1587 1678,1571" Fill="#FF0C0084"/>
<Polygon Points="496,1587 496,1599 1678,1599 1678,1587" Fill="#FF0E0084"/>
...

We get:

<Rectangle Width="1182" Height="2316" Canvas.Left="496" Canvas.Top="1494">
    <Rectangle.Fill>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#FF000082" Offset="0"/>
            <GradientStop Color="#FF68008E" Offset="0.3"/>
            <GradientStop Color="#FFB80067" Offset="0.6"/>
            <GradientStop Color="#FFFF0200" Offset="0.9"/>
            <GradientStop Color="#FFFF8200" Offset="1"/>
        </LinearGradientBrush>
    </Rectangle.Fill>
</Rectangle>

The rendered results are also much better:
Optimize gradients in ReaderWmf


Processing character spacing information

Texts in metafiles can have additional character spacing information that for each character in text define its offset. This can be used to create wider or more condensed text.

In WPF or Silverlight there is no such possibility except defining each character with its own TextBlock.

To create similar text sizes and also prevent dividing text, the new version of ReaderWmf can apply ScaleTransform to TextBlocks that need to display wider or more condensed text.

This functionality can be enabled or disabled with ProcessCharacterSpacing property.


Custom content Width and Height

By default the size of the read content is defined in the metafile. In this case the size is specified in GDI device units. This means that the size of read content is usually a few thousands x a few thousands (for example for A4 page copied from Microsoft Word the metafile size is 4253 x 4871).

Sometimes it can be unpractical to deal with sizes in thousands and font sizes in hundreds – for example when you need an 20 x 20 image.

In this case CustomContentWidth or CustomContentHeight can be set to specify how big the content size will be regardless of the size defined in metafile. In case only one of those properties is set, the aspect ratio of the read objects will be preserved.

To create a desired size, the Ab2d.ReaderWmf does not only add a RenderTransform to the root object but it actually changes all the positions, sizes and other data.



New features of Paste2Xaml application

Paste2Xaml main screen

The screenshot above shows the main user interface of the new Paste2Xaml application.

In the bottom right there are four new CheckBoxes. The first three are used to control some of the new features of Ab2d.ReaderWmf library. The last one is shown only when custom content size is remembered from previously read metafiles –details about Export dialog for more information about this.

Other improvements on the main screen are related to object selection and its manipulation.

On the upper left corner there is toolbox. The first button is used to toggle between selection and zoom mode. When it is unchecked, the zoom mode is controlled by other toolbox buttons. Note that Paste2Xaml is using ZoomPanel control that can be also bought from wpf-graphics.com. This means that you can have the same zooming functionality in your own WPF application (prices start from only $69 for single developer license).

When in selection mode it is possible to select individual objects with clicking with the mouse over the shown elements. It is also possible to hold the left mouse button down and move the mouse over objects – this is useful to preview how the objects are defined.

When an object is selected the three new buttons above objects TreeView become enabled.

The first one can be used to rename the selected object.

When the next button is clicked a Context Menu is opened. From there it is possible to convert the selected TextBlock or Rectangle into TextBox or ComboxBox.

This can be used to design forms form WPF or Silverlight applications in Microsoft Word, Excel or some other drawing application. Then you can select all objects in the form and copy and paste them into Paste2Xaml. There you can simply convert some predefined text elements or rectangles into TextBoxes or ComboBoxes and export the form into xaml. It is also recommended that you rename some elements and change the size of the form in the Export dialog (more about this later).

The last button above the TreeView can be used to delete the selected object.

A feature that is not so important but is very nice is a progress bar that is shown when bigger metafiles are loaded. It is using a new ProgressChanged event on ReaderWmf. I am going to write a blog post about it based on the solution used in Paste2Xaml.


Paste2Xaml Export dialog screen

The Export dialog also has some new parts.

There is a new Images tab where it is possible to preview the embedded images.

In the options part there are now new options to control the number of displayed decimals. In previous version the xaml was created with all numbers (except some transformations) displayed as integer number. This was enough because all the numbers in metafiles are also defined as integer numbers. But with the new version it is possible to change the size of the read objects – there integer numbers would not be enough. Note the xaml writer used by ReaderWmf removes the ending 0 decimals – for example “12.340000” is written as “12.34”.

The most important new feature of Paste2Xaml can be controlled by the Change Size options. There it is possible to enter new width or height and change the size of the read elements. As with CustomContentWidth and CustomContentHeight properties in Ab2d.ReaderWmf the change affects all the positions, sizes and other data of read objects. This is very useful to bring the size of the read elements into common sizes for WPF and Silverlight applications.

To change the size first select the “Width” or “Height” radio button, than enter the desired value and click “Change size”.

If you would like to use the same custom size with other metafile also, check the “Remember custom size for other metafiles” CheckBox. This will immediately save the setting (settings will be preserved when the Paste2Xaml is closed). When this checkbox is checked and the Export dialog will be closed there will be a new “Use custom width / height” CheckBox on the main screen. It is used to see what custom size will be used to read the metafile and also to remove the saved custom size settings.


That is about it.

I hope you will find the new features useful.


As usual the new version can be downloaded from User Account page (for commercial users) or from my Downloads page (for evaluation version).

Tags: , ,

ReaderWmf