Resumé

Distort Images with GDIplus (first approach)

Cristian Merighi () 0.00

Loud thoughts an prototype implementations to obtain a free distortion figure (not only a parallelogrammic one), manipulating an image with C# and GDI+...
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

Framework .Net is without a method, in its GDI+ APIs implementation, that permits to distort an image just by giving four new vertices.

The DrawImage method, member of System.Drawing.Graphics class, actually, allow this option (as you can see using Reflector) ...the bad news is that it's not implemented yet!

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 first approach to the problem is based on exploiting as much as I can what the .Net Framework can already offer. I tried to "mesh" the image, by subdividing it in multiple triangles.

At the actual point, this subdivision process (schemed below) isn't iterative and the masked slices don't fit perfectly. Hey, it's a prototype I developed in a couple of hours!...

scheme

I'll omit the details about geometric/trigonometric formulas and I propose an interactive demo...
(I have in mind an alternative solution, whose performance's aspects are still to be tested)

test img
top-left corner: ,
top-right corner: ,
bottom-left corner: ,
bottom-right corner: ,
 
 
warning! The method used in this article has to be considerated technically outdated.
For a definitive solution of the problem please refer to this article.

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds