Free Support Forum - aspose.com

How to set pictureframe size so picture appears at 100% scaling

I have the code below i am using to add a picture and pictureframe to a slide. The problem i have is i cant get the pictureframe to show the picture at 100% of its original size. The only example i found of adding pictures to slides has the picture width and height being multiplied by 4. When i then open up the result powerpoint and view properties of the pictureframe, it is scaling the image to 130% or soemthing like that, plus i can see its bigger. I always want the pictureframe to scale the picture to 100%. how can i do this no matter the original picture size.

Slide slide = pres.GetSlideByPosition(1);
//Creating a picture object that will be used to fill the ellipse
Aspose.Slides.Picture pic = new Aspose.Slides.Picture(pres, new MemoryStream(logo));

//Adding the picture object to pictures collection of the presentation
//After the picture object is added, the picture is given a uniqe picture Id
int picId = pres.Pictures.Add(pic);

//Calculating picture width and height
int pictureWidth = pic.Image.Width * 4;
int pictureHeight = pic.Image.Height * 4;

//Calculating the width and height of picture frame
//for whatever reason, aspose measurement unit is pixel * 8
int left = 50 * 8;
int top = 450 * 8;

//Adding picture frame to the slide
PictureFrame frame = slide.Shapes.AddPictureFrame(picId, left, top, pictureWidth, pictureHeight);

Kirk

Hi Kirk,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I am sorry for the delayed response.

Actually, in Aspose.Slides the slide size is 10'' x 7.5'', which is equivalent to 720 x 540 in screen resolution. There are 576 pixels drawn per inch for Aspose.Slides. This makes Aspose.Slides size to 5760 x 4320. Using simple mathematics, the scaling factor for an image having size with respect to screen coordinates is 8, i.e. 5760/720=8. So, if you want to add an image with actual size in Aspose.Slides picture frame the scale the frame size by 8 times of picture width and height. Please make, following changes in your code snippet to make things work for you as desired.

//Calculating picture width and height
int pictureWidth = pic.Image.Width * 8;
int pictureHeight = pic.Image.Height * 8;

Thanks and Regards,

if i set the size times 8, then the image comes out as 278% larger than original size. I have attached a .net 2010 sample solution. You will need to redo the main aspose test wpf app or make your own as there are some wcf calls i am making to pull files.

the asposehelper project is where i have methods to take in a file and logo, does some text replacing and then inserts the logo into the file. the mainwindow.xaml.cs BuildButton_Click event has some commented out code where i was pulling a local power point document under the project's "documents" folder, so use that local powerpoint as your basis. when it builds, the powerpoint document should have an image on the first slide that is at 100% of original size if it were working like my original office object code.

Hi Kirk,

I have worked with the source project shared by you. I have modified the part of code section where you have set the picture frame. Please note that according to my previous post, you need to multiply the image width and height with a factor 8, if you want the image in real size. Now coming to the question where by the generated image is 278 %. it is in reality 100 %, but the scaled image size, relative to original image size is 278 % of original size. But for viewing, we need to concentrate on dimensions and by scaling width and height by 8 will yield the same image as in original dimensions. For your kind reference, i have generated the image explaining the fact.

After the picture object is added, the picture is given a uniqe picture Id

int picId = pres.Pictures.Add(pic);

//Calculating picture width and height

int pictureWidth = pic.Image.Width * 8;

int pictureHeight = pic.Image.Height * 8;

//TODO: waiting for aspose to respond to question about pictureframe not scaling picture to 100%

//Calculating the width and height of picture frame

//for whatever reason, aspose measurement unit is pixel * 8

int left = 50 * 4;

int top = 450 * 4;

//Adding picture frame to the slide

PictureFrame frame = slide.Shapes.AddPictureFrame(picId, left, top, pictureWidth, pictureHeight);

pres.Write(output);

Thanks and Regards,

ok I might not have explained what i want the best then, so attached is a sample of what the powerpoint should look like and then a cutscreen of the image properties. Using the sample project i sent you before can you setup that code to produce the output as I have attached. I want the image to appear in the powerpoint at 100% scaling always and it should appear at the same place as in the sample powerpoint. The image in my sample powerpoint is not the same as the png file in the sample project, but it is the same size (360x245). The important part is the the scaling on the pictures once you get done should be 100%.

