Free Support Forum - aspose.com

PictureFormat not working properly

When I do an InsertImage using a PictureFormat in which the four crop
fields are all set to 0, I still get in Word a certain crop in the
picture. How come?



pictureFormat = new PictureFormat();



pictureFormat.CropTop = 0D;

pictureFormat.CropLeft = 0D;

pictureFormat.CropBottom = 0D;

pictureFormat.CropRight = 0D;



DocumentBuilder.InsertImage(image, widthInPoints, widthInPoints, pictureFormat);


I have tested InsertImage with the attached picture and have not noticed any cropping:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

string imageFileName = Application.StartupPath + "\\test.png";

PictureFormat pictureFormat = new PictureFormat();

pictureFormat.CropTop = 0D;

pictureFormat.CropLeft = 0D;

pictureFormat.CropBottom = 0D;

pictureFormat.CropRight = 0D;

builder.InsertImage(imageFileName, RelativeHorizontalPosition.Margin, 10, RelativeVerticalPosition.Margin, 10, 100, 100, WrapType.Through, WrapSide.Both, false, pictureFormat);

doc.Save(Application.StartupPath + @"\testInserImageCrop.doc");

Please supply a test project where the cropping displays itself.

Here’s an example console application. It reads a meta-file and inserts
it in a Word doc. If you check the meta-file you can see that it has no
margins. In the Word document is has margins.



using System;

using System.Collections.Generic;

using System.Text;



namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{


System.Drawing.Image
metaFile = new System.Drawing.Imaging.Metafile(@“c:\circle.emf”);





Aspose.Words.Document
document = new Aspose.Words.Document();



Aspose.Words.DocumentBuilder builder = new
Aspose.Words.DocumentBuilder(document);





double width =
Aspose.Words.WordConvert.PixelToPoint(metaFile.Width);



double height = Aspose.Words.WordConvert.PixelToPoint(metaFile.Height);





Aspose.Words.PictureFormat pictureFormat = new
Aspose.Words.PictureFormat();




pictureFormat.CropTop = 0D;

pictureFormat.CropLeft = 0D;

pictureFormat.CropBottom = 0D;

pictureFormat.CropRight = 0D;



builder.InsertImage(metaFile, width, height, pictureFormat);



document.Save(@“c:\foo.doc”);

}

}

}


Thank you for providing the code snippet and the image file. I have reproduced the problem. It is logged to our defect base as Issue #902. We will try to fix it in the next release which will be in 3-4 weeks.

BTW, I have found that the problem displays itself only with this particular image. I have tried insertion of other images in emf, wmf, png and gif format and there was no such effect with them. As a temporary workaround you can try to convert the image to bitmap format and insert it:

Image metaFile = Image.FromFile(Application.StartupPath + @"\Circle.emf");

metaFile.Save(Application.StartupPath + @"\Circle.png", ImageFormat.Png);

metaFile = Image.FromFile(Application.StartupPath + @"\Circle.png");

Thanks for your help, Vladimir

Sorry, cannot fix for this at the moment.

The margin around the picture is probably caused by the way the coordinate system/origin is specified in the drawing commands inside this particular metafile.

We just read the metafile into .NET Image object and it must somehow be different from what MS Word does with a metafile.