iamnobody
iamnobody

Reputation: 666

C# Interop.Word Adding an Image from project resource folder

Having some image problem with Interop.Word and C#. I want to add an image in the header of the document that I am going to generate. I have this code working perfectly

section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(@"C:\Logo.jpg", ref x, ref x, ref x, ref x, ref x, ref x, ref x);

Problem is that I can't have "C:\Logo.jpg" in the code since after publishing the project, there would most probably be no Logo.jpg in the C folder of the user. The image is already in my project's resource folder. I've used Image.FromFile("Logo.jpg") before but .AddPicture requires a string and not an image.

Any ideas?

-- edit --

Saw this over the net:

string anyPath = @"C:\logo.jpg";
Properties.Resources.logo.Save(anyPath);
section.Headers.[...].Shapes.AddPicture(anyPath, ...

but then I still get a generic error in GDI+ or ExternalException was unhandled.

Upvotes: 4

Views: 6320

Answers (5)

Takeo Nishioka
Takeo Nishioka

Reputation: 389

You can use project resource. Try this.

// Copy image to clickboard.
System.Windows.Forms.Clipboard.SetImage( Properties.Resources.your_resource_name);

//Select an location where you want to put the image
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();

// Page onto the document. wordApp is the instance of Microsoft.Office.Interop.Word.Application
wordApp.Selection.Paste();

Upvotes: 0

Himanshu
Himanshu

Reputation: 1

I happen to see this post and adding image from resource file is straight forward. Found a way and work for me without saving it to local machine.

 // PictureContentContorl                        
    Microsoft.Office.Tools.Word.PictureContentControl ct;
    // Removing the control if already exists
                       Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Controls.Remove("HeaderLogo");
    // adding control to a range rng
                            ct = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Controls.AddPictureContentControl(rng, "HeaderLogo");
                            ct.Image = Properties.Resources.my_logo;
                            ct = null;

May be it is helpful to someone.

Upvotes: 0

Arturo Turu
Arturo Turu

Reputation: 16

I'm stuck in the same situation, and I couldn't find a way to directly use the image resource with InlineShapes without having to save it before.

A nice folder to save these files is My.Computer.FileSystem.SpecialDirectories.Temp.

Upvotes: 0

Peter Høgstedt
Peter Høgstedt

Reputation: 11

I reluctantly - but successfully - did:

if (!File.Exists("singleright.png"))
{
object obj = Properties.Resources.singleright;
System.Drawing.Bitmap rs = (System.Drawing.Bitmap)(obj);
rs.Save("singleright.png");
}
insertionPoint.InlineShapes.AddPicture("singleright.png", ref LinkToFile, ref SaveWithDocument);

This does work - and it works on the foreign computer too when I published my Word - Addin. But I still don't like it. It should not be necessary to save an imagefile from the resources on the client computer, on should be able to use the resource directly.

Upvotes: 0

Kurru
Kurru

Reputation: 14331

Cant you use...

section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(@"Logo.jpg", ref x, ref x, ref x, ref x, ref x, ref x, ref x);

Use the relative location of the image file in the bin folder instead of an absolute positioned file

EDIT

You could use this to generate an absolute file location based on a relative link. Relies on using WinForms

string relative = @"images\Logo.jpg";
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
string absolute = System.IO.Path.Combine(fi.Directory.FullName, relative);

I don't think your updated method will work too great on vista/windows7 since they have various write permissions (maybe), not to mention, no one like random files being added to their C drive.

I think its bad practice for important files like this to not be in the application folder...

Upvotes: 1

Related Questions