# How can I distort and image into a trapezium, to give it a feel of perspective?RSS

## 9 replies

Last post Sep 30, 2006 08:04 PM by Ryoushin

• woodshed

None

0 Points

3 Posts

### How can I distort and image into a trapezium, to give it a feel of perspective?

I've been searching the web for the best part of a week, posting on forums but I just get no joy. I figure its either sooooo easy nobody has ever bother to ask or its so hard nobody knows the answer.

All I want to do is to tak a photograph, and then shrink it on one edge (imaging the starwars opening credits, "in a galaxy far far away..." but with an image instead of text.

I've got routines to scale, rotate, flip, translate, even skew but this one thing I can't find. A simple routine which took 4 points and then stretched the image within that bounding shape would be great.

Imagine I have an image which is 100 pixels square. What I want to do is distort that image, so the corner points starting from the top left and going clockwise become

10,0

90,0

100,100

0, 100

It can't be that hard can it? Any help would be absolutely great!![:)]

Regards

Chris

• Ryoushin

Member

63 Points

80 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

Mmhh... you jast gave me an idea for a new feature of my GDI+ component...

I think I have a solution to accomplish that:

The idea is to scan the image line by line, coloring only a reduced set of pixels...
Let's say that the very top row of pixel has to become the 10% shorter than it was before, you should skip 1 pixel each 10 starting to paint pixels from the (.05*{line_length})TH one.
With a linear interpolation from top to bottom you'd get the amount of "shrinking" you should give to each row...

Is it clear? I will try as soon as I can...

Ryoushin - Sator Arepo Tenet Opera Rotas.
• woodshed

None

0 Points

3 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

wow - that sounds like hard work! I was hoping there was just a call i'd missed?

It so frustrating, skewing by giving 3 points is such an easy call - if it would just let specify another point I'd be there.

Let me know if you build that component - I'll be your beta tester ;) BTW I'd like to be able to make the perspective left to right as well as top to bottom - just to make things a bit harder.

ie

0,0

100,10

100,90

0,100

Cheers

Chris

Chris

• Ryoushin

Member

63 Points

80 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

Well, actually ...there's a Call!

"Graphics.DrawImage Method (Image, Point[], Rectangle, GraphicsUnit) "

the function is designed for a 3-item-long Point array but if you use Reflector to snoop into the System.Drawing.Assembly, you'll see that both 3 and 4 items array are allowed:

```public void DrawImage(Image image, PointF[] destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, Graphics.DrawImageAbort callback, int callbackData)
{
if (destPoints == null)
{
throw new ArgumentNullException("destPoints");
}
if (image == null)
{
throw new ArgumentNullException("image");
}
int num1 = destPoints.Length;
if ((num1 != 3) && (num1 != 4))
{
throw new ArgumentException(SR.GetString("GdiplusDestPointsInvalidLength"));
}
IntPtr ptr1 = SafeNativeMethods.Gdip.ConvertPointToMemory(destPoints);
int num2 = SafeNativeMethods.Gdip.GdipDrawImagePointsRect(new HandleRef(this, this.NativeGraphics), new HandleRef(image, image.nativeImage), new HandleRef(this, ptr1), destPoints.Length, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, (int) srcUnit, new HandleRef(imageAttr, (imageAttr != null) ? imageAttr.nativeImageAttributes : IntPtr.Zero), callback, new HandleRef(null, (IntPtr) callbackData));
Marshal.FreeHGlobal(ptr1);
this.IgnoreMetafileErrors(image, ref num2);
this.CheckErrorStatus(num2);
}```

The real problem is: this optional path hasn't been implemented yet!! So you'll get a System.NotImplemented Exception...

Ryoushin - Sator Arepo Tenet Opera Rotas.
• woodshed

None

0 Points

3 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

I'm beginning to think this is just another of microsofts dirty little secrets - lol.

I've posted the question in upwards of 10 forums and your's is the only reply and thats not good news.

thanks

anyway.

Chris

• Ryoushin

Member

63 Points

80 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

As promised, I'm trying to approach the problem (to solve it!)...

Here a first one (http://www.pacem.it/CMerighi/Posts/19,en-US/Distort_Images_with_GDIplus_(first_approach).aspx) which isn't likely to be the right one, but, you know, I have to test solutions...

I haven't used the solution I mentioned earlier in this post (I've been figuring out a different one)...

You'll hear others from me (daily job permitting...)

Ryoushin - Sator Arepo Tenet Opera Rotas.
• spart3b

None

0 Points

7 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

Keeping an eye on this thread as it is exactly what I am curious about as well!  Did Microsoft run into a wall on the 4th point?  Very frustrating.

• Ryoushin

Member

63 Points

80 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

Hi everybody,

I think I solved the problem:

http://www.pacem.it/CMerighi/Posts/20,en-US/Distort_Images_with_GDIplus_(solution).aspx

(...I used the idea, generalized, that I posted earlier in this thread...)

Ryoushin - Sator Arepo Tenet Opera Rotas.
• spart3b

None

0 Points

7 Posts

### Re: How can I distort and image into a trapezium, to give it a feel of perspective?

[:D]

Great work!  Is there demo code to download?  I have been working with GraphicsPath and Warp, but this solution has many more positives, would love to see some code!

• Ryoushin

Member

63 Points

80 Posts