How to Add image from URL to Slide?

Hi, could you provide the steps required to add an image into a powerpoint, where the image resides at a url such http://www.%3Cmyurl%3E.com/test.jpg

Is this possible?

I know it’s possible for your Aspose Words product, but i don’t seem the same objects in slides.

Dear Michael,

If I am not mistaken it is not possible in MS-PowerPoint and therefore also not possible in Aspose.Slides.

You can add an image to Powerpoint with an external URL…it’s very easy. Just open the powerpoint, and then choose to insert a picture. Then put in the URL, and choose “Insert as Link”. It works fine.

Maybe we’ll need to use automation for this.

Dear Michael,

Thanks for your help.

There is a workaround to add a linked picture with external URL using Aspose.Slides feature namely Shape Serialization.

The idea is to import a linked PictureFrame from a source presentation to a target presentation using serialization and change the link path.

Please see the code below and also see the source.ppt and output.ppt generated by the code; which I have attached for your reference.

When you will open the output.ppt, it will display the Aspose Logo retrieving it from internet as shown in code.

C#

//Source presentation to get the linked picture from
Presentation srcPres = new Presentation("c:\\source.ppt");

//Source slide with a single shape which is a linked pictureframe
Slide srcSld = srcPres.GetSlideByPosition(1);

//Serialize the pictureframe
Shape shp = srcSld.Shapes[0];
MemoryStream ms = new MemoryStream();
shp.Serialize(ms);

//Now once serialized, we can import into our target presentation
//Create a target presentation
Presentation target = new Presentation();
Slide targetSld = target.GetSlideByPosition(1);

//Import the serialized shape
ms.Position = 0;
targetSld.Shapes.Add(ms);

//Access the shape as a pictureframe
PictureFrame pf = targetSld.Shapes[0] as PictureFrame;

//Change its link path
pf.PictureFileName = "http://www.aspose.com/Images/aspose-logo.jpg";

//Write the target presentation on disk.
target.Write("c:\\output.ppt");

Thanks! This works great..however, the remaining issue is if the Target PPT is not a new powerpoint, but an existing one.

so the target ppt is

Presentation target = new Presentation(@"C:\temp\myown.ppt");

When i try to run this, i get the following error

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 47:         pf.PictureFileName = "http://www.aspose.com/Images/aspose-logo.jpg";
Line 49: 

How can i fix this? thanks again for your help.

never mind...my stupid mistake...just needed to get the right indexer of the pictureframe.

thanks again for your help.

Hi...could you provide this same code to add an image from a URL in VB? I tried several C-to-VB converters and they all kept coming up with errors. I tried to debug myself in VS2005, but couldn't get this to work...sorry, I'm sure it's something easy, but it's escaping me after a couple of hours...thanks VERY much...Have a great day! I REALLY appreciate it.

Patrick.

Here is the code.

VB.NET

'Source presentation to get the linked picture from
Dim srcPres As Presentation = New Presentation("c:\test\source.ppt")

'Source slide with a single shape which is a linked pictureframe
Dim srcSld As Slide = srcPres.GetSlideByPosition(1)

'Serialize the pictureframe
Dim shp As Shape = srcSld.Shapes(0)
Dim ms As MemoryStream = New MemoryStream()
shp.Serialize(ms)

'Now once serialized, we can import into our target presentation
'Create a target presentation
Dim target As Presentation = New Presentation()
Dim targetSld As Slide = target.GetSlideByPosition(1)

'Import the serialized shape
ms.Position = 0
targetSld.Shapes.Add(ms)

'Access the shape as a pictureframe
Dim pf As PictureFrame = CType(targetSld.Shapes(0), PictureFrame)

'Change its link path
pf.PictureFileName = "http:\\www.aspose.com/Images/aspose-logo.jpg"

'Write the target presentation on disk.
target.Write("c:\test\output.ppt")

Can anyone please provide me the java code to add image to slide ?

Thanks ,

Sam

HELP...doesn't work no matter what I try...I finally cut and pasted your entire code into a brand new .aspx page...only changed filenames because no source.ppt was available...updated my aspose.slides.dll to 3.1.0...still no go...it throws NO error messages...here's my code and source.ppt that I used...Help...my boss needs this functionality by next Friday or I'm toast...thanks. P.

Trace.IsEnabled = True

Dim license As Aspose.Slides.License = New Aspose.Slides.License()

'Set the license of Aspose.Slides to avoid the evaluation limitations

license.SetLicense("aspose.total.lic")

'Source presentation to get the linked picture from

Dim srcPres As Presentation = New Presentation("C:\Inetpub\wwwroot\OSCARPub\OSCAR_PPT\source.ppt")

'Source slide with a single shape which is a linked pictureframe

Dim srcSld As Slide = srcPres.GetSlideByPosition(1)

'Serialize the pictureframe

Dim shp As Shape = srcSld.Shapes(0)

Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()

shp.Serialize(ms)

'Now once serialized, we can import into our target presentation

'Create a target presentation

Dim target As Presentation = New Presentation()

Dim targetSld As Slide = target.GetSlideByPosition(1)

'Import the serialized shape

ms.Position = 0

targetSld.Shapes.Add(ms)

'Access the shape as a pictureframe

Dim pf As PictureFrame = CType(targetSld.Shapes(0), PictureFrame)

'Change its link path

pf.PictureFileName = "http:\\www.aspose.com\Images\aspose-logo.jpg"

'Write the target presentation on disk.

target.Write("C:\Inetpub\wwwroot\OSCARPub\OSCAR_PPT\output.ppt")

I forgot to mention that the output.ppt is the same as the source.ppt...

I just can't get the PictureFileName to add either a local file (non memory stream), http: version, or stream...when I do a read of the PictureFileName it says that it is the same as the alternate_text

I hope that helps...thanks for any help...P.

ANother update: I've tried manipulating the PictureFilePath at every point and nothing I do seems to make ANY difference to the output...and there are NO errors, but that property just doesn't seem to make ANY difference.

Any thoughts or fixes? Thanks.

Hello,

In my code, source & output ppt files are different,

Does your source ppt file contains a linked image just like my source.ppt attached above at post id 178976?

I think, not, your image might be embedded not linked. To test it try my source.ppt instead of yours.

Also, I have retested the above code with Aspose.Slides 3.1.1.1

Shakeel...thanks for your help...I figured out the problem...

The slashing in your image URL were the wrong DIRECTION and that made all the difference in the world!

Wow! P.

Dear Aspose staff,


I try to use the above code in vb.net of Visual Studio Express 2013 and aspose.slides 14.10.

However, it reports that “Serialize” is not a function/member of Shape object. Is “Serialize” not in Shape object anymore in v14.10?

If so, how can I add image from URL to Slide in v14.10?

Thank you.

Hi Bosco,

I have replied to your same query here. Please check it and in case you need any further assistance, please do let us know.

Thanks & Regards,

Dear Owais,

Yes, it works. Thank you very much.

Hi Bosco,

Thank you for the confirmation of resolution of your reported issue. Please feel free to contact support in case you need any further assistance.

Thanks & Regards,