GDI +实现的上传图片画图功能

实现的一个把上传的图片化成名片一部分的功能 

public void DrawCard(string path, string path2, string path3)
    {
        Graphics graphics;

        Bitmap bmp = new Bitmap(301, 171);//名片
        graphics = Graphics.FromImage(bmp);
        graphics.Clear(Color.White);//用白色填充名片
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//定义画笔质量
        //画外框
        Pen pen = new Pen(new SolidBrush(Color.FromArgb(255, 67, 67, 144)));//紫色画笔
        Point p1 = new Point(0, 0);
        Point p2 = new Point(300, 0);
        Point p3 = new Point(0, 170);
        Point p4 = new Point(300, 170);
        graphics.DrawLines(pen, new Point[] { p1, p2 });
        graphics.DrawLines(pen, new Point[] { p2, p4 });
        graphics.DrawLines(pen, new Point[] { p1, p3 });
        graphics.DrawLines(pen, new Point[] { p3, p4 });
        //画LOGO
        if (card.CompanyLogo.Trim() != "")
        {
            string logopath = path2 + card.CompanyLogo;
            Image newlogo = Image.FromFile(logopath);
            Bitmap MyBitmapSource = (Bitmap)(newlogo);
            int w1 = MyBitmapSource.Width;
            int h1 = MyBitmapSource.Height;
            for (int i = 0; i < w1; i++)
            {
                for (int j = 0; j < h1; j++)
                {
                    Color MyColor = MyBitmapSource.GetPixel(i, j);
                    //SetPixel(i - X, j - Y, MyColor);
                    bmp.SetPixel(((110 - w1) / 2) + i, ((70 - h1) / 2) + j, MyColor);//居中画图片
                }
            }

        }
        else
        {
            string logopath = path2 + "CLogo.png";
            Image newlogo = Image.FromFile(logopath);
            Bitmap MyBitmapSource = (Bitmap)(newlogo);
            int w1 = MyBitmapSource.Width;
            int h1 = MyBitmapSource.Height;
            for (int i = 0; i < w1; i++)
            {
                for (int j = 0; j < h1; j++)
                {
                    Color MyColor = MyBitmapSource.GetPixel(i, j);
                    //SetPixel(i - X, j - Y, MyColor);
                    bmp.SetPixel(((110 - w1) / 2) + i, ((70 - h1) / 2) + j, MyColor);//居中画图片
                }
            }
        }
        //画Photo
        if (card.Photo.Trim() != "")
        {
            string logopath = path2 + card.Photo;
            Image newlogo = Image.FromFile(logopath);
            Bitmap MyBitmapSource = (Bitmap)(newlogo);
            int w1 = MyBitmapSource.Width;
            int h1 = MyBitmapSource.Height;
            for (int i = 0; i < w1; i++)
            {
                for (int j = 0; j < h1; j++)
                {
                    Color MyColor = MyBitmapSource.GetPixel(i, j);
                    //SetPixel(i - X, j - Y, MyColor);
                    bmp.SetPixel(((110 - w1) / 2) + i, ((100 - h1) / 2) + 70 + j, MyColor);
                }
            }

        }
        else
        {
            string logopath = path2 + "RLogo.png";
            Image newlogo = Image.FromFile(logopath);
            Bitmap MyBitmapSource = (Bitmap)(newlogo);
            int w1 = MyBitmapSource.Width;
            int h1 = MyBitmapSource.Height;
            for (int i = 0; i < w1; i++)
            {
                for (int j = 0; j < h1; j++)
                {
                    Color MyColor = MyBitmapSource.GetPixel(i, j);
                    //SetPixel(i - X, j - Y, MyColor);
                    bmp.SetPixel(((110 - w1) / 2) + i, ((100 - h1) / 2) + 70 + j, MyColor);
                }
            }
        }
        //画公司信息
        int height = 12;
        Font afont = new Font("Arial", 9, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
        //Font afont = new Font("Calibri", 10, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, 128, true);
        Font afont2 = new Font("Arial", 12, FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
        Font afont3 = new Font("Arial", 9, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Near;
        sf.LineAlignment = StringAlignment.Center;
        sf.Trimming = StringFormat.GenericTypographic.Trimming;
        if (card.CompanyName.Trim() != "")
        {


            graphics.DrawString(card.CompanyName, afont, new SolidBrush(Color.Black), 110, height, sf);
            height += 12;
        }
        if (card.street.Trim() != "")
        {
            if (card.street.Trim().Length > 40)
            {

                string[] strs = (card.street).Split(' ');
                string s1 = "";
                string s2 = "";
                string s = strs[0];
                s1 = s;
                if (strs.Length > 1)
                {
                    for (int i = 1; i < strs.Length; i++)
                    {
                        s = s + " " + strs[i];
                        if (s.Length <= 40)
                        {
                            s1 = s;
                        }
                        else
                        {
                            s2 += ((s2 == "" ? "" : " ") + strs[i]);
                        }
                    }
                }
                else
                {
                    s1 = card.street.Substring(0, 40);
                    s2 = card.street.Substring(40, card.street.Length - 40);
                }

                //Font afont = new Font("Calibri", 10, System.Drawing.GraphicsUnit.Pixel);
                graphics.DrawString(s1, afont, new SolidBrush(Color.Black), 110, height, sf);
                //graphics.DrawString(s1, afont, new SolidBrush(Color.Black), 110, height, StringFormat.GenericTypographic);
                height += 12;

                //Font afont = new Font("Calibri", 10, System.Drawing.GraphicsUnit.Pixel);
                graphics.DrawString(s2, afont, new SolidBrush(Color.Black), 110, height, sf);
                height += 12;

                string s3 = (card.City == "" ? "" : (card.City + " ")) + (card.State == "" ? "" : (card.State + " ")) + card.Country + " " + card.Postalcode;
                graphics.DrawString(s3, afont, new SolidBrush(Color.Black), 110, height, sf);
                height += 12;
            }
            else
            {
                graphics.DrawString(card.street, afont, new SolidBrush(Color.Black), 110, height, sf);
                height += 12;

                string s4 = (card.City == "" ? "" : card.City + " ") + card.State;
                if (s4.Trim() != "")
                {
                    graphics.DrawString(s4, afont, new SolidBrush(Color.Black), 110, height, sf);
                    height += 12;
                }

                string s5 = card.Country + " " + card.Postalcode;
                graphics.DrawString(s5, afont, new SolidBrush(Color.Black), 110, height, sf);
                height += 12;


            }

            graphics.DrawString(card.WebSite, afont, new SolidBrush(Color.Black), 110, height, sf);
            height += 12;

        }
        else
        {
            string s4 = (card.City == "" ? "" : card.City + " ") + card.State;
            if (s4.Trim() != "")
            {
                graphics.DrawString(s4, afont, new SolidBrush(Color.Black), 110, height, sf);
                height += 12;
            }

            string s5 = card.Country + " " + card.Postalcode;
            graphics.DrawString(s5, afont, new SolidBrush(Color.Black), 110, height, sf);
            height += 12;

            graphics.DrawString(card.WebSite, afont, new SolidBrush(Color.Black), 110, height, sf);
            height += 12;

        }
        //画工作信息
        int height2 = 73;
        graphics.DrawString(card.Name, afont2, new SolidBrush(Color.Black), 110, height2, sf);
        height2 += 12;
        if (card.JobTitle.Trim() != "")
        {
            graphics.DrawString(card.JobTitle, afont, new SolidBrush(Color.Black), 110, height2, sf);
            height2 += 12;
        }
        if (card.Department.Trim() != "")
        {
            graphics.DrawString(card.Department, afont, new SolidBrush(Color.Black), 110, height2, sf);
            height2 += 12;
        }
        //画联系信息
        int height3 = 110;
        if (card.Mobile.Replace("-", "").Trim() != "")
        {
            string[] mobilestrs = card.Mobile.Split('-');
            string mobilestr = ((mobilestrs[0].Trim() == "") ? "" : ("(" + mobilestrs[0].Trim() + ") ")) + ((mobilestrs[1].Trim() == "") ? "" : "(" + mobilestrs[1] + ") ") + mobilestrs[2].Trim();
            graphics.DrawString("Mobile :", afont, new SolidBrush(Color.Black), 110, height3, sf);
            graphics.DrawString(mobilestr, afont3, new SolidBrush(Color.Black), 145, height3, sf);
            height3 += 12;
        }

        if (card.Phone.Replace("-", "").Trim() != "")
        {
            string[] phonestrs = card.Phone.Split('-');
            string phonestr = ((phonestrs[0].Trim() == "") ? "" : ("(" + phonestrs[0].Trim() + ") ")) + ((phonestrs[1].Trim() == "") ? "" : "(" + phonestrs[1].Trim() + ") ") + phonestrs[2].Trim() + (phonestrs[3].Trim() == "" ? "" : "-" + (phonestrs[3].Trim()));
            graphics.DrawString("Phone :", afont, new SolidBrush(Color.Black), 110, height3, sf);
            graphics.DrawString(phonestr, afont3, new SolidBrush(Color.Black), 145, height3, sf);
            height3 += 12;
        }

        if (card.Fax.Replace("-", "").Trim() != "")
        {
            string[] faxstrs = card.Fax.Split('-');
            string faxstr = ((faxstrs[0].Trim() == "") ? "" : ("(" + faxstrs[0].Trim() + ") ")) + ((faxstrs[1].Trim() == "") ? "" : "(" + faxstrs[1].Trim() + ") ") + faxstrs[2].Trim();
            graphics.DrawString("Fax :", afont, new SolidBrush(Color.Black), 110, height3, sf);
            graphics.DrawString(faxstr, afont3, new SolidBrush(Color.Black), 145, height3, sf);
            height3 += 12;
        }
        if (card.cardEmail.Trim() != "")
        {

            graphics.DrawString("Email :", afont, new SolidBrush(Color.Black), 110, height3, sf);
            graphics.DrawString(card.cardEmail, afont, new SolidBrush(Color.Black), 145, height3, sf);
            height3 += 12;
        }

        if (card.workim.Trim() != "")
        {
            //string workimstr = "Im :     " + card.workim;
            graphics.DrawString("Im :", afont, new SolidBrush(Color.Black), 110, height3, sf);
            graphics.DrawString(card.workim, afont, new SolidBrush(Color.Black), 145, height3, sf);
            height3 += 12;
        }


       // Image img2 = bmp.GetThumbnailImage(90, 50, new Image.GetThumbnailImageAbort(Abort), IntPtr.Zero);

        //;
       // img2.Save(path3);
        File.Delete(path);
        bmp.Save(path);

 


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值