加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

动态绘制WPF中画图与保存

(2010-12-07 09:12:24)
标签:

杂谈

不闲话,代码。

c#Code:

 private void DrawImg()
        {
            getscnstr();
            //if (scn.State == ConnectionState.Closed)
            //{
            //    scn.Open();
            //}
            //String SqlStr = "SELECT top 1000 [PersonerCode]      ,[Personer]      ,[ProfessionalKnowledge]      ,[ManageKnowledge]      ,[ProfessionalExperience]      ,[LeaderExperience]      ,[DrivePower]      ,[MeanPower]      ,[AffectPower]      ,[DoPower]      ,[ValueMean]  FROM [ais].[dbo].[PM_PersonValue]";

            //DataTable dt = acc.GetSereachData(scn, SqlStr);
            int Rectanglefar = 5;
            int RectangleHigh = 15;
            int ImagePer = 150;
            int StartPoint = 0;
            if (txtprfar.Text.Trim() != "")
                Rectanglefar = Convert.ToInt16(txtprfar.Text.Trim());
            if (txtprhigh.Text.Trim() != "")
                RectangleHigh = Convert.ToInt16(txtprhigh.Text.Trim());
            if (txtImgPer.Text.Trim() != "")
                ImagePer = Convert.ToInt16(txtImgPer.Text.Trim());
            if (txtstart.Text.Trim() != "")
                StartPoint = Convert.ToInt16(txtstart.Text.Trim());
            if (StartPoint > 5)
            {
                MessageBox.Show("起始点在0-5之间,请录入正确数字");
                return;
            }

            int high = 50000;
            high = dt.Rows.Count * (RectangleHigh + Rectanglefar) * 28 + 20;//可设置每条记录距离
            IMG = new Bitmap(900, high);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(IMG);
            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Blue, 1);

           
           
            //for (int n = 2; n < dt.Columns.Count; n++)
            //{
            //    string header = dt.Columns[n].ColumnName;

            //    dt.Columns[n].Expression = header+" * 10";
            //}
            for (int n = 0; n < dt.Rows.Count; n++)
            {
                int ny=400*(n);
                int nx=0;

               

                System.Drawing.Point[] po = new System.Drawing.Point[28];
                for (int i = 0; i <= 27; i++)//设置宽
                {
                    if(dt.Rows[n][i].ToString()=="")
                    {
                        i++;
                    }
                    int y = (i + 1) * (RectangleHigh + Rectanglefar) + ny;//高度
                    int x = Convert.ToInt16(Convert.ToDecimal(dt.Rows[n][i + 3]) * ImagePer) - StartPoint*ImagePer;//长度
                    if (x < 0)
                    { x = 0; }
                    po[i] = new System.Drawing.Point(x,y);//画线的点

                    g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 3), x, y, 2, 2);//在点上作标记

                    if (cbPrName.IsChecked == true)
                    {
                        if (cbPrValue.IsChecked == true)
                        {
                            g.DrawString(dt.Columns[i + 3].ColumnName.Trim() + ":" + dt.Rows[n][i + 3].ToString(), new Font("宋体", 8f), System.Drawing.Brushes.Black, new System.Drawing.Point(x, y));
                        }
                        else
                        {
                            g.DrawString(dt.Columns[i + 3].ColumnName.Trim(), new Font("宋体", 8f), System.Drawing.Brushes.Black, new System.Drawing.Point(x, y));
                        }
                    }
                    else
                    {
                        if (cbPrValue.IsChecked == true)
                        {
                            g.DrawString(dt.Rows[n][i + 3].ToString(), new Font("宋体", 8f), System.Drawing.Brushes.Black, new System.Drawing.Point(x, y));
                        }
                    }


                    g.DrawRectangle(myPen, nx, y, x, RectangleHigh);
                    System.Drawing.Brush bs = new SolidBrush(System.Drawing. Color.Blue);
                    if (CbxColor.Text == "red")
                    {
                        bs =new SolidBrush(System.Drawing.Color.Red);
                    }
                    else if (CbxColor.Text.Trim() == "gray")
                    {
                        bs=new SolidBrush(System.Drawing.Color.Gray);
                    }
                    else if (CbxColor.Text.Trim() == "green")
                    {
                        bs = new SolidBrush(System.Drawing.Color.Green);
                    }
                    g.FillRectangle(bs, nx, y, x, RectangleHigh);

                    System.Windows.Shapes.Rectangle aa = new System.Windows.Shapes.Rectangle();
                  
                }
                Font font = new Font("宋体", 12f);
               // g.DrawString(dt.Rows[n][0].ToString() + " " + dt.Rows[n][1].ToString(), font, System.Drawing.Brushes.Black, new System.Drawing.Point(0, high - n * 100 - 200));
                //g.DrawLines(myPen, po);//此处为画曲线代码

               
            }
            ImgShowData.IsEnabled = true;
            ImgShowData.Width = 1000;
            ImgShowData.Height = high;
            ImgShowData.IsEnabled = false;
            ShowImage(IMG, ImgShowData);
            image1.Height = high;
            ShowImage(IMG, image1);
            IMG.Save("c:\\2.jpg");           
        }
//显示图片:

private void ShowImage(Bitmap BTP,System.Windows.Controls.Image  IMG)
        {
            ImageSource aa;

            ImageSourceConverter ImgCon = new ImageSourceConverter();
           
            //System.Drawing.Bitmap Btp = new System.Drawing.Bitmap(@"D:\Applition\WPF\TEST\WPFTEST\WPFTEST\Images\星.gif");//可用于调用本地
            MemoryStream stream = new MemoryStream();
            BTP.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            BTP.Save("D:\\btp.jpg");
            aa = (ImageSource)ImgCon.ConvertFrom(stream);
            IMG.Source = aa;

        }

http://s15/middle/3e653ee6g96c7d565777e&690

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有