清风的blog 优然探索

合成两张图片,并在上打字

/// <summary>
        /// 合成两张图片
        /// </summary>
        /// <param name="sourcefile">原始图案背景图</param>
        /// <param name="composefile">要合成的图案 </param>
        /// <param name="newfile">新文件名</param>
        public void view_picture(string sourcefile, string composefile, string newfile,string[] sInfo)
        {
            System.Drawing.Image imgClothing = null;
            imgClothing = System.Drawing.Image.FromFile(sourcefile);//原始图案  
            Graphics gPrimalClothing = Graphics.FromImage(imgClothing);


            System.Drawing.Image imgPic = null;
            imgPic = System.Drawing.Image.FromFile(composefile);//要合成的图案  

            //int xPosOfPic = (281 - 100 + 10) / 2;
            //int yPosOfPic = (238 - 90 - 20) / 2;

            int xPosOfPic = 23;
            int yPosOfPic = 47;
            gPrimalClothing.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            gPrimalClothing.DrawImage(
            imgPic.GetThumbnailImage(85, 85, null, IntPtr.Zero),
            new Rectangle(xPosOfPic, yPosOfPic, 85, 85),
            0, 0, 85, 85, GraphicsUnit.Pixel);//合成图案  

            #region 向上面打字

            Bitmap bmp = new Bitmap(imgClothing);
            //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
            //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //下面这个也设成高质量
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //下面这个设成High
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            SolidBrush drawBrush = new SolidBrush(ColorTranslator.FromHtml("#514E00"));
            Font drawFont = new Font("Arial", 3, FontStyle.Regular, GraphicsUnit.Millimeter);
            //Font drawFont = new Font("宋体", 4, FontStyle.Regular, GraphicsUnit.Millimeter);


            int xPos = 122;//文字X轴坐标
            int yPos = 48;  //文字Y轴坐标

            //设置好x,y坐标,可以写多个

            for (int i = 0; i < 4; i++)
            {
                gr.DrawString(sInfo[i], drawFont, drawBrush, xPos, yPos + (i*22));
            }
            //保存图像,大功告成!
            bmp.Save(newfile);
            #endregion

            Response.ContentType = "image/jpeg";
            Response.Charset = "gb-2312";
            //直接在页面输出
            //imgClothing.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //imgClothing.Save(newfile);
            imgClothing.Dispose();
            gPrimalClothing.Dispose();
        }

2011年1月23日 | 发布:admin | 分类:技术文章 | 评论:0

发表留言: