NickThissen
Feb 3rd, 2010, 10:46 AM
Hey,
I have been playing with the idea to create some 'tool' that can convert an image to .NET GDI+ drawing code automatically. I got the idea a long time ago when I first started playing with custom ToolStrip renderers. I found there was a lot of repetitive work like finding the correct colors and writing the GDI+ code to replicate an existing image as close as possible, so I thought perhaps it is possible to automate this.
In the most primitive form, I could scan the image pixel per pixel and simply generate GDI+ code that draws a pixel of that color on the correct position. Obviously, this will work and it will create a 100% matching image. The downside is obvious: if the image has (for example) a large rectangular area of one color in it, it will be MUCH faster to draw that rectangle using DrawRectangle, instead of drawing it pixel per pixel. In fact I'm pretty sure drawing an image pixel per pixel would be impractical at best, and probably impossible in real-time.
Obviously, if a large area (line, rectangle, possibly ellipses?) consisting of a single color* exists, then that area should be drawn using DrawLine / DrawRectangle, etc, instead of drawing it pixel-by-pixel. The amount of pixel-by-pixel drawing should be at an absolute minimum to make the drawing code as fast as possible.
* If we don't need a 100% match, we could also use a small range of colors, so that colors that only differ by 1 R/G or B value (for example) are considered equal.
So what I'm basically looking for is some kind of 'algorithm' that can scan an image and 'detect' areas of the same color. These areas should at least contain lines and rectangles, but if possible also ellipses or triangles (which I'm sure will be MUCH harder to do).
As a simple example, consider the following image:
http://i47.tinypic.com/ak8z8j.jpg
After the algorithm, my application should 'know' that it consists of these rectangles (where the gradient should be translated to a LinearGradientBrush if possible, but it could also be a large number of DrawLines calls that draw vertical lines in decreasing color):
http://i45.tinypic.com/2d0yrs5.jpg
I am certain the task will be very hard, but at this time I am just looking if an algorithm like this already exists. I can't figure out how I would do it, it seems really complicated, but I'm sure stuff like this is useful in other applications as well so I can imagine that something similar already exists.
So... Any thoughts? Links? Thanks!
I have been playing with the idea to create some 'tool' that can convert an image to .NET GDI+ drawing code automatically. I got the idea a long time ago when I first started playing with custom ToolStrip renderers. I found there was a lot of repetitive work like finding the correct colors and writing the GDI+ code to replicate an existing image as close as possible, so I thought perhaps it is possible to automate this.
In the most primitive form, I could scan the image pixel per pixel and simply generate GDI+ code that draws a pixel of that color on the correct position. Obviously, this will work and it will create a 100% matching image. The downside is obvious: if the image has (for example) a large rectangular area of one color in it, it will be MUCH faster to draw that rectangle using DrawRectangle, instead of drawing it pixel per pixel. In fact I'm pretty sure drawing an image pixel per pixel would be impractical at best, and probably impossible in real-time.
Obviously, if a large area (line, rectangle, possibly ellipses?) consisting of a single color* exists, then that area should be drawn using DrawLine / DrawRectangle, etc, instead of drawing it pixel-by-pixel. The amount of pixel-by-pixel drawing should be at an absolute minimum to make the drawing code as fast as possible.
* If we don't need a 100% match, we could also use a small range of colors, so that colors that only differ by 1 R/G or B value (for example) are considered equal.
So what I'm basically looking for is some kind of 'algorithm' that can scan an image and 'detect' areas of the same color. These areas should at least contain lines and rectangles, but if possible also ellipses or triangles (which I'm sure will be MUCH harder to do).
As a simple example, consider the following image:
http://i47.tinypic.com/ak8z8j.jpg
After the algorithm, my application should 'know' that it consists of these rectangles (where the gradient should be translated to a LinearGradientBrush if possible, but it could also be a large number of DrawLines calls that draw vertical lines in decreasing color):
http://i45.tinypic.com/2d0yrs5.jpg
I am certain the task will be very hard, but at this time I am just looking if an algorithm like this already exists. I can't figure out how I would do it, it seems really complicated, but I'm sure stuff like this is useful in other applications as well so I can imagine that something similar already exists.
So... Any thoughts? Links? Thanks!