コード例 #1
0
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public bool ProcessCode()
        {
            StrCode = GenerateCheckCode(intLength);
            if (StrCode == null || StrCode.Trim() == String.Empty)
            {
                return(false);
            }
            Bitmap   image = new Bitmap((int)Math.Ceiling((intLength * fontSize)), intHeigth);
            Graphics g     = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的背景噪音线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Times New Roman", fontSize, (FontStyle.Italic | FontStyle.Bold)); // FontStyle.Strikeout
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)), 1.2f, true);
                g.DrawString(StrCode, font, brush, 2, (float)(intHeigth / 2.0 - fontSize + 4));
                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
                ImageStream.Close();
                ImageStream = new MemoryStream();
                image.Save(ImageStream, ImageFormat.Gif);
                return(true);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                return(false);
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
コード例 #2
0
ファイル: frmViewer.cs プロジェクト: theRealZing/ICL-X9.37
        public void Byte2Image(ref Image NewImage, byte[] ByteArr, int startIndex)
        {
            MemoryStream ImageStream = null;

            byte[] newArr = new byte[ByteArr.GetUpperBound(0) - startIndex + 1];
            Array.Copy(ByteArr, startIndex, newArr, 0, ByteArr.Length - startIndex);
            try
            {
                if (newArr.GetUpperBound(0) > 0)
                {
                    ImageStream = new MemoryStream(newArr);
                    NewImage    = Image.FromStream(ImageStream);
                }
                else
                {
                    NewImage = null;
                }
                ImageStream.Close();
            }
            catch (Exception ex)
            {
                NewImage = null;
            }
        }