We'll use the support of .ico
files — Windows native icons — as an example of extending the graphic capabilities of XMLmind XSL-FO Converter.
Implementing a IGraphicFactory is straightforward. You just need to implement 4 methods: GetInputFormats
, GetOutputFormats
, CreateGraphic
and ConvertGraphic
.
Excerpts of samples/dotnet/IcoGraphicFactory.cs
:
... using XmlMind.FoConverter; public class IcoGraphicFactory : IGraphicFactory { private static readonly string[] inputFormats = { "image/vnd.microsoft.icon" }; private static readonly string[] outputFormats = { "image/png" }; public string[] GetInputFormats() { return inputFormats; } public string[] GetOutputFormats() { return outputFormats; } ...
GetInputFormats returns the list of the media types (AKA MIME types) that the | |
GetOutputFormats returns the list of the media types that the |
... public IGraphic CreateGraphic(string location, string format, object clientData, IGraphicEnv env) { Image image = LoadImage(location); double xRes = 0; double yRes = 0; if ((image.Flags & ((int) ImageFlags.HasRealDpi)) != 0) { xRes = image.HorizontalResolution; yRes = image.VerticalResolution; } return new Graphic(location, format, image.Width, image.Height, xRes, yRes, GraphicType.Raster, clientData); } private static Image LoadImage(String location) { Image image = null; Stream stream = GraphicUtil.OpenStream(location); try { image = Image.FromStream(stream); } finally { stream.Close(); } return image; } ...
Method CreateGraphic basically needs to parse the image file found at absolute URI Note that argument | |
Class Graphic is a simple implementation of interface | |
In order to obtain the dimension of the image ( |
... public IGraphic ConvertGraphic(IGraphic graphic, string format, double xScale, double yScale, object clientData, IGraphicEnv env) { int width = graphic.GetWidth(); int height = graphic.GetHeight(); double xRes = graphic.GetXResolution(); double yRes = graphic.GetYResolution(); Image image = LoadImage(graphic.GetLocation()); if (xScale != 1) { width = (int) Math.Round(width * xScale); } if (yScale != 1) { height = (int) Math.Round(height * yScale); } Bitmap bitmap = new Bitmap(image, width, height); if (xRes > 0 && yRes > 0) { bitmap.SetResolution((float) xRes, (float) yRes); } string outPath = env.CreateTempFile(".png"); bitmap.Save(outPath, ImageFormat.Png); return new Graphic(GraphicUtil.FilenameToLocation(outPath), format, width, height, xRes, yRes, GraphicType.Raster, clientData); } ...
Method ConvertGraphic is invoked to convert its Note that argument | |
The converted image file must be stored in a temporary file created using method IGraphicEnv.CreateTempFile. Such temporary files are automatically deleted when no longer needed. | |
Class GraphicUtil contains several useful helper functions, among them |
public static int Main(string[] args) { ... GraphicFactories.Register(new IcoGraphicFactory()); ... converter.SetInput(inUri); converter.SetOutput(outPath); converter.Convert(); ...
For an implementation of |