C#图片上画图+写字(菜鸟勿喷)

  public void InitPage(string url, UserInfoBusinessCardView model) {
            string path = Path.GetDirectoryName(url);
            string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
            using (Image bigImage = Image.FromFile(url)) {
                using (Image smallImg = Image.FromFile(model.HeadPhotos)) {
                    using (Graphics g = Graphics.FromImage(bigImage)) {
                        int x = bigImage.Width - smallImg.Width;
                        int y = bigImage.Height - smallImg.Height;
                        //添加头像
                        g.DrawImage(smallImg, 0, 0, smallImg.Width, smallImg.Height);
                        SolidBrush drawBush = new SolidBrush(Color.Red);
                        Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
                        string newPath = path + "\\" + filename + ".png";
                        //写汉字
                        g.DrawString(model.Name, drawFont, drawBush, 10, 10);
                        g.DrawString(model.Title1String + model.Title2String, drawFont, drawBush, 50, 50);
                        bigImage.Save(newPath, System.Drawing.Imaging.ImageFormat.Png);
                        if (System.IO.File.Exists(newPath)) {
                            //return newPath;
                        }
                        else {
                            //  return null;
                        }
                    }
                }
            }
        }

  //调用的地方

 public ActionResult PPP() {
            string url = Request.MapPath("~/Images/4560.jpg");
            string heda = Request.MapPath("~/Images/745.jpg");
            UserInfoBusinessCardView model = new UserInfoBusinessCardView();
            model.Name = "刘建伟";
            model.Title1String = "XXXXXXXXX牛逼公司";
            model.Title2String = "码农";
            model.HeadPhotos = heda;
            InitPage(url, model);
            return View();
        }

 ///

  public void InitPage(string bigImgUrl, string smallImgUrl, string name = null, string address = null, string firstInfo = null, string secondInfo = null, int samllImgX = 137, int samllImgY = 17, int nameX = 14, int nameY = 15, int addressX = 14, int addressY = 33, int firstInfoX = 14, int firstInfoY = 80, int secondInfoX = 14, int secondInfoY = 62, int emSize = 4, string familyName = "Arial") {
            string path = Path.GetDirectoryName(bigImgUrl);
            string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
            using (Image bigImage = Image.FromFile(bigImgUrl)) {
                using (Image smallImg = Image.FromFile(smallImgUrl)) {
                    using (Graphics g = Graphics.FromImage(bigImage)) {
                        int x = bigImage.Width - smallImg.Width;
                        int y = bigImage.Height - smallImg.Height;
                        //添加头像
                        g.DrawImage(smallImg, samllImgX, samllImgY, smallImg.Width, smallImg.Height);
                        SolidBrush drawBush = new SolidBrush(Color.Gray);
                        SolidBrush drawNameBush = new SolidBrush(Color.Black);//名字刷子
                        Font drawFont = new Font(familyName, emSize, FontStyle.Regular, GraphicsUnit.Millimeter);
                        Font drawNameFont = new Font(familyName, emSize, FontStyle.Bold, GraphicsUnit.Millimeter);//名字的字体
                        string newPath = path + "\\" + filename + ".png";
                        //名字 
                        if (name != null) {
                            g.DrawString(name, drawNameFont, drawNameBush, nameX, nameY);
                        }   //地区
                        if (address!=null) {
                            g.DrawString(address, drawFont, drawBush, addressX, addressY);
                        }    //信息
                        if (firstInfo != null) {
                            g.DrawString(firstInfo, drawFont, drawBush, firstInfoX, firstInfoY);
                        }
                        if (secondInfo!=null) {
                            g.DrawString(secondInfo, drawFont, drawBush, secondInfoX, secondInfoY);
                        }
                        bigImage.Save(newPath, System.Drawing.Imaging.ImageFormat.Png);
                        #region MyRegion
                        if (System.IO.File.Exists(newPath)) {
                            //return newPath;
                        }
                        else {
                            //  return null;
                        } 
                        #endregion
                    }
                }
            }
        }

  

posted @ 2014-10-20 15:58  ICupid  阅读(2977)  评论(0)    收藏  举报