Write multiple line string with GDI+ - c#

I have a small display on my keyboard, it's 320x240, I'm creating a plugin that allows me to see skype messages on this display even when I'm playing full screen games.
The biggest problem I have, after solving all things about interacting with skype, is that I need to show chat messages in such a small area.
I'm using a font with size 10 to avoid unreadable text, this will allow me nothing more than 7 lines and around 40 characters per line.
I need a way to write a string on multiple lines, I've imagined some ways but I don't want to reinvent the wheel, I'm sure someone found the best solution for this, otherwise I couldn't have written this question :)
So, how to write multiple line string with gdi+ on an image of size 320x240? (well, we should consider it of size 290x240 because I have a header).

I solved the problem by myself:
First, drawstring method do already "what i want" (makes the text fill a rectangle, if possible, otherwise keep writing it increasing height and not width).
With measurestring method, you can do the job.
I managed to split the string in multiple lines using some custom algorithms that worked well talking about performance. I won't enter into details because the algorithm is focused on my application and is definitely not generic.

Related

Need to visualize various sized tables and charts in printed document

I do have a C# application that calculates storage space in terms of boxes, for some products which itself consist of several parts.
The calculation includes suggestions on how many single parts to put into one box, taking in account volume and weight of the part. The overall result states the number of boxes needed for a given quantity of products.
As of now I am putting the results into a database and then copy it into an Excel file. Furthermore I am creating some charts to better visualize the numbers.
I am not satisfied with this solution. I do however have no idea, on how to programmatically produce a printing output. I am familiar with standard printing in C#, but woking with various sized tables and charts there just seems wrong and an awful lot of work to me.
I have to admit I am lacking some experience in this field, therefore I would be pleased to hear from you on this matter.
The short answer is, it is going to take a good amount of work. I think the most important step towards coming up with a solution is stepping back and realizing everything that needs to be printed and how you would handle it if you were a printer (sounds weird, I know!).
You're going to want to write your own DocumentPaginator and draw elements for each page in the overriden DocumentPaginator.GetPage method. The DocumentPaginator is what the printer uses to print documents, so it calls the GetPage method and you can send it a DocumentPage with user controls drawn on it. You also have to be able to do things like calculate how many pages to print, so keep that in mind.
Since they are different sizes you have a couple options that I can think of:
Only draw (scale) the tables/charts to be the width of the maximum size of the page being printed
Draw 1 visual per page (I would suggest keeping them all landscape or all portrait oriented to keep it simpler for now)
Basically if you're going to use the PrintDialog's dialog for the user to select the printer and printer preferences, it'll save you some work. If you don't want to show a dialog, then you will need to become fimiliar with PrintDialog, PrintQueue, XpsDocumentWriter, PrintTicket, PrinterCapabilities, PrintServer, and DocumentPaginator.
Note: If you allow a users to print 1 control across multiple pages, realize that providing "half drawn" controls and/or printing both "vertically" and "horizontally" adds another degree of complexity.

Good realtime display perfomance and input processing for .NET desktop application approach

