Exemple #1
0
        /// <summary>
        /// 生成卷烟品牌的图片
        /// 其中每5个品牌为一个组
        /// 图片640的宽度只能放下5个品牌名称
        /// </summary>
        /// <param name="picture"></param>
        /// <param name="startledgroup">起始组</param>
        /// <param name="endledgroup">结束组</param>
        /// <param name="filename">保存文件名</param>
        /// <returns></returns>
        public bool CreateCigNamePic(PictureBox picture, int startledgroup, int endledgroup, string filename)
        {
            try
            {
                Bitmap image = new Bitmap(640, 192);
                int    left  = image.Height / 2 - 18;
                int    top   = -image.Width / 2;
                //创建Graphics类对象
                Graphics g = Graphics.FromImage(image);
                //设置旋转中心点
                g.TranslateTransform(image.Width / 2, image.Height / 2);
                //设置旋转角度
                g.RotateTransform(-90);


                LedDisplay ledDisplay = new LedDisplay();
                ledDisplay.GetLedLineboxs();


                //将文本生成到图片框内
                int leftoffset = 1;
                for (int i = startledgroup; i <= endledgroup; i++)
                {
                    //按组获取显示的名称拼成字符串
                    string ledstring = ledDisplay.GetCigNames(i);

                    int topmove = 0;
                    foreach (char c in ledstring)
                    {
                        //画名称
                        g.DrawString(c.ToString(), Font, new SolidBrush(Color.Red), left - ((leftoffset - 1) * 16), top + topmove);
                        topmove += 16;
                    }
                    leftoffset++;

                    //LED屏有问题必须写空两行再写 
                    int j = 1;
                    if (i == 2 || i == 8)
                    {
                        //写两行空白字符
                        while (j++ <= 2)
                        {
                            //空白字符
                            ledstring = "                                        ";

                            topmove = 0;
                            foreach (char c in ledstring)
                            {
                                //画名称
                                g.DrawString(c.ToString(), Font, new SolidBrush(Color.Red), left - ((leftoffset - 1) * 16), top + topmove);
                                topmove += 16;
                            }
                            leftoffset++;
                        }
                    }
                }
                g.ResetTransform();


                //显示生成的图片
                picture.Image = (Image)image;
                picture.Image.Save(filename, ImageFormat.Bmp);


                Stream path = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                Image  img  = new Bitmap(path);
                picture.Image = img;
                path.Close();
                path.Dispose();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }