C# CGI+

这个事生成一个图片的例子。

    private void ShowReportImgs(int types, string pathId, int reportTypes)
    {
        #region 获取数据
        Hashtable detailFirst = new Hashtable();// getPathDetail(pathId, "1001");//明细信息
        Hashtable detailConfirm = new Hashtable();//getPathDetail(pathId, "1002");//明细信息
        Hashtable path = getPath(pathId);//信息
        if (path.Count == 0) return;
string url1 = "";
        string url2 = "";
        string url3 = "";
        string url4 = "";
        string remark1 = "";
        string remark2 = "";
        string remark3 = "";
        string remark4 = "";
        string reportContents = "";
        string closeReporttime = "";
        string chuzhenEmpAccID = "";
        string fuzhenEMpAccID = "";


        try
        {
            switch (reportTypes)
            {
               ........
            }
        }
        catch (Exception)
        {

            return;
        }
        #endregion
//===============================================================================以上是取数据模块,下面是计算这些数据所要用的空间(高度,宽度是固定的)
        #region 计算高度
        int heiPaing = 10;//行与行之间的间隔距离
        int heig = 40;
        int manping = 65;//左右两边的空格距离
        int MaxWidth = 795;
        int MaxHeight = 1224;
        Bitmap MyMap = new Bitmap(MaxWidth, MaxHeight);
        Graphics MyG = Graphics.FromImage(MyMap);
        #region 
        int jjHeighttemp = 210;//开始y坐标
        // heig = 260;
        List<string> tempsdtemp = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), path["Generally"].ToString().Trim());
        int lgstemp = tempsdtemp.Count;
        for (int i = 0; i < lgstemp; i++)
        {
            jjHeighttemp += 25;
        }
        #endregion
        #region 图像
        //换行,图片340
        int imgsHeighttemp = jjHeighttemp > 340 ? jjHeighttemp + 20 : 340;//指定图像模块开始y坐标
        //创建区域
        imgsHeighttemp = imgsHeighttemp + 25;

        int remYtoptemp = 0;
        try
        {

            // Rectangle rct = new Rectangle(imgOdd, imgsHeight, imgW, imgH);
            int tempRemYtoptemp = 0; remYtoptemp = 0;
            if (!string.IsNullOrEmpty(remark1))
            {
                remYtoptemp = imgsHeighttemp + imgH + 10;
                List<string> reList1temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark1);
                int lgs = reList1temp.Count;
                for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
            }


            if (!string.IsNullOrEmpty(remark2))
            {
                remYtoptemp = imgsHeighttemp + imgH + 10;
                List<string> reList2temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark2);
                int lgs = reList2temp.Count;
                for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
                tempRemYtoptemp = remYtoptemp;
            }

            remYtoptemp = Math.Max(remYtoptemp, tempRemYtoptemp);
            if (remYtoptemp == 0) imgsHeighttemp += remYtoptemp + 10;
            else imgsHeighttemp = remYtoptemp + 10;
            tempRemYtoptemp = 0; remYtoptemp = 0;
            if (!string.IsNullOrEmpty(remark3))
            {
                remYtoptemp = imgsHeighttemp + imgH + 10;
                List<string> reList3temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark3);
                int lgs = reList3temp.Count;
                for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
            }
            if (!string.IsNullOrEmpty(remark4))
            {
                tempRemYtoptemp = imgsHeighttemp + imgH + 10;
                List<string> reList4temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark4);
                int lgs = reList4temp.Count;
                for (int i = 0; i < lgs; i++) { tempRemYtoptemp = tempRemYtoptemp + 20; }
            }

            remYtoptemp = Math.Max(remYtoptemp, tempRemYtoptemp);//取得备注1和备注2坐标的大者
            if (remYtoptemp == 0) imgsHeighttemp += remYtoptemp + 10;
            else imgsHeighttemp = remYtoptemp + 10;


        }
        catch
        {


        }
        #endregion
        #region 诊断
        int pathHeighttemp = imgsHeighttemp;// > 870 ? imgsHeighttemp + 20 : 870;//块开始y坐标
        List<string> conListtemp = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), reportContents);
        int lgse = conListtemp.Count;
        for (int i = 0; i < lgse; i++) { pathHeighttemp += 25; }
        #endregion
        #region 页尾部分
        //换行划线
        int tailHeighttemp = pathHeighttemp > 1050 ? pathHeighttemp + 160 : 1050 + 160;//尾部开始y坐标 
        #endregion
//=========================计算完成,下面是开始画图

        #endregion

        MyMap.Dispose();
        #region 页头部分
        MyMap = new Bitmap(MaxWidth, tailHeighttemp);
        MyG.Clear(Color.White);
        MyG.Dispose();
        MyG = Graphics.FromImage(MyMap);
        MyG.Clear(Color.White);
        string textInf = getHostMsg("LocalConsultation");
        textInf = textInf + "";
        try
        {
            //30px
            MyG.DrawString(textInf, new Font("宋体", 22, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - textInf.Length * 30) / 2), heig), StringFormat.GenericTypographic);

        }
        catch (Exception) { }

        heig += heiPaing + 30;//行间隔+字体高度
        //加载文字 25px
        string text2 = "报告";
        MyG.DrawString(text2, new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - text2.Length * 25) / 2), heig), StringFormat.GenericTypographic);
        heig += heiPaing + 10;//行间隔+字体高度
        //加载文字 25px
        string stringsef = "号码:" + path["ObjID"].ToString().Trim();
        MyG.DrawString(stringsef, new Font("宋体", (float)10.5, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - stringsef.Length * 20 - 20)), heig), StringFormat.GenericTypographic);
       
        #endregion
        #region 信息
        int basicHeight = 110;//基本信息开始y坐标
        //换行划横线
        basicHeight += heiPaing;
        MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(manping, basicHeight), new Point(MaxWidth - manping, basicHeight));
        //换行:
        basicHeight += heiPaing + 10;
        MyG.DrawString("姓名:", new Font("宋体", 12), Brushes.Black, new Point(60, basicHeight));
        int lgode = 60 + 5 * 16;
        MyG.DrawString(path["Name"].ToString().Trim(), new Font("宋体", 12), Brushes.Black, new Point(lgode, basicHeight));

        int mmlg = ((MaxWidth - manping * 2) / 3);
        MyG.DrawString("性别:", new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60, basicHeight));
        string sex = path["Sex"].ToString().Trim();
        MyG.DrawString(sex, new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60 + 5 * 16, basicHeight));

        string age = path["Age"].ToString().Trim();
        MyG.DrawString("年龄:", new Font("宋体", 12), Brushes.Black, new Point(mmlg * 2 + age.Length * 15 + 20, basicHeight));
        MyG.DrawString(age, new Font("宋体", 12), Brushes.Black, new Point(mmlg * 2 + 60 + age.Length * 5 + 16 * 3 + 20, basicHeight));


        //换行:
        basicHeight += heiPaing + 20;

        MyG.DrawString("生:", new Font("宋体", 12), Brushes.Black, new Point(60, basicHeight));
        lgode = 60 + 5 * 16;
        string sjdoctor = getEmp(path["UploadAccSysID"].ToString().Trim())[0];
        MyG.DrawString(sjdoctor, new Font("宋体", 12), Brushes.Black, new Point(lgode, basicHeight));

        mmlg = ((MaxWidth - manping * 2) / 3);
        MyG.DrawString("日期:", new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60, basicHeight));
        string sjdate = DateTime.Parse(path["CreateTime"].ToString().Trim()).ToShortDateString();
        MyG.DrawString(sjdate, new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60 + 5 * 16, basicHeight));

        //换行划线
        basicHeight += heiPaing + 16;
        MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(60, basicHeight), new Point(MaxWidth - 60, basicHeight));
        #endregion
        #region
        int jjHeight = 210;//开始y坐标
        MyG.DrawString("检查:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, jjHeight), StringFormat.GenericTypographic);

       
        List<string> tempsd = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), path["Generally"].ToString().Trim());
        int lg2s = tempsd.Count;
        for (int i = 0; i < lg2s; i++)
        {
            jjHeight += 25;
            MyG.DrawString(tempsd[i], new Font("宋体", 12), Brushes.Black, new Point(60, jjHeight));
        }
      
        #endregion
        #region 图像
        //换行,图片340
        int imgsHeight = jjHeight > 340 ? jjHeight + 20 : 340;//模块开始y坐标
        MyG.DrawString("图像:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, imgsHeight));
        //创建图片路径


        //创建区域
        imgsHeight = imgsHeight + 25;

        int imgOdd = 141;//(MaxWidth - manping * 2) / 4 + manping - img1.Width / 2;      //x1坐标

        int imgEven = 410;// (MaxWidth - manping * 2) / 4 * 3 + manping - img1.Width / 2; //x2坐标
        int remYtop = 0;
        try
        {

            int wi, hi;
            System.Drawing.Image img1;
            System.Drawing.Image img2;
            System.Drawing.Image img3;
            System.Drawing.Image img4;
            // Rectangle rct = new Rectangle(imgOdd, imgsHeight, imgW, imgH);
            int tempRemYtop = 0;
            if (!string.IsNullOrEmpty(url1))
            {
                wi = 0; hi = 0;
                //  url1 = FileHostUrl + "/" + url1;
                try
                {
                    MemoryStream ms1 = new MemoryStream(getBytes(url1, null, null, ""));
                    img1 = System.Drawing.Image.FromStream(ms1);

                    getImageWithAndHeigth(img1, out wi, out hi, imgW, imgH);
                    Rectangle rct = new Rectangle(imgOdd, imgsHeight, wi, hi);
                    MyG.DrawImage(img1, rct);//画图1
                    img1.Dispose();
                    ms1.Close();
                    ms1.Dispose();

                }
                catch (Exception ex)
                {


                }


                remYtop = imgsHeight + hi + 10;

                List<string> reList1 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark1);
                for (int i = 0; i < reList1.Count; i++)
                {
                    MyG.DrawString(reList1[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgOdd, remYtop));
                    remYtop = remYtop + 20;

                }
            }


            if (!string.IsNullOrEmpty(url2))
            {
                wi = 0; hi = 0;
                //  url2 = FileHostUrl + "/" + url2;
                try
                {
                    MemoryStream ms2 = new MemoryStream(getBytes(url2, null, null, ""));
                    img2 = System.Drawing.Image.FromStream(ms2);
                    getImageWithAndHeigth(img2, out wi, out hi, imgW, imgH);
                    Rectangle rct = new Rectangle(imgEven, imgsHeight, wi, hi);
                    MyG.DrawImage(img2, rct);//画图1
                    img2.Dispose();
                    ms2.Close();
                    ms2.Dispose();
                }
                catch (Exception)
                { }
                tempRemYtop = imgsHeight + hi + 10;
                List<string> reList2 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark2);
                for (int i = 0; i < reList2.Count; i++)
                {
                    MyG.DrawString(reList2[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgEven, tempRemYtop));
                    tempRemYtop = tempRemYtop + 20;

                }

            }

            remYtop = Math.Max(remYtop, tempRemYtop);//取得备注1和备注2坐标的大者
            if (remYtop == 0) imgsHeight += remYtop + 20;
            else imgsHeight = remYtop + 20;

            remYtop = 0;
            tempRemYtop = 0;
            if (!string.IsNullOrEmpty(url3))
            {
                wi = 0; hi = 0;

                //url3 = FileHostUrl + "/" + url3;
                try
                {
                    MemoryStream ms3 = new MemoryStream(getBytes(url3, null, null, ""));
                    img3 = System.Drawing.Image.FromStream(ms3);
                    getImageWithAndHeigth(img3, out wi, out hi, imgW, imgH);
                    Rectangle rct = new Rectangle(imgOdd, imgsHeight, wi, hi);
                    MyG.DrawImage(img3, rct);//画图1
                    img3.Dispose();
                    ms3.Close();
                    ms3.Dispose();
                }
                catch (Exception)
                {


                }
                remYtop = imgsHeight + hi + 10;
                List<string> reList3 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark3);
                for (int i = 0; i < reList3.Count; i++)
                {
                    MyG.DrawString(reList3[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgOdd, remYtop));
                    remYtop = remYtop + 20;

                }
            }

            if (!string.IsNullOrEmpty(url4))
            {
                wi = 0; hi = 0;
                //  url4 = FileHostUrl + "/" + url4;
                try
                {
                    MemoryStream ms4 = new MemoryStream(getBytes(url4, null, null, ""));
                    img4 = System.Drawing.Image.FromStream(ms4);
                    getImageWithAndHeigth(img4, out wi, out hi, imgW, imgH);
                    Rectangle rct = new Rectangle(imgEven, imgsHeight, wi, hi);
                    MyG.DrawImage(img4, rct);//画图1
                    img4.Dispose();
                    ms4.Close();
                    ms4.Dispose();
                }
                catch (Exception)
                {


                }


                tempRemYtop = imgsHeight + hi + 10;
                List<string> reList4 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark4);
                for (int i = 0; i < reList4.Count; i++)
                {

                    MyG.DrawString(reList4[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgEven, tempRemYtop));
                    tempRemYtop = tempRemYtop + 20;
                }

            }
            remYtop = Math.Max(remYtop, tempRemYtop);//取得备注1和备注2坐标的大者
            if (remYtop == 0) imgsHeight += remYtop + 20;
            else imgsHeight = remYtop + 20;



        }
        catch
        {


        }
        #endregion
        #region 
        int pathHeight = imgsHeight;// > 870 ? imgsHeight + 20 : 870;//开始y坐标
        MyG.DrawString("测试:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, pathHeight));
        List<string> conList = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), reportContents);
        int reportLine = conList.Count;
        for (int i = 0; i < reportLine; i++) { pathHeight += 25; MyG.DrawString(conList[i], new Font("宋体", 12), Brushes.Black, new Point(60, pathHeight)); }
        #endregion
        #region 页尾部分
        //以下是页尾部分
        //换行划线
        int tailHeight = pathHeight > 1050 ? pathHeight + 20 : 1050;//尾部开始y坐标
        tailHeight += heiPaing + 16;
        MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(60, tailHeight), new Point(MaxWidth - 60, tailHeight));

        //换行,日期
        tailHeight += heiPaing + 10;
        MyG.DrawString("日期:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));
        lgode = 60 + 5 * 16;
        string conDate = DateTime.Parse(closeReporttime).ToShortDateString();
        MyG.DrawString(conDate, new Font("宋体", 12), Brushes.Black, new Point(lgode, tailHeight));
        //换行,日期
        tailHeight += heiPaing + 15;
        MyG.DrawString("第一个人:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));
        string userName = "";
        string[] sdoc = new string[2];//
        if (!string.IsNullOrEmpty(chuzhenEmpAccID.Trim()))
        {

            sdoc = getEmp(chuzhenEmpAccID);//
            lgode = 60 + 5 * 16;
            userName = sdoc[0] != "" ? sdoc[0] : "";
            MyG.DrawString(userName, new Font("宋体", 12), Brushes.Black, new Point(lgode, tailHeight));
        }
        string[] edoc = new string[2];//第二个人的数据
        int temp = (MaxWidth - manping * 2) / 2 + manping;
        MyG.DrawString("二个人:", new Font("宋体", 12), Brushes.Black, new Point(temp, tailHeight));
        if (!string.IsNullOrEmpty(fuzhenEMpAccID.Trim()))
        {


            edoc = getEmp(fuzhenEMpAccID);
            userName = edoc[0] != "" ? edoc[0] : "";
            MyG.DrawString(edoc[0], new Font("宋体", 12), Brushes.Black, new Point(temp + 76, tailHeight));
        }


        tailHeight += heiPaing + 15;
        MyG.DrawString("签字:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));//
        try
        {
            if (!string.IsNullOrEmpty(sdoc[1]))
            {
                MemoryStream ms = new MemoryStream(getBytes(sdoc[1], null, null, ""));
                System.Drawing.Image sign = System.Drawing.Image.FromStream(ms);
                Rectangle rct = new Rectangle(110, tailHeight, 100, 30);
                MyG.DrawImage(sign, rct);//数字签名
                sign.Dispose();
                ms.Close();
                ms.Dispose();

            }
        }
        catch
        {
        }


        MyG.DrawString("签字:", new Font("宋体", 12), Brushes.Black, new Point(temp, tailHeight));
        try
        {
            if (!string.IsNullOrEmpty(edoc[1]))
            {
                MemoryStream ms = new MemoryStream(getBytes(edoc[1], null, null, ""));
                System.Drawing.Image sign = System.Drawing.Image.FromStream(ms);
                Rectangle rct = new Rectangle(temp + 50, tailHeight, 100, 30);
                MyG.DrawImage(sign, rct);//数字签名
                sign.Dispose();
                ms.Close();
                ms.Dispose();

            }
        }
        catch
        {
        }
        #endregion

        if (types == 1)
        {
            //输出显示
            MyMap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        if (types == 2)
        {
            MemoryStream ms = new MemoryStream();
            MyMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode("report.jpg", System.Text.Encoding.UTF8));
            Response.BinaryWrite(ms.ToArray());
            Response.Flush();
            Response.End();
        }
        MyMap.Dispose();
        MyG.Dispose();
    }


其他方法:文本换行用。

 public static List<string> GetStringLine(Graphics dc, Font font, int nWidth, string str)
    {
        int nStrCount = str.Length;
        List<string> strLine = new List<string>();
        int nStartPos = 0;
        int nEndPos = 0;
        for (int nIndex = 0; nIndex < nStrCount; nIndex++)
        {
            nEndPos = nIndex;
            if (dc.MeasureString(str.Substring(nStartPos, nEndPos - nStartPos + 1), font).Width > nWidth)
            {
                strLine.Add(str.Substring(nStartPos, nEndPos - nStartPos));
                nStartPos = nEndPos;
            }

            if (nIndex == nStrCount - 1)
            {
                strLine.Add(str.Substring(nStartPos));
            }
        }

        return strLine;
    }


以上代码分为三部分,

第一部分是先取出数据,对数据进行处理,第二部分是对数据进行预加载,看看这个画板要多大。第三部分是开始填充画板。最后是显示或者下载。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C# 中使用企业微信 API 可以通过发送 HTTP 请求来与企业微信进行交互。你可以使用 HttpClient 类来发送请求,并通过调用企业微信 API 的不同接口来实现各种功能,如发送消息、获取用户信息等。 首先,你需要在企业微信后台注册一个应用,并获取到应用的相关信息,包括企业ID、应用ID、应用密钥等。 下面是一个示例代码,演示如何使用 C# 发送消息到企业微信: ```csharp using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { string corpId = "your-corp-id"; string appSecret = "your-app-secret"; string agentId = "your-agent-id"; string accessToken = await GetAccessToken(corpId, appSecret); if (!string.IsNullOrEmpty(accessToken)) { await SendMessage(accessToken, agentId, "user-id", "Hello from C#!"); } } static async Task<string> GetAccessToken(string corpId, string appSecret) { using (HttpClient client = new HttpClient()) { string url = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpId}&corpsecret={appSecret}"; HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); // 解析响应获取 access_token // 注意:实际开发中,建议将 access_token 缓存在本地,并定期更新 // 这里仅为示例,直接返回获取到的 access_token return "your-access-token"; } else { Console.WriteLine($"Failed to get access token. Status code: {response.StatusCode}"); return null; } } } static async Task SendMessage(string accessToken, string agentId, string userId, string message) { using (HttpClient client = new HttpClient()) { string url = $"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={accessToken}"; string requestBody = $"{{\"touser\": \"{userId}\", \"agentid\": \"{agentId}\", \"msgtype\": \"text\", \"text\": {{\"content\": \"{message}\"}}}}"; StringContent content = new StringContent(requestBody, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { Console.WriteLine("Message sent successfully."); } else { Console.WriteLine($"Failed to send message. Status code: {response.StatusCode}"); } } } } ``` 请替换代码中的 `your-corp-id`、`your-app-secret`、`your-agent-id` 和 `user-id` 分别为你的企业ID、应用密钥、应用ID和发送消息的用户ID。这个示例代码是使用企业微信的消息推送接口发送文本消息给指定用户。 希望这个示例能帮到你,如果有其他问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值