I am tasked to write a .NET application that displays a slide-show with some information (words or images) and when the user sees certain items, s/he must immediately press space and the time of the key press must be recorded. The items will be displayed one after the other, for about 50ms. I need then to evaluate the difference between the timestamp of the keypress and and the one when the slideshow started (so I will know how long it took the user to react on the presented item). Edit: I must also record the time of the occurence of the special item.
I need to reduce any unpredictable lags that may occur as the application is running, so that the input processing is as realistic as possible, as well as to reduce any lags between the slides. Currently I am thinking of 3 approaches:
Write a standard Windows Forms (GDI+) application.
Write a WPF application
Write a DirectX-enabled windows forms application that utilizes the Tom Miller's Render loop concept (it is praised as effective in terms of performance).
Something else that you might suggest
I must clarify that I will not use advanced display techniques, special effects or designed for the purpose 3D environments - just plain text slides in different fonts and colours, or images. Unfortunately I cannot cite my sources, but I have read that Windows Forms and GDI+ cannot provide me with the desired accuracy. So, is WPF going to provide me with a better solution? Do I need to use the render loop, or some other approach. I am not experienced in such type of performance requirements for desktop applications, and all advices will be appreciated greatly.
I personally love WPF, but I would be very wary of using it for this application. It is not going to have the same time precision as GDI+ or DirectX. There are all sorts things you'll have to work with like the DispatchTimer and it just wasn't build for something like this. WPF is a whole set of gigantic abstractions on top of graphics and the farther you get away from the metal, you're introducing potential problems. If you want to put a video projected on top of a 3D sphere inside a combobox then WPF is the way to go, but if you need accuracy/precision on the scale you're talking, WPF is not the answer. I don't know where you read that WPF will provide you with better accuracy, I can practically guarantee that it will not.
DirectX would most likely be the most accurate in ensuring that a picture is only displayed for 50ms at a time. But GDI+ would be a decent alternative solution because it will make it easier to deal with text from a programming perspective.
Another consideration, screen refresh rates. yikes. if you do the math most LCDs have a 5ms response rate which is 10% of your allotted time. That and they only display at 60Hz. If you're displaying 20 pictures per second (50ms per picture) it is only going to be on the screen for 3 refresh cycles.
I hope this helps.
50 msec isn't long. Maybe encode and play the slideshow as a video?

Displaying, selecting and editing text on UserControl

I'm in the process of writing a small text-editor that is supposed to have very basic formatting capabilities, nothing fancy, yet more complex than what RichTextBox can provide (including a 'page' display functionality).
However, last time I wrote any text editors was with WinAPI in C for Windows 3.1. I have no idea on how to approach this with .NET. The furthers I've gotten until a blank is using TextRenderer.DrawText() to output the text I have into a rectangle... but something tells me that's not the way to go in this case, because I've thought all along that for one, I will want the background text in XML format... and while I can break up the paragraphs with tags, I have no clue how to do the formatting, such as or a different font family, so it probably need to be approached a little differently. Probably each word separately. Do I have to keep each word in an array (with respecting xml tags) and draw it as a separate control keeping its positions in an array as well? That sounds like an awful lot of memory and drawing power if we're talking about hundreds of thousands of words in one file (it's a possibility, although I highly doubt one file would extend beyond 20000 words, however anything's possible and together with the word's points (formatting is only available to full words, so no worry about that) it may grow pretty big with memory usage, something that I don't particulary prefer to happen.
So... what I'm looking for is a few hints and tips (I can't use Tx Text Editor, DevExpress or Telerik's RichTextEditors for a good reason plus their price). on the most correct way to build a 'word processor' that is well extendable with C#.NET as it's the one thing I have so far never had to stick my nose into.
Basically:
1) Best way to draw text into a custom control. How should I keep my text? I will probably use the base Text property for pre-formatting stage when I load the files. Or perhaps I'll extend it with my custom XML class? Should I keep words in order in position and joining them by hand when backspace is used? Etc etc.
2) Best way to do selection (probably only one way I presume?), while in WinAPI I could just detect the character under cursor as it was standard text, considering the amount of fonts and the way windows draws the characters I would either have to OCR and then start calculating position... so I presume there's a better way of selecting text?
3) Editing text - which I would assume is simple once I have a cursor position.
Cheers for reading and... hopefully someone comes up with a better solution than my feeble DrawText which... isn't really the solution.
If you're looking to create a more fully-functional text editor in WPF, you might want to start with this sample and work on from there.

Separation and analysis of an image

