最近的项目中,由于要实现一个个性T恤或者瓷杯的DIY界面,允许用户自己上传图片,文字,叠放图片文字,等任意操作(当然在局部范围内),并保存为一个图片。目前silverlight中还没有图片合成的功能,因此可以将图片合成的一些参数传递到webclient或者wcf中进行合成,然后在传递回来。当然这样的效率不会很高,尤其是图片很多的时候。。
前台界面图,中间部分是合成时添加的自己的图片
下面是图片合成时的一些代码:
using System; using System.Web; using System.Drawing; using System.IO; using System.Web.Services; using System.Drawing.Drawing2D; using System.Drawing.Imaging;
namespace ImageSnapperWeb { public class PhotoHandler : IHttpHandler { private int intWdith; private int intHeight; private int intLeft; private int intTop; private int intOpacity; public void ProcessRequest(HttpContext context) { String strPath1 = context.Request.QueryString["P1"]; String strPath2 = context.Request.QueryString["P2"]; String strPoing = context.Request.QueryString["P3"]; String strNFN = System.DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "").Replace("/", ""); strNFN = strNFN + new Random().Next(100).ToString() + ".jpg"; String fileName = HttpContext.Current.Server.MapPath(@"ClientBin/" + strPath1); String waterfileName = strPath2; Image img = System.Drawing.Image.FromFile(fileName); string[] arr = strPoing.Split(','); double fl = img.Height / 380.00; if (arr.Length == 5) { intWdith = int.Parse((double.Parse(arr[0]) * fl).ToString("f0")); intHeight = int.Parse((double.Parse(arr[1]) * fl).ToString("f0")); intLeft = int.Parse((double.Parse(arr[2]) * fl - 20).ToString("f0")); intTop = int.Parse((double.Parse(arr[3]) * fl + 150.00).ToString("f0")); intOpacity = int.Parse(arr[4]); }
//合成图片 System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(@"ClientBin/" + strPath1)); System.Drawing.Image copyImage; copyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(@"ClientBin/" + strPath2));
//HttpContext.Current.Response.Write(copyImage+"<br>"); Graphics g = Graphics.FromImage(image); //控制图片显示的大小和位置 ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float transparency = 0.5F; if (intOpacity >= 1 && intOpacity <= 10) { transparency = (intOpacity / 10.0F); }
float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} };
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(copyImage, new Rectangle(intLeft, intTop, intHeight, intWdith), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes); ////加文字 //Font f = new Font("Verdana", 32); //Brush b = new SolidBrush(Color.Tomato); //string addText = "这是SilverLight图片合成测试!"; //g.DrawString(addText, f, b, 10, 10); g.Dispose();
//保存图片 image.Save(HttpContext.Current.Server.MapPath(@"ClientBin/ImgOK/" + strNFN)); image.Dispose(); copyImage.Dispose();
//Screen scr = Screen.PrimaryScreen; //Rectangle rc = scr.Bounds; //int iWidth = 400; //int iHeight = 400; ////创建一个和屏幕一样大的Bitmap //Image myImage = new Bitmap(iWidth, iHeight); ////从一个继承自Image类的对象中创建Graphics对象 //Graphics g = Graphics.FromImage(myImage); ////抓屏并拷贝到myimage里 //g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight)); ////保存为文件 //myImage.Save(HttpContext.Current.Server.MapPath(@"ClientBin/ImgOK/" + strNFN));
context.Response.ContentType = "text/plain"; context.Response.Write(strNFN); }
public bool IsReusable { get { return false; } }
} }