VSTO how add image from embedded resources - c#

I try import image to Word file from embedded resources. But i don't know how do it.
For example:
Globals.ThisDocument.InlineShapes.AddPicture(Resources1.ImageLogo.ToString());
But it isn't work, becose need full path to file as string.
Globals.ThisDocument.InlineShapes.AddPicture(#"C:\tmp\ImageLogo.jpg");
How can i import (add) image from embedded resources to Word file?
Thanks a lot!
I tried insert as "stream file", but my knowledge is not enough, to solve this problem.

The InlineShapes.AddPicture method doesn't accept streams or any other .net types. You need to save the embedded file to the disk and then specify the file path to the method. The first parameter is the path and file name of the picture.

Related

Output image with build

I am using MigraDoc, and would like to insert an image in a document. The simplest way to do this, is to provide a path for the image file (rather than having it in the resources). So what I would like to do is, to include an image as a file, alongside the other files in the build. Then MigraDoc can use the image based on relative path.
But I have no clue how to do this or if it is even possible. So can I make my project include an image file, outside the resources.
P.S. I know that there is a work around, where you include the image in the resources, save it in a temporary folder before MigraDoc has to use it. It just seems like a simpler design, if the image is always available.
P.P.S. There is a work around, to make MigraDoc use images from the project resources (see accepted answer).
As shown on the MigraDoc site you can use images you have as a byte[] to create PDF files. This can easily be used with images from resources (as shown on the site).
You just convert the image resource to a special string and pass that string where MigraDoc expects a filename.
static string MigraDocFilenameFromByteArray(byte[] image)
{
return "base64:" +
Convert.ToBase64String(image);
}
Link to official MigraDoc site:
http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
And MigraDoc can also use images that are stored in the program folder with your assemblies. You can use the Assembly class to find out where your assembly is stored and search that folder for images that should be there. This has nothing to do with MigraDoc, just some basic .NET features.

How To Get the images filename which that stored in resource file .resx

I needed a way to store images, so i found the best way is to create a resource file and store them in it, however i choose the option {Link at Compile Time} so it creates a resource folder in the project,
but when i call for an image it returns Bitmap Image file instead of the image URL or filename or image path, I need it this way because i'm developing a web API so i don't wanna send an image but i want instead to send its URL/path so is there a way to get image URL/path from resource file since it's already there
here is an image explain where it is in resource file:
The compiled code has the resource (the image in your case) itself embedded. The file path of the resource is evaluated at compile time but is not included in the compiled code.
Unless you are using a convention like resource name = image file name, you cannot get its path.
It is the very purpose of resources to make you independent of files.
You should probably include the image name or image url as string resource instead of the image itself.
Use "Add New String" instead of "Add Existing File...".
See also SO question: ASP.NET Get physical filepath from URL
If you really want to store images inside your assembly file and return that images URL's in web responses, you can apply some name encoding scheme and return some resource name based strings, for instance http: //www.mysite/stored-images/{resource-name}, where resource-name could be _6aa_Left.
Additionally you should implement route handler, that for path /stored-image/* will write into response stream your actual image bytes from appropriate resource when image will be required.

Can i retrieve images and shapes from word file and display them in PictureBox control of C#?

I want to extract images and shapes from word file and display them in picturebox control of C#.Is it possible? and if its then how ? i am searching it for many days but didn't find any solution. So plz help me
If you're working with MS Word documents in Open Office XML format (e.g. .docx files) you could try reading them out of the file directly. The .docx file is just a zip file and the images are contained in a media directory inside them. This page has more information on the file format.1
Alternatively I guess you could try automating Word using COM, to open the file and then retrieve the image that way. This approach has the disadvantage of spawning an instance of Word.
Either method will involve more detailed steps than I've described here, but should be do-able.
You can actually do it without going in to the trouble of using OpenXML or word interop libs.
This is what you need to do.
Rename your MyDoc.docx file with MyDoc.zip.
Then unzip the contents. You will get a very specific folder structure.
Ex: _rels, docProps,word, [Content_Types].xml
Go into word folder
Inside the word folder you will see a folder called media. That's where your images are.
Now you should be able to read and load these images in to picture boxes.

How to retrieve path for a file embedded in Resources (Resource Manager)

I am trying to retrieve file path for a html file that is embedded in resource (resx file) in VS2008 C# project. I want to give path of this file to native webbrowser control (PIEHtml) to be able to navigate (DTM_NAVIGATE) in my application. I know I can pass the string to this control using DTM_ADDTEXTW but since html text size is so big, I dont want to pass string to the control. I need to somehow extract the file path for this html file embedded inside resource manager.
I tried using but this does not give the file path of html inside assembly:
private ResourceManager resManager = new ResourceManager("AppName.FolderName.FileName", System.Reflection.Assembly.GetExecutingAssembly());
this.lbl.Text = resManager.GetString("StringInResources");
and also read Retrieving Resources in Satellite Assemblies but it did not solve my problem.
Can somebody please provide info as to how to achieve this ?
Wouldn't you first have to extract the resource? Or at least load it to memory?
Try this link: http://www.csharper.net/blog/getting_an_embedded_resource_file_out_of_an_assembly.aspx

Getting the type of file from the ASP.NET FileUpload control?

I want to get the type of file uploaded using the ASP.NET FileUpload control. When I upload a file, I want to be able to get the type of file uploaded, so I can assign a an icon to the file (such as a word, excel, pdf icon).
Here is the problem, I can't go off the file extension because a file could be called test.xxxxxxxx and be a valid pdf file, or a file might not have an extension.
The other option is to read the content-type, but with some of these appear not to be standard or in a simple to read format such as excel files, so is there another option to determine the file type?
I would review the process that names a PDF file "test.xxxxxxxx" without a ".pdf" extension. Most workflows that files have some kind of naming convention (manual or automated)
If you cannot read the extension, the file format will need to be detected by interogating markers that make it a known file format: eg: if the stream starts with or contains "%PDF" its a PDF.
If you don't know the extension, then you would have to know the file format of popular file types and then read the file and see if it matches one of your known formats. That doesn't sound optimal, though.
An easy way to check the true type of a file server side, using System.IO.BinaryReader, is described here:
http://forums.asp.net/post/2680667.aspx
and VB version here:
http://forums.asp.net/post/2681036.aspx
You'll need to know the binary 'codes' for the file type(s) you're checking for, but you can get those by implementing this solution and debugging the code.
Also note that when the BinaryReader is closed with the r.Close() statement, this will make FileUploader.HasFile = False

Categories