views:

275

answers:

3

Imagine I have a rectangle say 400px x 300px. Then let’s say I want to load an image in that. All of this is very easy using Sytem.Drawing.DrawImage.

Rectangle

But then I want to leave the left hand side as 300px but change the right hand side to 250 px. I can draw the box using 4 DrawLines but I don’t know how to squash the image into the new shape. I want the right hand side of the shape to be 250, the left size 300 and the top and bottom 400px.

Resized

I can’t use DrawImage as it expects the left and right sizes to be the same. Is there a way to manipulate the image into the new shape?

I've looked at other questions, but they only apply where the left and right hand side is equal.

Any thoughts on how to squash an image into a shape which did not have parallel sides?

(If it helps, I'm happy to sacrifice image quality to fit the right shape.)

+1  A: 

I did not find a solution to your problem, but I have some information which might help you along:

What you want is called a warp transformation.

As far as I know, the .net framework natively supports this kind of transformation only for a GraphicsPath, namely, the GraphicsPath.Warp method. Unfortunately, I don't think that this will help you, unless you are willing to redraw your image using a .net GraphicsPath object.

If you need the transformation directly in the UI layer, your UI library might help: Silverlight, for example, includes the PlaneProjection class, which can be used for such effects; in WPF, the 3D engine might be useful for this (requiring more programming effort, through).

Heinzi
+1  A: 

Disclaimer: I work for Atalasoft.

Our DotImage product has a command called QuadrilateralWarpCommand that can do this. It's in DotImage Photo.

Lou Franco
+1  A: 

What you want to do is non-trivial (but also very powerful).

@Heinzi is correct, the general class is called warp transformations. What you're trying to do is specifically a perspective transformation. At a high level, it involves running the individual coordinates through a transformation matrix to get their new positions, and then doing interpolation between pixel values based on the old and new locations.

This article talks about some transformations, one of them being a sheer, so it might be helpful overall. I'm not sure, I haven't read it closely. In general, you want to google for something approximately like "c# image transformation" or "c# image perspective transform".

Depending on what you're planning on using it for, buying a library might be the best way to go about it, although there is a lot to learn about image manipulation by doing it yourself.

Donnie