Hi Kirk,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I have worked on the problem specified by you. Actually, there may be two images with same resolution and there width height resolution in inches may be varying depending upon the size in inches. I have generated an image that has same resolution as of your image but the width and height of image in inches is not the one that your image has. For your kind reference, I have shared the image as well. You may please multiply the image width and height by a factor of 2.87 to achieve 100% image size.

I shall brief you a little about the theory behind this fact. The total slide width in inches 10'' and height is 7.5''. When you insert the logo image in slide without any scaling factor then the image in the slide has width and height 0.63'' and 0.43'' respectively. So, the generated image is 5.7333% of the original slide size. But the actual size of your image has width and height 1.8'' and 1.23''. The original image is 16.4% of the slide size in inches. So, by this the scaling factor for the desired image becomes 16.4/5.7333=2.87.

Hope it answers your problem.

Thanks and Regards,

I agree with the first sentence specifically if you didnt create an image at 200 dpi. was your image at a lower or higher resolution? all of our images are at 200 dpi, so in that case, the image i was using would always be the same. a 360x245 image at 200 dpi would always be 1.8"x1.23" in physical size.

1. would 2.87 always work no matter the size of the logo as long as the dpi of the logo is 200dpi or was that scaling specifically for a 360x245 logo.

2. i dont quite get what the total slide size has anything to do with the scaling of the image in your second sentence, but what i can say is that if I open up excel and insert the image with excel, it inserts at 100% scaling and 1.8"x1.23" as expected, so can i assume this issue is caused by your objects and how you handle inserting images then? funny thing is that i dont have to do any weird scaling with aspose.cells or aspose.word, so why is aspose.slides different?

Kirk

Hi Kirk,

Answering to the first part of your question that; yes you will have to use factor 2.87 as long as DPI is same and what ever resolution you may use. I used images with different resolutions but same DPI and they used same scaling factor 8. The slide size plays a role, since you have to project the image inside slide. Since, you have mentioned that you did not face any scaling problem while using Apose.Words and Aspose.Cells and this issue we will have to verify and address this scaling factor issue.

We appreciate your feedback and hope things will work for you.

Thanks and Regards,

thanks so much for your support so far. your product seems to provide us what we need so far, but we just wanted to work out some details on its use and/or any issues first.

the ppt scaling seems to work so far, so hopefully it won't be issue going forward now that i know what the value should be.

so i applied these same principles to a pptx file and get totally different results. I switched the ppt code i had over to using the PresentationEx and made some adjustments for how images are added based on some forum postings i found. Ran into a couple of big issues.

Using the attached solution, here are my findings:
(again if you want to actually run the project then you will need to get rid of invalid references and comment out code related to those references. I have a button called SampleButton in the mainwindow that will build all the local templates in the Documents folder.)

helper.cs has a method called BuildPowerpointX which is the pptx version of the same code you helped me with for ppt.

1. the result output file immediately throws errors when opening in powerpoint2007 that say some of the slides are invalid and replaces them with blank slides, but i can open up the original test.pptx just fine.

2. the image that is supposed to be inserted in the first slide is now pushed way down on the first slide. you can tell because the scrollbar is way small now and if you scroll down and look at the image properties, the image scaling is 797% vs the 100% when same code runs for ppt file.

3. there's a lot of formatting changes that seem to occur between the original test.pptx and the output/sample.pptx, like the last slide. some words are closer together or missing spaces, etc. a lot of the slides look totally different.

4. am i coding correctly for pptx when it comes to the picture being inserted into a slide, or is there a different way i should do it?

Kirk

Hi Kirk,

I have worked with the source project shared by you and have modified the following function to make things work for you. Actually, you need to replace text on portion level, if you intend to preserve the formatting properties, so I have modified the text replacement section for text frame to portion level replacement. As far as the image scaling factor in the generated slide is related, its coming 238% of original when no scaling factor is applied. You may please need to divide the image width and height by scaling factor 2.78 here to obtain the actual image. Actually, in PPT the resolution used at back end is 5760 x 4320 and all scaling factor logic was devised for that. In PPTX, we need to divide the same by 2.78 to obtain 100% size, since in PPTX the underlying resolution is 750 x 520 (real resolution). Also the left and right side of image frame start from top left corner of slide being 0,0 and bottom right being 750, 520. Hope it answers your query.

