How to use a Resource Image in Word document? - c#

I'm working on a project that will auto-generate Word and HTML reports. If they don't provide an Image to use in the header of the Word report, the customer wants to use their logo. I have the logo stored in the resources of one of the projects as a .jpg.
The method to add a picture to a range needs the path as a string to the image, without any overloads. I know that when a file is added to the resources of the project, it doesn't exist as it gets embedded inside the .dll that gets created. Is there no way to utilize that embedded resource in that method?
Do I need to copy that file as part of the install to the directory? I'm thinking that may be the easiest solution, however what should I do for testing purposes?

I was making this a lot harder than it needed to be.
All you have to do is make an image object from the resource:
Image img = Data.Properties.Resources.ImageFile;
Then after that, all you have to do is save it:
img.Save(#"C:\DestinationFolder\Image.jpg");
Now you have the file to use however you want. It won't matter if you're in debugging or not (as far as your Application.Run location) in the event you have it in a subfolder in your project (as you should), but wouldn't necessarily have it in the same path at the end.
I was way over-thinking this the other day when I asked this question. Hopefully I'll be able to help someone else out who's having this same issue.

Related

c# Storing resources(specifically images) in folder structure

If I want to add pictures, I have to put those into the resources and access them from the picturebox.Image property by using this:
AddPicturesFromOtherFolders.Properties.Resources.myPicture
Thats the only way I know. That works fine if I have 10 or so images, but what if I had 500 images? Nobody could keep track of anything. So I would like to structure these hyperthetical 500 Images in a folder structure which I could then access with something like:
pictureBox1.Image= ../../Assets/img/specialImages/myImage.png
That would be very neat, but I have found no way, that involves 100% C# code.
I would be wuite grateful, if you could help me.
Have a nice day,
Alexander Lenssen
You could use Image.FromFile and load the image from any file you have stored in your file system. For example:
pictureBox1.Image.FromFile(#"D:/Assets/img/specialImages/myImage.png");
There is no way that involves 100% C# code. At least some Compiler options or Setup actions are nessesary. But the first question is even where to store it: Programm Directory or UserProfiles?
Asuming these images are static (will only change when a installer runs), you can just store them into the Programm Directory. And from there deploy them with the rest of the code. Getting them Into the Output directory is not that difficulty. Visual Studio has options for that: https://msdn.microsoft.com/en-us/library/0c6xyb66.aspx You could go further, like having a Shared Repository for Images (i.e., most Photoshop programms have one Content Folder under Programms).
You can go as far as "soft linking" them, wich means you can have one actuall folder on your disk that will be copied/synched into the output directories on any buil.
If you need to Update those Images them on the fly (without adminsitrative rights), stuff becomes more complicated. You can still do it via the SpecialFolders. CommonApplicationData seems like the right place to put this kind of stuff. Even Steam and Minecraft's old Java Launcher do quite some storage there. Not to mention every WebBrowser.

How to create a dll just like this with a change of text?

I have added the link to the dll.
I want to create the same kind of dll but with a change inside the file containing
"O:\76\d3c7d16586ecd8fdc7c8781a.pdf" - I want to change this link inside the dll.
Can someone give me a full source code to make this kind of dll ?
Its around 2 KB in filesize.
http://www.datafilehost.com/d/589b215c
Uncheck "Use our download manager and get recommended downloads" before downloading the file.
You could simply put a big block of text (the size of the maximum URL you expect to use) into the globals and then later open the dll, insert the string, and save a copy. I apologize for not putting code here, but it doesn't seem necessary.

Display Image in Crystal reports Using URL

I want to show image in a crystal report.
Scenario is something like this.
I have a database where my path of an image is persisting.
eg ftp://Images/1.jpg
Now i want to repeat this image in a crystal report.
When i fills my datatable it shows me complete url. When i displays this field in GridView i uses imageBox to display my image and it works for me very fine.
But when i tries to do the same with crystal reports, it starts me showing image path as it is. Now here instead of path i want an image to be displayed.
OK, so the trail of tears to show images in CR report over the web is as follows:
1) The following is assumed:
a) CR 2008 aka CR 12. I know not about earlier versions, but XIR2 (11.5) may work.
b) Web display of pictures in reports is desired, with local workstation development and preview
c) IIS, ASP.NET application, .NET 4.0
d) Crystal Reports installed correctly ( that is an entirely different discussion, but suffice it to say, you better have a folder named aspnet_client with subdirectories as follows:
**system_web
4_0_30319
crystalreportviewers12**
etc.
that is parallel to the location of the web application. There's a lot more - but not here though...
e) Images are like photos or whatever but are reasonably sized and not too huge bytes-wise.
f) Thumbnails for each image exist, or a default thumbnail file is available.
g) They are JPG, PNG, or BMP images. Otherwise, you are out of luck AFAICT. If they are documents like Word, PDF, etc. which you wish to show in the same list, you will need a thumbnail for those too. But let's stay on the image topic...
h) You have images organized into a folder hierarchy on your web server, or accessible to your web server, but in any event, accessible to the website. Let's assume they are all under a main location D:\MyDocuments
I HAVE NOT TRIED THIS REFERRING TO AN FTP SITE LIKE THE ORIGINAL QUESTION BUT I DOUBT IT WILL WORK.
2) You need a database table or other sort of repository accessible to the webserver to register your images. The DB format is flexible, but we will assume it is a list keyed to your primary domain of interest, where you have 0:N images per main item, say, pictures of a residence, or pictures of a bridge, or pictures of a home inspection. This table has either a full path to your files, or a relative path, or a folder location plus a dedicated file name column. Whatever, but they have to make a file path like:
D:\MyDocuments\folderA\folder1\area51\whatever\myfile.png
so the database holds the whole thing , or part of it, or the bits, or whatever.
3) The root folder is D:\MyDocuments when viewing reports locally/standalone/not with a browser. This is an arbitrary name but keep track of it for now.
4) You register that root folder so CR can find it. This can either be hard-wired into your reports (bad) or looked up from an INI file (ummm, OK) or in a database field (why not,since you are registering your images anyway?) or passed as a parameter to your reports that want to show pictures or document links (simple, but what happens when you deploy to some other file system?)
5) In your report that shows the pictures, and I am assuming here N / item of interest as described in (2) above, you have a picture inserted with the CR designer. Link it to some really bogus picture or a default image so you can tell if you are resolving the file names....
6) The picture thumbnail's path is retrieved from the database and assembled as necessary, with BACK SLASHES, into a file name. It will be stored in a Shared StringVar FullQualifiedThumbnailFileName (let's say) in the report and consists of the document root, which you made available to the report in step (4) and stored in a dedicated Shared StringVar DocRoot (let's say) PLUS the calculated file name. So formula field FullyQualifiedThumbnailFileName looks like:
{#DocRoot} & FolderLocationFromDB & ThumbnailFileNameFromDB or in real life:
D:\MyDocuments\folderA\folder1\area51\whatever\tn_myfile.png
7) So now you have a thumbnail file name. Drop it anywhere on your draft report so you can see what it is resolving too during design. Do the exact same thing to refer to the REAL file name and make a variable called FullyQualifiedThumbnailFileName. It should be openable with a picture viewer if it has been constructed correctly. Drop it somewhere too so you can read it and use it to test.
8) Right-click on the picture object on the report, pick Format Graphic, click the picture tab, and open the formula icon for the picture location.
Before you start whining at me please look at the assumptions at the top - I have no clue which earlier versions of CR support this, or if they do it differently.
Then, in the formula editor, enter the following:
{#FullQualifiedThumbnailFileName} which you created just a minute ago. Your thumbnail is a Windows DOS path to a local file name on the webserver or your development workstation.
9) Now add a parameter to your report, or make a formula variable, or whatever, that consists by default of the string "file://". This will be REPLACED at runtime with the httpContext.Current.Session application root but we'll get to that in a minute. I guess you could have done this earlier.... Name this WebURLRoot
10) Create a formula field called txtImageURL Anyway, the name is up to you but guess what goes here? Something like the following:
if lowercase( {#WebURLRoot} ) = "file://" THEN
{#WebURLRoot} &
REPLACE( {#txtDocumentFileFullyQualifiedName},"/","\")
else
URLENCODE( {#WebURLRoot} &
REPLACE(
{*DocumentFileNameFromYourSource*}
,"\","/") )
The appending of file name DocumentFileNameFromYourSource to WebURLRoot works for me because I have relative paths in my situation that do NOT include the DocRoot. Your situation may be different. In any event, in standalone mode, this variable should resolve to:
file://D:\MyDocuments\folderA\folder1\area51\whatever\myfile.png
where this is not a thumbnail. At runtime on the web, it should resolve to:
http://somewebhost/website/folderA/folder1/area51/whatever/myfile.png
because we are going to supply http://localhost/website to the variable WebURLRoot somehow.I did it using a parameter passed from the web application. It could be looked up or hardwired but remember, what happens if the website gets relocated?
Put {#txtImageURL} into the hyperlink calculated file name formula, and click the option to indicate that it is coming from a website on the internet AKA your local development server or whatever.
In standalone mode, the file strings in txtImageURL have backslashes. At runtime, they are set to forward slashes for the full file name. The URLEncode function from Crystal makes them nice for web purposes.
Again drop txtImageURL onto your development surface until it is straight.
11) You now have a CR RPT with
a) A variable holding the Windows root of your documents tree C:\MyDocuments, which you supplied to the report by whatever means. I store it in the database my application connects to.
b) A variable holding the Window path to your thumbnail, constructed from the thumbnail name in the database plus the document root
c) A variable holding the Windows path to your real file name, again constructed from the actual file name in the database plus the document root
d) A web site URL stem which is file:// during development and http://localhost/website/ at runtime for the website. You are passing this
e) A working URL for the image file combining the web site URL stem with the actual file.
Good so far? Grab a beer. Maybe 2.
C# changes
1) OK, so we need to tweak our world to pass the web site URL stem to a report at runtime as a parameter. You cannot do this over the web using the Crystal Reports Viewer. Assuming you have followed one of the many examples available for how to load, parameterize and show a report from a web application, and I think you can find these with Google, make sure you do this in your application somewhere. I located mine in Global.asax.cs in the Session_Start event which seems highly reasonable according to the authoer... Please note that credit is due to the referenced URL personage... :
// so Crystal can receive the APP_Path as an argument
// Code that runs when a new session is started
// http://aquesthosting.headtreez.com/doc/d9ccf4d8-1873-469e-9dca-815e5854b963
string appPath = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
if (appPath == "/") //a site
appPath = "/";
else if (!appPath.EndsWith(#"/")) //a virtual directory i.e. in a subfolder
appPath += #"/";
Session["APP_Path"] = appPath; //stores the value to a session variable for us to use
2) Now where you create the parameters for your report, make sure to pass Session["APP_Path"] as a parameter something like this:
// START CHANGE
// this next check seems unlikely
if(! (HttpContext.Current.Session == null))
{
// pass HttpContext.Current.Session["APP_Path"].ToString() as a parameter
if (!(String.IsNullOrEmpty(HttpContext.Current.Session["APP_Path"].ToString())))// set in Global.asax.cs Start_Session
exporter.Arguments.Add(exporter.Arguments.Count, HttpContext.Current.Session["APP_Path"].ToString()); // to last parameter position
else
exporter.Arguments.Add(exporter.Arguments.Count, String.Empty); // or nothing to last parameter position
// end change
}
where exporter.Arguments holds the parameters for my reports. Your situation will doubtlessly be different. One somewhat important thing is to put this parameter either ALWAYS FIRST or ALWAYS LAST, such that other parameters are not screwed up by it. Use the very same order for parameters in the report itself. The parameters apparently associate values by name anyway but I think mish-mashing them is a bad idea and ultimately confusing.
3) So now you run your reports standalone, and when prompted for the web site URL stem you enter file://. When the same exact report is run over the web, the application sends it whatever the website stem is but something like http://localhost/website/ .
GOTCHAS:
1) During development of this, show your variables on your reports. Make sure that for the thumbnails you are using server-relative but full Windows/DOS paths always, and for the pictures, either full Windows/DOS paths during development prepended with file://
You can always hide them before production.
2) Watch out for too many slashes etc. It is easy to end up with a double slash which screws up the URLs...
3) Remember that formulas are evaluated in some sort of mystical sequence by Crystal but I understand that the Page Header is processed before the detail. I put ALL my common variables i.e. DocRoot and WebSiteRootURL there in the Page Header (or even the Report Header since they do not change), so they are quantified first, and the detail band can use them.
4) I delegated all the picture showing and listing to an embedded subreport. It shares the variables it needs using a Shared StringVar xyz approach. It is all a bit squirrelly but basically the subreport always gets its value from the container report for the common stuff (see comment 3 right above). For laying out that subreport, I made an even tinier subreport to serve as the holder for the common variables so I would not have to run the whole monstrosity.
5) These 'variable holder'reports cannot be suppressed or the values do not appear to resolve. So make them extremely tiny and borderless, then squish up the page headers or whatever to hide them. I guess the text and background could be set identical as well to further cloak them but don't do that until you are done fiddling with the layout etc. or you will have to graze around to find the itty-bitty subreport. They are your local memory variables.
6) Image/document/linked files in standalone MUST use conventional DOS/Windows file name paths (no forward slashes). These can resolve for the website but to make a hyperlink, the slashes gotta go forward as far as I can tell so assume you will have to flip-flop.
7) As a side note, I was able to relocate my documents out of the website hierarchy during development by using Junction Magic to remap ~/MyDocuments to D:\MyDocuments etc.. The web server does not appear to care that it is actually traversing D:\MyDocuments but thinks that it is in a subfolder of the website. HOWEVER, you may have to use a Virtual Directory approach - be forewarned. Or, store the documents right under the website but somehow that seems klutzy.
8) Did I mention permission problems? No, but they might bite you. Make sure the IIS_IUSR and whatever can access the files/folders etc.
9) Shouldn't this have all been much easier? Oh well, thank you anyway SAP...
NEWS BLAST -
so apparently the Request object also holds the web site URL stem. Use that if you don't want to go the route of a global.
Also, I may not have explicitly stated it, but the CR OLE object only understands Windows... not unix file name conventions. As a further consideration, path lengths have to conform.
Finally, when I show file names on the web page, I cloak both the DOS/Windows root and the website stem and just show the relative path e.g. ~/Folder/Folder/File.png so that it is not too obvious how the documents are arranged on the website - probably needlessly paranoid but also has the benefit that if the website moves users don't get bewildered.
Questions may or may not get answers. Have fun.
Here you go... (tested with VS 2013) - Working !!!
1) Ad an image to the report using insert->picture
2) Right click on image -> format object ->picture
3) Change the formula of the graphic location
ex-
"E:\tmp\wrk\s1.jpg"
You can change the path and the file name according to your requirement and conditions using the formula builder
The main problem is CrystalImageHandler.aspxonly works for the root folder (I mean: for c:\inetpub\wwwroot) So there are 2 options:
From the ISS Manager change Default Web Site-> Advanced Settings->Physical Route from %SystemDrive%\inetpub\wwwrootto YourFolderName
Move your files to %SystemDrive%\inetpub\wwwroot
So far, I found this solution. Hope it helps.
I know that if you are using the Crystal Reports included with Visual Studio, this is not supported. It will display images stored in a db field, but not from a url.
I think (but do not know for sure) that no version of Crystal supports the ftp protocol for displaying images.

Reading a Bitmap from a TIF file - Works in WinForms not in ASP.NET

EDIT Solution Found: See my post below.
We are writing a library that reads in a TIF file from a scanner. Basically, its a scantron. We are examining the form and reading values from it.
Currently we have a windows test app, and we give it a filepath as a string ("c:\testing\image.tif"). It loads up the tif, reads the document correctly and parses the values.
We also have an ASP.NET web application. We have a test page that does exactly what the windows app does, we hand it an identical string, and i calls the same function on the same class from the same library. It however does NOT read the form correctly. We have verified that it does it fact load up the tif file, and it is actually filled with data (pixels we expect to be white/black are white/black when we examine the Bitmap obect in the immediate window of Visual Studio).
The specific problem is in a library called DataMatrix we use to scan a bar-code off the document. This function is supposed to return a List<string>, each of which is a barcode the library found on the document. In the windows app, this function (DataMatrixDecoder.DecodeBarcode(bitmap)) correctly returns with a Count=1. When using the asp.net app, this returns with Count=0.
Because its the exact same image file, I cannot imagine the problem is in DataMatrix. I can only assume its something with ASP.NET or something.
This isn't even my project, but another guy and I are helping our coworker figure this out, and we are just pulling our hair out. All signs indicate that ASP.NET is correctly loading and handing the image off disk to the "processor" class (which is a class library that uses the DataMatrix stuff, we are not doing ANY code in ASP.NET except for opening/handing the file to the function.).
Does anyone have any ideas as to what it might be, or different things we can check?
I'm not even sure what kind of information to give so I tried to say it all, if you have any questions please ask I'd be more than happy to elaborate on anything. Thanks.
edit:
this is the code on the ascx.cs code-behind, in a button-click event:
if (formReader.ReadTIFF(#"c:\testing\image.tif"))
{
messages.Controls.Add(HtmlHelper.DivSuccess("Read successful."));
}
The formReader class then open the file with a FileStream, and uses that to create a Bitmap. The ASP.NET application is not actually opening the file at all (we were uploading it through a FormUpload control, but after experiencing problems we dummied it down to this). This is the most perplexing thing, that it works in the windows app but not from this web site. ASP.NET has full permissions on that folder to do whatever it wants. It can open the image fine, and the bitmap it creates from the FileStream is the actual image.
edit: Also, the ReadTIFF function right now copies the FileStream into a MemoryStream, ensuring its a not a problem streaming from disk (the entire file is in memory).
How are you passing the filepath to the web application?
It is possible that the function which Decodes might be swallowing some exception.
Use reflector to examine the library (if you have not written it).
I agree. It seems your problem is most probably related to User rights on the directory where you're trying to access the files from. Try giving your Web users the Full access rights on the source directory.
EDIT
Solution Found: The problem was that the open file dialog was changing the CurrentWorkingDirectory. The reason the website never worked, was because the Environment.CurrentDirectory was set incorrectly. When I manually set the CurrentDirectory to the websites' bin folder, parsing works correctly.
Small update. Using the Windows App, and selecting the file via OpenFileDialog, will cause the barcode decoder to fail. Technically, I am using the exact same string to hand to the parser ("c:\testing\image.tif"), yet when I use the OpenFileDialog to get the string, the decoder fails. Is there a clue in this?
update: In fact, even if I don't use the string the OpenFileDialog gives me, if I just open the file dialog at all, it will fail. I don't get this. It's something simple. I need to debug the C++ DataMatrix library, really.

How to parse an .msstyles file?

I need to extract some bitmaps from an .msstyles file (the Windows XP visual style files) and I'm not sure where to start. I can't seem to find any documentation on how to do it, and the file format seems to be binary and not easily parsed. I have been able to extract the bitmap by itself using:
IntPtr p = LoadLibrary(UxTheme.ThemeName);
Bitmap bmp = Bitmap.FromResource(p, "BITMAP_NAME");
FreeLibrary(p);
However, I also need the information related to the bitmap, like the margin sizes, spacing and number of "images" per bitmap. Does anyone have any experience with this or any links to documentation that I can use?
This site claims the file format is documented though not by Microsoft.
Also found this in the Wine Crossreference.
Hope that helps!
If you want to get files out of a dll directly (remember, msstyles are dlls with another extension), you could have a look at the Anolis Project.
As for actually parsing that stuff you should look at the various tutorials on creating msstyles for information on how the various text resources in that file work.
This codeproject article seems to have exactly what you want, with a little interop involved. A managed wrapper exists and it seems rather good. The .Net WindowsForms also has the functionality built in, you might want to look at the System.Windows.Forms.VisualStyles namespace if you want simplified read only access.
You can open the msstyles using 7-zip, install it, then right click the msstyles > 7-zip, ther's 2 open inside, one as a normal button and the other with a arrow, choose the second one, then select "#"
You're now inside the msstyles, now right click to 1..mst > Open inside
You're inside the actual theme now, now just extract it's resources
Image of the msstyles open (is in spanish tho)

Categories