Basically, I have a bunch of images that I can combine into a single image(an atlas) or use individually. For testing/designing purposes, I would like to keep all the images separate but for release they should all be combine into the atlas(for non-embeddable compilers) or embedded.
e.g., suppose I have 100 sprites. I can create a single atlas for them, use them all individually unembedded, or embed the atlas, or embed all the image.
I'd like to start out with the individual+unembedded case for development then move to the embedded atlas case or embedded individual case(but I think this is probably not as good as the embedded atlas).
Is there an easy way to set this up in C# so the transition will be minimal?
The problem with the embedded atlas is that I would have to convert loading images(simple Image.FromFile into extracting images from the atlas).
Just curious what would be a good approach to remove the need for rewriting a lot of code in the future when the switch is made.
I could have something like Image.FromAtlas that essentially figures out the details(uses FromFile if we are in "development mode" or extracts the image from the atlas if in production... I think the embedded case would be rather trivial to implement).
What do you think? Thanks.
Related
I have been searching for help with this for a long time. If it's been answered here already, I cannot find it. I'm using C# on a Windows Form.
I'm trying to create a simple program that allows me to open a PDF, flatten any layers within it, and then, for each click of the mouse, draw a circle.
Centered within each circle I need to have a number, beginning with "1", and chronologically increasing to infinity (could be 1, could be 15000).
Finally, I need to be able to save, and print the final result.
There are other things I need to add, but if someone can get me started with this, I should be able to figure out the rest on my own.
I've been able to import the .pdf. However, any tut I've found for creating a transparent layer on which to draw, never allows me to see the pdf behind. Do I even need this transparent layer, or can I draw directly on the pdf? My second biggest issue figuring out is how to create the circle, with the chronologically increasing number anywhere I choose to click my mouse.
Thanks in advance for any help.
Please see the image below for what it should look like.
You can do that with Atalasoft's DotImage/DotPdf/DotAnnotate packages (disclaimer: I used to work on these products up until 3 years ago). There are a number of ways to do this. If you don't care the markup being an annotation, you can make an annotation with a custom appearance and add each one to the document.
If you care that the numbers get added to the document, you can use DotPdf directly to append content to the content stream of any page.
How to do this on your own. Good luck (seriously - this is not an easy problem to solve).
Here's what you need to be able to do first (at a minimum) for putting new content into an existing page:
First, let's talk about the PDF rendering model:
PDF uses a little non-Turing complete RPN language to place content on the page. A given page has one or more streams of code which gets executed in order to render a page. If you want add content on top, you need to render it last (there are other more complicated ways to do this, but this is good enough). That means you either append to the existing content stream (wouldn't recommend), or you take advantage of the fact that a PDF page can have any number of content streams on it. You make a new stream and add in the code to render the content that you want.
I'll warn you ahead of time that rendering text is non-trivial, especially if you have to embed fonts or use unicode encoding. I'll also warn you that there is no "circle" primitive in PDF. You have to approximate it with Bezier curves.
I'm studious in my laziness, so I created abstractions to make it easier to correctly create content streams. For example, I make a class called a drawing surface and I could tell it "set the drawing style to this" "place a 'shape' here" "draw text here" and so on. When it was told to render, it would generate the PDF rendering program that matched. On top of this, I had another abstraction that consisted of a drawing list of higher level objects and the drawing list, when rendered would write to the drawing surface which would in turn write PDF.
Append changes (generations) to a PDF
Create Content streams
Append a replacement page object for an existing page with the Contents changed from a Stream to an array (or if it's an array already, append to it) and with new resources added to the Resource dictionary
Here's what you need to be able to do first (at a minimum) for putting annotations on an existing page:
Append changes (generations) to a PDF
Append a replacement page object for an existing page with the Annots added with new annotations (or modified by inserting/appending new annotations)
Create appearance streams
Create annotations with using the custom appearance streams
As far as how to do the UI, that's oddly straight forward as long as you have a PDF renderer. Render a page into an image and make a control that gives you a mouse click onto the page. Then build a transformation matrix that goes from image coordinates to PDF page coordinates and push the mouse coordinates through that matrix. The result will be the origin of your mark up on the page (be aware that some pages are rotated and you will need to adjust your transformation matrix to match).
Now, to be clear, when I wrote this library for Atalasoft, I already had several years of PDF experience (I worked on Acrobat v 1 - 4). While I wasn't working on the library full time, it was written over the span of 10 years. The code to append to an existing PDF took several months of time to get right. Eventually, I shed that code because of complications in appending anything but simple changes (like annotations) and wrote code that could rewrite an entire PDF with updates to existing content (page reordering, annotations, bookmarks, new content on a page, edited images, etc), while simultaneously shedding anything that is no longer needed. This is akin to adding in and clipping out sections of a directed graph with cycles and being able to ensure that you have a correct graph on the other end.
The hard part wasn't working within the specification - that's fairly straightforward for me. The problem was dealing with cockamamie PDF generated by other tools that had all kinds of bizarro spec violations and handling that correctly.
Now, I'm not saying don't do this. I'm all for people learning new things and learning about PDF. There's a lot there to learn and a lot of interesting ideas, but you need to be aware that simple sounding problems in PDF space aren't trivial unless you have a great deal of infrastructure in place. For example, "how many pages are in a PDF?" requires a PDF scanner and code to execute or parse PDF content so you can read in the cross reference table (which may be a compressed cross reference stream), the document dictionary (which may include encryption), and finally the page tree: all which can be easily derailed by non-compliant PDF.
If you're trying to balance time and cost, remember that your time is far from free and maybe a library to do the heavy lifting is not a bad thing. iTextPdf is open source and can do all of the things you need to do, but it will cost you in time to learn the library, but that's a huge savings over having to write PDF tools on your own. Atalasoft's code is not free, but was written to have a much shallower learning curve than most libraries.
I'm trying to create a card game in C# and for this I have alot of images that I need to load. They're all jpg images and there are about 7000 of them.
I would like to make sure that if you download the game, the images will not be easily accessible, meaning that they should not just be JPG images in a sub folder of the application. So I thought about imbedding them in a DLL file.
But how do I do this? And how do I handle this efficiently? Is there a tecnique to this sort of thing, or is another method preferable?
I would like to make sure that [...] the images will not be easily accessible
First, you should ask yourself why you want to forbid this. If you just want to avoid that someone else manipulates the pictures, you can leave them in a bunch of subfolders as JPGs, just generate checksums for each file and check them at the time the program loads the pictures.
If you want to avoid reuse of the pictures, you can leave them in a bunch of subfolders, but not as JPGs. Encode them with for example with the standard AES algorithm. But beware, that won't prevent anyone else of making screenshots while you application is running, so you should consider if that's really worth the effort.
EDIT: if you want to embed the images because installation gets easier when you have just one big file to deploy instead of 7000 single files, then you may write a helper program for creating resource files programmatically. See this page from Microsoft, especially the part about .resource files, to learn how to utilize the ResourceWriter class for that purpose.
If you have 7000 image, you need a database. Microsoft SQL Server Compact 4.0 is an option. It's small and easy to use.
I'm assuming that this is a windows application
In order to Embed a Image to the assembly
1. Right click the Image file and Select properties
2. In the Properties Pane Set the BuildAction as Embeded resource
So this Image becomes a embeded resource when the application is compiled
Then you can access the Image from the assembly like:
global::[[MyNameSpace]].Properties.Resources.[[ImageName]]
for eg:this.pictureBox1.Image = global::[[MyNameSpace]].Properties.Resources.[[ImageName]]
Hi and thanks for looking!
Update
For the sake of clarity, a third-party .NET library is just fine. Preferably an open-source or free one. The solution need not be native .NET.
Background
I am working on an enterprise web application for which the client has given us thousands of pages of content in MS Word documents that we have to parse, extract data, and send to the content database.
Within these docs are various embedded images representing a larger original image in a separate folder.
The client did not provide any paths to the original source image, so when we see content with an embedded image in the MS Word doc, we have to go through several "assets" folders and look for the corresponding image which is extraordinarily time consuming.
We are already using DocX to parse the documents, so you can assume that we have a list of bitmap images to loop through that we have pulled from the document.
Question
Given a list of bitmaps that we just extracted from the document, how do we search a different folder containing hundreds of images, for the matching image, and then return the file path to it?
TinEye.com does this over the web. I am wondering if, using System.Drawing or something, we can do it on a PC with C#.
Thanks!
Matt
Hate to propose an answer to my own question, but I think I might be on to something here. Here is heuristic/pseudo code for a C# forms app--your thoughts are appreciated:
Part 1
Using System.IO, traverse the "assets" folders and get all images.
For each image, Base64 encode it.
Take the resulting string and place in an XML file:
<Image>
<Path>C:\SomePath</Path>
<EncodedString>[Some Base64 String]<Encoded String>
</Image>
Now we have an XML file containing all original images, in Base64 form, along with their file path.
Part 2
Using DocX, extract all images from MS Word Doc.
For each image, use Linq-to-Xml to search for an exact match in the XML file from Part 1.
If there are no exact matches, start iterating the XML file and computing the Levenshtein distance.
While in the foreach store the XML node Id (or file path) and Levenshtein Distance as a key value pair in an object.
Take the k/v pair with the lowest LD score and return the file path.
For performance, set tolerance so that the foreach stops if a certain original image has an acceptably low LD score when compared to the image extracted from the document.
Since this is a one-off task, I don't need instant performance. So, I could run this tonight before leaving the office and, hopefully, come back tomorrow to a list of paths connecting the original images to the ones embedded in the docs.
UPDATE
The heuristic above worked beautifully! I ended up using the Sift library to efficiently calculate distances between Base64 strings. Specifically, I used their FastDistance() method. Having 100% accuracy on finding the images I need, even if the angle from which the photo was taken is slightly different.
There is no built-in algorithm in the .NET framework for generating image similarity. You'd need to use a third-party library or do it yourself. Lots of image similarity algo questions on SO:
Algorithm for finding similar images
How can I measure the similarity between two images?
comparing images programmatically - lib or class
One more, for .NET: Are there any OK image recognition libraries for .NET?. This one refers you to AForge, which seems to have the algorithm that you are after.
According to this SO answer to a similar question, you should look at OpenCV and VLFeat. The former has a C++ API and the latter a C API, so you would need to write your own P/Invoke wrapper or perhaps wrap them in a C++/CLI facade, which you could call from C#.
I'm looking to build a website that has a flash interface and allows visitors to upload vector art in a number of file formats such as SVG, EPS and AI.
I have two rather large problems...
1) I need to load the original vector art, probably convert it to FLV and display it in my flash application.
2) After the user potentially loads a number of images, adds some text, rotates or transforms some elements, I need to save the resulting composition into a vector art format that I can print.
I'm not much of an AS developer...my experience is mostly in .NET/C# & C++.
I'm looking for a good library or API that provides the functionality I will need to convert different image formats and save the results.
How much of this can be done using Flash / AS...? How much in C#...?
I've used the libspark svgParser before to generate mxml from an svg and the similar fxgParser to create animation from a vector file.
The svgParser should come handy, but also have a look at their svgEditor.
HTH
In my application the user can create multiple objects (so called drawings) each of which has a SurfaceInkCanvas, very similar with the Photopad (the Photo Paint app)in the SDKSamples(provided by MS Surface SP1 SDK).
What would be the best way to save the content of the inkCanvas(the drawing object) given the fact that there may be tens or even hundreds of them created.
After some research, one option popped-out: converting it to an image, jpg or png, but this method doesn't seem that scalable IMO.
Would it be possible to store the points in a database(preferably sqlite)?
The goal is to be able to restore the drawings(contents of the inkCanvas) for further editing upon loading.
Saving to an image isn't really viable for what it sounds like you want to do because restoring vector lines from a bitmap is much harder and potentially lossy than the vector to bitmap conversion, in addition to not being very efficient from a data density perspective.
You could write some code to save and restore the collection of lines that's stored in the Strokes property. This would allow you to reduce the size of your data down to a minimum and easily store in a database. Each Stroke has some data about what the line looks like and a set of points that make up the line. It's fairly simple to extract and restore that data to another InkCanvas when reloading.
You might also be able to use XamlWriter/XamlReader to just serialize the entire InkCanvas to xml but you would need to be able to put the new deserialized instance into your UI and make sure there aren't any naming or resource complications in your XAML that tend to cause errors with this method.
there's already a format that microsoft uses to save the strokes in, which is ISF - standing for Ink Serialized format that saves the Strokes collection in an .isf file.
However in this example each drawing is saved in a separate (but small-size) .isf file that can be easily loaded back for further editing or even converting (exporting) it to bitmap.
So a good method would be to save each drawing in files(even though this is a bit more expensive, time-wise, than simply storing it in a sqlite db)and save the path of each along with an id in the database to identify each session.
Any suggestion for further optimising this method from a scalability point of view?