Here's the scenario:
I am using Visual Studio 2008 with .NET framework 3.5. I am using C#. And for database I am using MySQL. I have a picturebox on a form and 10-12 buttons (each with some image manipulation function). On clicking one of the buttons openfiledialog box is shown up where the user can select the specific file to provide to the program. On clicking another button the program should perform the actions as explained below.
I have an image of a circuit. Suppose this is the image which is provided to the program. e.g.
What I intend to do is that - the program should hypothetically label the circuit as follows:
and then it should separate the image and store the information in a database.
Is there any way to do that. Can anyway tell me the approach to do that? Any help or suggestions please.
Thanks.
In image processing, the problem of finding the 'parts' of the circuit is known as connected component labeling. If you are using C#, I believe that you can use EmguCV (a wrapper to the OpenCV library) to solve the first part of the problem. To do that, you have to consider that the white pixels are the background and that the black pixels are objects.
Now that you have the separated traces, the problem is reduced to finding and labeling the white dots. Again, you can solve it by connected component labeling, but now the objects are represented by white pixels and the background are the black pixels.
At least for your example case, a very simple algorithm would work.
Find a black pixel from the image
Using a flood-fill algorithm, find all the pixels connected to it, and separate it. That's one of your traces.
Working with the separated trace, find a white pixel and use a flood-fill algorithm to find all the pixels connected to it. If you run to the edge of the image, it's not a hole. If you don't, it might be a hole, or a loop in the trace. Use a threshold for the hole size to determine if it's a terminal hole or a loop.
Label the hole and remove it from consideration. Repeat until there are no more unprocessed white pixels.
Remove the whole trace from consideration, and jump to 1.
When there are no more black pixels in consideration in step 1, you're done.
You should probably get pretty far with a basic image editing library that has a flood-fill function, a function to separate a certain color into a new image, and a function to replace colors (the last two are trivial to implement, and there are plenty of flood-fill algorithms available online). You can use different colors to mark different things, for instance, color everything "no in consideration" red. It also makes for an interesting visualization if you look at it in real time!

Recognizing barcodes with AI

As a pet project/learning experience (no this is not homework) I'm working on software to recognize barcodes from a photograph. I'm not looking for software or a library that does it - instead I'm using this as a learning exercise that I'm blogging about and will post up on Codeplex.
I have code that successfully recognizes EAN13 barcodes (which I published on CodePlex) and UPC version A/E should follow shortly. I have two areas that I'm concerned about, though. First is in decoding barcodes that are in a picture that is bit blurry or with poor contrast, etc. Second is in simply finding the actual barcode in a larger picture (right now you have to give me a photo of just the barcode).
I have the gut feeling that some form of AI is going to help me out here. I played a bit in the past with genetic algorithms and I took a course ages ago on AI so it's not totally foreign to me, but I'm not quite sure where to start.
What type of algorithm is best suited to this type of problem? Any recommended reading or code for the AI grunt work? Yes, I want to understand what's happening, but I don't necessarily want to go down to the level of coding the sorts, etc myself.
I would suggest to search for properties that a barcode has. Some that I have in mind are:
Histogram of colors shows two distinct colors in about even distribution
Doing a hough transformation finds many parallel lines
The thickness of the lines have two distinct dimensions.
Some other?
Having this I would split the image into pieces and do a classification with these features then cobine the results to calculate a liklyhood if the piece contains an barcode or not.
For your second problem (blurry image) I would suggest to calculate the 1st order derivative of the grayvalues and then detect the edges of the lines in this space. The maximum of the derivative is lower if the image is blurred but it should be detectable to a certain blurring factor.
Does this help you?
As mp already noted you don't necessary need any real AI technique for it. Have a look at chapter 12 of Real World Haskell. It implements an almost complete barcode recognizer. Sample code is in Haskell, but there is plenty of explanation, so you can probably understand the ideas and tricks even without Haskell experience.
If you want to solve it with AI then the best bet is probably using ANNs. For the given problem I would recommend to use a quite advanced technique called HyperNEAT. See my explanation (and links) as the first answer to the SO question Neural Network Size...
I would probably use two or three different networks,
The first one to find the barcode on the bigger picture. One output neuron per pixel/set of pixels, output value is the confidence if that pixel seems to be a part of a barcode. Based on the result I would use some image transformation to convert it to a "standard" format (x*y rectangle)
If you have difficulties with finding the location of the barcode use a second one. Feed the result of the first one, and ask it to give the coordinates of two corners. However, I'm not quite sure that it will be very easy to evolve this one.
Last one would work on the standardized format, output neurons for each line (or square, if you work with a possibly 2D barcode), saying if the given area should be considered black or white.
Probably it would also help to do some pre-processing of the image, e.g. those that are described in RWH.
You don't need any specific AI or softcomputing technique. You need to apply image processing technique to improve the quality of the image or to isolate the barcode from a larger image.
You could use Matlab for prototyping and learnig more about image processing.

Categories