C#实现图片扭曲为不规则图形
最近需要把矩形图片改为梯形,首先想到用GDI+来实现,结果不语,GDI+没有提供直接扭曲不规则图片的方法。然后就是无限Google,结果算法倒是不少,都没有现成的。有通过WPF的三维映射来实现的,但是满足不了我。苦逼了好多天,终于实现,直接上图(算法是Google大神的)。
string url="保存图片的地址";
Bitmap bmp = new Bitmap(Server.MapPath(url));
ImageTransform transform = new ImageTransform();
transform.Bitmap = bmp;
CustomPoint[] points = new CustomPoint[4];
points[0] = new CustomPoint(0, 100);//左上
points[1] = new CustomPoint(bmp.Size.Width, 80);//右上
points[2] = new CustomPoint(bmp.Size.Width, bmp.Size.Height);//右下
points[3] = new CustomPoint(0, bmp.Size.Height + 100);//左下
transform.Points = points;//传入新的4个点
transform.Save(Server.MapPath("~/新地址/")+"文件名");
this.Old.ImageUrl = url;
this.New.ImageUrl = "/新地址/"+"文件名";
效果如图:

我整理成了一个DLL:

如果有需要帮助的可以联系我,可以提供算法和源码。
浙公网安备 33010602011771号