public static byte[] BuildPowerpointX(byte[] inputFile, byte[] logo)

{

Aspose.Slides.License license = new Aspose.Slides.License();

//license.SetLicense("Aspose.Total.lic");

using (Stream output = new MemoryStream())

{

PresentationEx pres = new PresentationEx(new MemoryStream(inputFile));

TextFrameEx[] textFrames = PresentationScanner.GetAllTextFrames(pres, true);

List<KeyValuePair<string, string>> fields = GetTemplateFields();

foreach (KeyValuePair<string, string> item in fields)

{

foreach (TextFrameEx textFrame in textFrames)

{

// textFrame.Text = ReplaceString(textFrame.Text, String.Format("[{0}]", item.Key), item.Value, StringComparison.CurrentCultureIgnoreCase);

//preserve the text formating properties. changing text on

//text frame level will reset the formating

foreach (ParagraphEx para in textFrame.Paragraphs)

{

foreach (PortionEx portion in para.Portions)

{

portion.Text = ReplaceString(portion.Text, String.Format("[{0}]", item.Key), item.Value, StringComparison.CurrentCultureIgnoreCase);

}

}

}

}

SlideEx slide = pres.Slides[0];

//Creating a picture object that will be used to fill the ellipse

ImageEx pic = pres.Images.AddImage(Bitmap.FromStream(new MemoryStream(logo)));

//Calculating picture width and height

//NOTE: this might not work in all cases depending on logo size. testing is needed

//dividing bt 2.78 will yeild actual sized image.

int pictureWidth = (int)(pic.Image.Width/2.78);

int pictureHeight =(int)(pic.Image.Height / 2.78);

//Calculating the width and height of picture frame

//0,0 means top left and 720,550 means bottom right

int left = 20;

int top = 380;

//Adding picture frame to the slide

slide.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, left, top, pictureWidth, pictureHeight, pic);

pres.Write(output);

return StreamToByteArray(output);

}

}

Thanks and Regards,

Thanks again for your continued support. This is so close now!

For pptx, I changed my code to divide the width and height by 2.78 and that fixed the scaling. I also had to remove the *8 for the left and top postition to make it appear in the right place.

fyi, i downloaded and installed the latest version of aspose.net as i noticed you had updated sometime in june and i didnt have that update. looks like there were a couple of minor breaking changes with naming, but easy to fix.

two small issues when looking at the output/sample.pptx from my sample project

1. the inserted logo image on the first slide has a border around it, while the one on the output/sample.ppt does not. how do i turn the border off for inserted images in pptx?

2. if you compare the last slide in the documents/test.pptx to the last slide of the output/sample.pptx, you will note that the "Thank You" in the text looks like "ThankYou" (no space). There were a couple of other slides where spaces got removed in the output file. Ideas on what caused this? bug?

Kirk

Hi Kirk,

I have tried to execute the project shared by you and observed that you are trying to replace the text on text frame level. As i mentioned and shared the code snippet earlier, please manipulate the fonts on portion level. Please use the shared presentation as input PPTX. I have just opened it an just reset the position of Thank you and it works fine now. You can also hide the picture frame border in PPTX using the following code snippet lines.

int iIndex= slide.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, left, top, pictureWidth, pictureHeight, pic);<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

PictureFrameEx pFrame = slide.Shapes[iIndex] as PictureFrameEx;

pFrame.LineFormat.FillFormat.FillType = FillTypeEx.NoFill;

Thanks and Regards,

i think the issue with the text on the last slide might have been my fault. somehow i was referencing an older version of aspose.slides dll. when i changed the reference to point to the latest version i have installed, then the text on the last slide ended up looking the same from the input to the output pptx.

that code for changing the LineFormat worked like a charm too, so i think i am good for now.

thanks for all your help!