ASP.NET MVC 网站开发总结(七)——C#操作图片:多张图的拼接(旋转)
其实用C#来操作图片的拼接就是在用Graphic画图。个人感觉还是挺有趣的,各种类库提供了丰富多彩的功能。
源代码(移植到一个简单的C#程序中,并没有放在ASP.NET项目中):
using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace Test { class Program { static void Main(string[] args) { Image img = new Bitmap(new Bitmap("model1.png"), 800, 450);//加载图片 Graphics g = Graphics.FromImage(img);//将图片转化Graphics g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//加上抗锯齿效果 // Point[] destPoints1 = { new Point(453, 87),//图片左上点 new Point(638, 19),//图片右上点 new Point(497, 212),//图片左下点 }; g.DrawImage(new Bitmap("img1.jpg"), destPoints1);//按指定的顶点画图 g.DrawLines(new Pen(Brushes.Gray), new Point[] { new Point(453, 87), new Point(638, 19), new Point(683, 144), new Point(497, 212), new Point(453, 87), });//画线,描绘img1图片的边界 Point[] destPoints2 = { new Point(400, 158), new Point(590, 203), new Point(370, 287) }; g.DrawImage(new Bitmap("img1.jpg"), destPoints2); g.DrawLines(new Pen(Brushes.Gray), new Point[] { new Point(400, 158), new Point(590, 203), new Point(559, 332), new Point(370, 287), new Point(400, 158), }); g.FillRectangle(Brushes.Gray, 525, 279, 201, 135);//画矩形 g.DrawImage(new Bitmap("img1.jpg"), 526, 280, 199, 133);//直接按指定长宽画图 img.Save("test1.png");//保存为图片 img.Dispose();//显示释放资源 g.Dispose(); } } }
效果图:
此次知识分享就到这,敬请期待下一次的分享。^_^
<我的博客主页>:http://www.cnblogs.com/forcheng/
<Wing工作室主页>:http://www.wingstudio.org/