Beispiel #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            System.Drawing.Pen pen =
                new System.Drawing.Pen(this.ForeColor, 0);

            Rectangle rect = this.ClientRectangle;

            rect.Width  -= 1;
            rect.Height -= 1;

            System.Drawing.Brush backBrush =
                new System.Drawing.Drawing2D.HatchBrush(
                    System.Drawing.Drawing2D.HatchStyle.SmallCheckerBoard,
                    this.ForeColor, this.BackColor);
            e.Graphics.FillRectangle(backBrush, rect);
            backBrush.Dispose();

            if (_brush != null)
            {
                e.Graphics.FillRectangle(_brush, rect);
            }
            e.Graphics.DrawRectangle(pen, rect);

            pen.Dispose();
        }
Beispiel #2
0
 //------------------------------------------------------------------------
 private void CGanttTree_DragOver(object sender, DragEventArgs e)
 {
     UndrawZoneDestDragDrop();
     if (e.Data.GetDataPresent(typeof(TreeNode)) && !LockEdition)
     {
         TreeNode nodeMoved = e.Data.GetData(typeof(TreeNode)) as TreeNode;
         TreeNode nodeDest  = null;
         bool     bAfter    = false;
         Point    pt        = PointToClient(new Point(e.X, e.Y));
         GetDestDragDrop(nodeMoved, pt, ref nodeDest, ref bAfter);
         if (nodeDest != null)
         {
             e.Effect = DragDropEffects.Move;
             Rectangle rct = nodeDest.Bounds;
             using (Graphics g = CreateGraphics())
             {
                 Brush br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.DottedGrid, Color.Black, Color.Blue);
                 int   nY = rct.Top;
                 if (bAfter)
                 {
                     nY = rct.Bottom;
                 }
                 m_rectZoneDestDragDrop = new Rectangle(0, nY - 1, Width, 3);
                 g.FillRectangle(br, m_rectZoneDestDragDrop);
                 br.Dispose();
             }
         }
         else
         {
             e.Effect = DragDropEffects.None;
             m_rectZoneDestDragDrop = new Rectangle(0, 0, 0, 0);
         }
     }
 }
Beispiel #3
0
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.Enabled)
            {
                System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, bkcolor, dwcolor);
                Pen p = new Pen(dwcolor);

                if (rect.Height < 2)
                {
                    e.Graphics.DrawLine(p, rect.X, rect.Y, rect.X + Width, rect.Y);
                }
                else
                {
                    e.Graphics.FillRectangle(hb, rect);
                }

                hb.Dispose();
                p.Dispose();
            }
        }
Beispiel #4
0
		protected override void OnPaint(PaintEventArgs e)
		{
			System.Drawing.Pen pen = 
				new System.Drawing.Pen(this.ForeColor, 0);

			Rectangle rect = this.ClientRectangle;
			rect.Width -= 1;
			rect.Height -= 1;

			System.Drawing.Brush backBrush =
				new System.Drawing.Drawing2D.HatchBrush(
					System.Drawing.Drawing2D.HatchStyle.SmallCheckerBoard,
					this.ForeColor, this.BackColor);
			e.Graphics.FillRectangle(backBrush, rect);
			backBrush.Dispose();

			if (_brush != null)
				e.Graphics.FillRectangle(_brush, rect);
			e.Graphics.DrawRectangle(pen, rect);

            pen.Dispose();
		}
        /// <summary>
        /// Evaluates the DrawStyle of the control and calls the appropriate
        /// drawing function for content
        /// </summary>
        protected void DrawContent()
        {
            Graphics g = Graphics.FromImage(m_Output);

            Rectangle Rect = new Rectangle(4, 11, Width - 8, Height - 21);

            // Draw a nice checker box
            System.Drawing.Drawing2D.HatchBrush Checker = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LargeCheckerBoard, Color.Gray, Color.DarkGray);
            g.FillRectangle(Checker, Rect);
            Checker.Dispose();

            // Draw a gradient box
            System.Drawing.Drawing2D.GraphicsPath Path = new System.Drawing.Drawing2D.GraphicsPath();
            Path.AddRectangle(Rect);

            System.Drawing.Drawing2D.PathGradientBrush Gradient = new System.Drawing.Drawing2D.PathGradientBrush(Path);
            Gradient.SurroundColors = new Color[] { Color.FromArgb(0, Color.White), Color.FromArgb(255, Color.White), Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White) };
            Gradient.CenterPoint    = new PointF(0.5f * (Rect.Left + Rect.Right), .5f * (Rect.Bottom + Rect.Top));
            Gradient.CenterColor    = Color.FromArgb(128, Color.White);

            g.FillRectangle(Gradient, Rect);

            Gradient.Dispose();
        }
Beispiel #6
0
        internal void DrawPlusMinusLines(Graphics g)
        {
            if (!IsInATreeListView)
            {
                return;
            }
            if (TreeListView._updating)
            {
                return;
            }
            Debug.Assert(!TreeListView.InvokeRequired);
            if (!TreeListView.ShowPlusMinus || TreeListView.Columns.Count == 0)
            {
                return;
            }
            int       itemLevel     = Level;
            Rectangle plusminusRect = GetBounds(TreeListViewItemBoundsPortion.PlusMinus);
            Rectangle entireRect    = GetBounds(TreeListViewItemBoundsPortion.Entire);

            System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50, TreeListView.PlusMinusLineColor, BackColor);
            Pen   pen = new Pen(hb);
            Point point1, point2;

            #region Vertical line
            point1 = new Point(
                plusminusRect.Right - SystemInformation.SmallIconSize.Width / 2 - 1,
                entireRect.Top);
            point2 = new Point(point1.X, entireRect.Bottom);
            // If ListView has no items that have the same level before this item
            if (!HasLevelBeforeItem(itemLevel))
            {
                point1.Y += SystemInformation.SmallIconSize.Height / 2;
            }
            // If ListView has no items that have the same level after this item
            if (!HasLevelAfterItem(itemLevel))
            {
                point2.Y -= SystemInformation.SmallIconSize.Height / 2 + 1;
            }
            if (TreeListView.Columns[0].Width > (Level + 1) * SystemInformation.SmallIconSize.Width)
            {
                g.DrawLine(pen, point1, point2);
            }
            #endregion
            #region Horizontal line
            point1 = new Point(
                plusminusRect.Right - SystemInformation.SmallIconSize.Width / 2 - 1,
                GetBounds(TreeListViewItemBoundsPortion.Entire).Top + SystemInformation.SmallIconSize.Height / 2);
            point2 = new Point(plusminusRect.Right + 1, point1.Y);
            if (TreeListView.Columns[0].Width > (Level + 1) * SystemInformation.SmallIconSize.Width)
            {
                g.DrawLine(pen, point1, point2);
            }
            #endregion
            #region Lower Level lines
            for (int level = Level - 1; level > -1; level--)
            {
                if (HasLevelAfterItem(level))
                {
                    point1 = new Point(
                        SystemInformation.SmallIconSize.Width * (2 * level + 1) / 2 + entireRect.X,
                        entireRect.Top);
                    point2 = new Point(
                        point1.X, entireRect.Bottom);
                    if (TreeListView.Columns[0].Width > (level + 1) * SystemInformation.SmallIconSize.Width)
                    {
                        g.DrawLine(pen, point1, point2);
                    }
                }
            }
            #endregion
            pen.Dispose();
            hb.Dispose();
        }
Beispiel #7
0
    private void GenerateImage()
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap
                                           (this.width, this.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        System.Drawing.Rectangle            rect       = new System.Drawing.Rectangle(0, 0, this.width, this.height);
        System.Drawing.Drawing2D.HatchBrush hatchBrush =
            new System.Drawing.Drawing2D.HatchBrush(
                System.Drawing.Drawing2D.HatchStyle.SmallConfetti,
                System.Drawing.Color.LightGray, System.Drawing.Color.White);
        g.FillRectangle(hatchBrush, rect);
        System.Drawing.SizeF size;
        float fontSize = rect.Height + 1;

        System.Drawing.Font font;

        do
        {
            fontSize--;
            font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, fontSize, System.Drawing.FontStyle.Bold);
            size = g.MeasureString(this.text, font);
        } while (size.Width > rect.Width);
        System.Drawing.StringFormat format = new System.Drawing.StringFormat();
        format.Alignment     = System.Drawing.StringAlignment.Center;
        format.LineAlignment = System.Drawing.StringAlignment.Center;
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        //path.AddString(this.text, font.FontFamily, (int) font.Style,
        //    font.Size, rect, format);
        path.AddString(this.text, font.FontFamily, (int)font.Style, 75, rect, format);
        float v = 4F;

        System.Drawing.PointF[] points =
        {
            new System.Drawing.PointF(this.random.Next(rect.Width) / v, this.random.Next(
                                          rect.Height) / v),
            new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v,
                                      this.random.Next(rect.Height) / v),
            new System.Drawing.PointF(this.random.Next(rect.Width) / v,
                                      rect.Height - this.random.Next(rect.Height) / v),
            new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v,
                                      rect.Height - this.random.Next(rect.Height) / v)
        };
        System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
        matrix.Translate(0F, 0F);
        path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);
        hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
            System.Drawing.Drawing2D.HatchStyle.Percent10
            , System.Drawing.Color.Black, System.Drawing.Color.SkyBlue);
        g.FillPath(hatchBrush, path);
        int m = System.Math.Max(rect.Width, rect.Height);

        for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
        {
            int x = this.random.Next(rect.Width);
            int y = this.random.Next(rect.Height);
            int w = this.random.Next(m / 50);
            int h = this.random.Next(m / 50);
            g.FillEllipse(hatchBrush, x, y, w, h);
        }
        font.Dispose();
        hatchBrush.Dispose();
        g.Dispose();
        this.image = bitmap;
    }
Beispiel #8
0
        //		protected override CreateParams CreateParams
        //		{
        //			get
        //			{
        //				CreateParams cp = base.CreateParams;
        //				cp.ExStyle |= 0x20;
        //				return cp;
        //			}
        //		}
        //
        //		protected override void OnMove(EventArgs e)
        //		{
        //			RecreateHandle();
        //		}
        //
        //		protected override void OnPaintBackground(PaintEventArgs e)
        //		{
        //			// do nothing
        //		}

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);

            int ym;

            switch (LineVerticalAlign)
            {
            case LineVerticalAlign.Top:
                ym = 0;
                break;

            case LineVerticalAlign.Middle:
                ym = Convert.ToInt32(Math.Ceiling((decimal)Size.Height / 2)) - 1;
                break;

            case LineVerticalAlign.Bottom:
                ym = Size.Height - 2;
                break;

            default:
                ym = 0;
                break;
            }

            SizeF captionSizeF  = e.Graphics.MeasureString(Caption, this.Font, this.Width - CaptionMarginSpace * 2, StringFormat.GenericDefault);
            int   captionLength = Convert.ToInt32(captionSizeF.Width);

            int beforeCaption;
            int afterCaption;

            if (Caption == "")
            {
                beforeCaption = CaptionMarginSpace;
                afterCaption  = CaptionMarginSpace;
            }
            else
            {
                switch (CaptionOrizontalAlign)
                {
                case CaptionOrizontalAlign.Left:
                    beforeCaption = CaptionMarginSpace;
                    afterCaption  = CaptionMarginSpace + CaptionPadding * 2 + captionLength;
                    break;

                case CaptionOrizontalAlign.Center:
                    beforeCaption = (Width - captionLength) / 2 - CaptionPadding;
                    afterCaption  = (Width - captionLength) / 2 + captionLength + CaptionPadding;
                    break;

                case CaptionOrizontalAlign.Right:
                    beforeCaption = Width - CaptionMarginSpace * 2 - captionLength;
                    afterCaption  = Width - CaptionMarginSpace;
                    break;

                default:
                    beforeCaption = CaptionMarginSpace;
                    afterCaption  = CaptionMarginSpace;
                    break;
                }
            }

            // Lines
            if (_LineOrientation == LineOrientation.Horizontal)
            {
                // -------
                // |      ...caption...
                e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
                                     new Point[] {
                    new Point(0, ym + 1),
                    new Point(0, ym),
                    new Point(beforeCaption, ym)
                }
                                     );

                //                  -------
                //	      ...caption...
                e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
                                     new Point[] {
                    new Point(afterCaption, ym),
                    new Point(this.Width, ym)
                }
                                     );

                //        ...caption...
                // -------
                e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
                                     new Point[] {
                    new Point(0, ym + 1),
                    new Point(beforeCaption, ym + 1)
                }
                                     );

                //        ...caption...       |
                //                  -------
                e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
                                     new Point[] {
                    new Point(afterCaption, ym + 1),
                    new Point(this.Width, ym + 1),
                    new Point(this.Width, ym)
                }
                                     );
            }
            else if (_LineOrientation == LineOrientation.Vertical)
            {
                System.Drawing.Drawing2D.HatchBrush aHatchBrush = new
                                                                  System.Drawing.Drawing2D.HatchBrush
                                                                      (System.Drawing.Drawing2D.HatchStyle.Vertical,
                                                                      SystemColors.ControlLightLight,
                                                                      SystemColors.ControlDark);

                Pen myPen = new Pen(aHatchBrush, 2);
                e.Graphics.DrawLine(myPen, beforeCaption / 2, 0, beforeCaption / 2, ClientRectangle.Height);
                myPen.Dispose(); aHatchBrush.Dispose();
            }

            // Render caption
            if (Caption != "")
            {
                switch (_LineOrientation)
                {
                case LineOrientation.Horizontal:
                    e.Graphics.FillRectangle(new SolidBrush(BackColor),
                                             beforeCaption + CaptionPadding, 1,
                                             e.Graphics.MeasureString(Caption, this.Font).Width,
                                             e.Graphics.MeasureString(Caption, this.Font).Height
                                             );
                    e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), beforeCaption + CaptionPadding, 1);
                    break;

                case LineOrientation.Vertical:
                    e.Graphics.FillRectangle(new SolidBrush(BackColor),
                                             0, beforeCaption + CaptionPadding,
                                             e.Graphics.MeasureString(Caption, this.Font).Height,
                                             e.Graphics.MeasureString(Caption, this.Font).Width
                                             );
                    e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), 0, beforeCaption + CaptionPadding, new StringFormat(StringFormatFlags.DirectionVertical));
                    break;
                }
            }

            //			e.Graphics.DrawLines(new Pen(Color.Red, 1),
            //				new Point[] {
            //								new Point(0, 0),
            //								new Point(this.Width-1, 0),
            //								new Point(this.Width-1, this.Height-1),
            //								new Point(0, this.Height-1),
            //								new Point(0, 0)
            //							}
            //				);
        }
Beispiel #9
0
        ///
        /// makeCaptcha
        ///
        public System.Drawing.Bitmap makeCaptcha()
        {
            System.Random randomGenerator = new System.Random();

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img_width, img_height
                                                                  , System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, img_width, img_height);


            System.Drawing.StringFormat sFormat = new System.Drawing.StringFormat();
            sFormat.Alignment     = System.Drawing.StringAlignment.Center;
            sFormat.LineAlignment = System.Drawing.StringAlignment.Center;

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);


            // Set up the text font.
            System.Drawing.SizeF size;
            float fontSize = img_fontsize + 1;

            System.Drawing.Font font;

            // try to use requested font, but
            // If the named font is not installed, default to a system font.
            try
            {
                font = new System.Drawing.Font(img_fontname, img_fontsize);
                font.Dispose();
            }
            catch (System.Exception ex)
            {
                img_fontname = System.Drawing.FontFamily.GenericSerif.Name;
            }


            // build a new string with space through the chars
            // i.e. keyword 'hello' become 'h e l l o '
            string tempKey = "";

            for (int ii = 0; ii < sKeyword.Length; ii++)
            {
                tempKey = string.Concat(tempKey, sKeyword[ii].ToString());
                tempKey = string.Concat(tempKey, " ");
            }

            // Adjust the font size until the text fits within the image.
            do
            {
                fontSize--;
                font = new System.Drawing.Font(img_fontname, fontSize, img_fontstyle);
                size = g.MeasureString(tempKey, font);
            } while (size.Width > (0.8 * bmp.Width));

            img_font = font;


            g.Clear(System.Drawing.Color.Silver);                               // blank the image
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // antialias objects

            // fill with a liner gradient
            // random colors
            g.FillRectangle(
                new System.Drawing.Drawing2D.LinearGradientBrush(
                    new System.Drawing.Point(0, 0),
                    new System.Drawing.Point(bmp.Width, bmp.Height),
                    System.Drawing.Color.FromArgb(
                        255,                         //randomGenerator.Next(255),
                        randomGenerator.Next(255),
                        randomGenerator.Next(255),
                        randomGenerator.Next(255)
                        ),
                    System.Drawing.Color.FromArgb(
                        randomGenerator.Next(100),
                        randomGenerator.Next(255),
                        randomGenerator.Next(255),
                        randomGenerator.Next(255)
                        )),
                rect);

            // apply swirl filter
            if (fDegree == 0)
            {
                BitmapFilter.Swirl(bmp, randomGenerator.NextDouble(), true);
            }
            else
            {
                BitmapFilter.Swirl(bmp, fDegree, true);
            }

            // draw a first line crossing the image
            //			int y1, y2;
            //			y1 = randomGenerator.Next(bmp.Height/3) + (bmp.Height/3);
            //			y2 = bmp.Height - randomGenerator.Next(bmp.Height/3);
            //
            //			g.DrawLine(new Pen( img_fontcolor, 2),
            //				new Point(0,y1), new Point(bmp.Width,y2));


            // Add some random noise.
            System.Drawing.Drawing2D.HatchBrush hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
                System.Drawing.Drawing2D.HatchStyle.LargeConfetti,
                System.Drawing.Color.LightGray,
                System.Drawing.Color.DarkGray);

            int m = System.Math.Max(rect.Width, rect.Height);

            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = randomGenerator.Next(rect.Width);
                int y = randomGenerator.Next(rect.Height);
                int w = randomGenerator.Next(m / 50);
                int h = randomGenerator.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }


            // write keyword

            // keyword positioning
            // to space equally
            int posx;
            int posy;
            int deltax;


            deltax = System.Convert.ToInt32(size.Width / tempKey.Length);
            posx   = System.Convert.ToInt32((img_width - size.Width) / 2);

            // write each keyword char
            for (int l = 0; l < tempKey.Length; l++)
            {
                posy  = ((int)(2.5 * (bmp.Height / 5))) + (((l % 2) == 0)?-2:2) * ((int)(size.Height / 3));
                posy  = (int)((bmp.Height / 2) + (size.Height / 2));
                posy += (int)((((l % 2) == 0)?-2:2) * ((size.Height / 3)));
                posx += deltax;
                g.DrawString(tempKey[l].ToString(),
                             img_font,
                             img_fontcolor,
                             posx,
                             posy,
                             sFormat);
            }


            // draw a curve
            System.Drawing.Point[] ps = new System.Drawing.Point[nPoints];

            for (int ii = 0; ii < nPoints; ii++)
            {
                int x, y;
                x      = randomGenerator.Next(bmp.Width);
                y      = randomGenerator.Next(bmp.Height);
                ps[ii] = new System.Drawing.Point(x, y);
            }
            g.DrawCurve(new System.Drawing.Pen(img_fontcolor, 2), ps
                        , System.Convert.ToSingle(randomGenerator.NextDouble()));

            // apply water filter
            BitmapFilter.Water(bmp, nWave, false);

            // Clean up.
            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            return(bmp);
        }
Beispiel #10
0
        /// <summary>
        /// Обрабатывает запрос на тестовую картинку с заданными размерами (Width and Height)
        /// </summary>
        public System.Web.Mvc.ActionResult GetImage(System.String w, System.String h)
        {
            // parse args
            var width = System.Int32.Parse(w);

            if (width <= 0)
            {
                throw new System.ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
            }
            var height = System.Int32.Parse(h);

            if (height <= 0)
            {
                throw new System.ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
            }
            var text   = "";
            var random = new System.Random();

            for (var i = 0; i < 6; i++)
            {
                text += random.Next(10);
            }
            // keep captcha key in session
            this.Session["CAPTCHA"] = text;
            // Create a CAPTCHA image and send it as response
            System.Drawing.Bitmap image = null;
            try
            {
                // Create a new 32-bit bitmap image.
                image = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                // Create a graphics object for drawing.
                var g = System.Drawing.Graphics.FromImage(image);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                var rect = new System.Drawing.Rectangle(0, 0, width, height);
                // Fill in the background.
                var hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
                g.FillRectangle(hatchBrush, rect);
                // Set up the text font.
                System.Drawing.SizeF size;
                var fontSize = rect.Height + 1;
                System.Drawing.Font font;
                // Adjust the font size until the text fits within the image.
                do
                {
                    fontSize--;
                    font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif.Name, fontSize, System.Drawing.FontStyle.Bold);
                    size = g.MeasureString(text, font);
                } while (size.Width > rect.Width);
                // Set up the text format.
                var format = new System.Drawing.StringFormat();
                format.Alignment     = System.Drawing.StringAlignment.Center;
                format.LineAlignment = System.Drawing.StringAlignment.Center;
                // Create a path using the text and warp it randomly.
                var path = new System.Drawing.Drawing2D.GraphicsPath();
                path.AddString(text, font.FontFamily, (System.Int32)font.Style, font.Size, rect, format);
                var v      = 4F;
                var points = new System.Drawing.PointF[]
                {
                    new System.Drawing.PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v),
                    new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v),
                    new System.Drawing.PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v),
                    new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v)
                };
                var matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.Translate(0F, 0F);
                path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);
                // Draw the text.
                hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LargeConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.DarkGray);
                g.FillPath(hatchBrush, path);
                // Add some random noise.
                int m = System.Math.Max(rect.Width, rect.Height);
                for (var i = 0; i < (System.Int32)(rect.Width * rect.Height / 30F); i++)
                {
                    g.FillEllipse(hatchBrush, random.Next(rect.Width), random.Next(rect.Height), random.Next(m / 50), random.Next(m / 50));
                }
                // Clean up.
                font.Dispose();
                hatchBrush.Dispose();
                g.Dispose();
                // send image response
                var converter = new System.Drawing.ImageConverter();
                return(this.File((System.Byte[])converter.ConvertTo(image, typeof(System.Byte[])), "image/jpeg"));
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
Beispiel #11
0
        //        protected override CreateParams CreateParams
        //        {
        //            get
        //            {
        //                CreateParams cp = base.CreateParams;
        //                cp.ExStyle |= 0x20;
        //                return cp;
        //            }
        //        }
        //
        //        protected override void OnMove(EventArgs e)
        //        {
        //            RecreateHandle();
        //        }
        //
        //        protected override void OnPaintBackground(PaintEventArgs e)
        //        {
        //            // do nothing
        //        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);

            int ym;
            switch (LineVerticalAlign)
            {
                case LineVerticalAlign.Top:
                    ym = 0;
                    break;
                case LineVerticalAlign.Middle:
                    ym = Convert.ToInt32(Math.Ceiling((decimal)Size.Height / 2)) - 1;
                    break;
                case LineVerticalAlign.Bottom:
                    ym = Size.Height - 2;
                    break;
                default:
                    ym = 0;
                    break;
            }

            SizeF captionSizeF = e.Graphics.MeasureString(Caption, this.Font, this.Width - CaptionMarginSpace * 2, StringFormat.GenericDefault);
            int captionLength = Convert.ToInt32(captionSizeF.Width);

            int beforeCaption;
            int afterCaption;

            if (Caption == "")
            {
                beforeCaption = CaptionMarginSpace;
                afterCaption = CaptionMarginSpace;
            }
            else
            {
                switch (CaptionOrizontalAlign)
                {
                    case CaptionOrizontalAlign.Left:
                        beforeCaption = CaptionMarginSpace;
                        afterCaption = CaptionMarginSpace + CaptionPadding * 2 + captionLength;
                        break;
                    case CaptionOrizontalAlign.Center:
                        beforeCaption = (Width - captionLength) / 2 - CaptionPadding;
                        afterCaption = (Width - captionLength) / 2 + captionLength + CaptionPadding;
                        break;
                    case CaptionOrizontalAlign.Right:
                        beforeCaption = Width - CaptionMarginSpace * 2 - captionLength;
                        afterCaption = Width - CaptionMarginSpace;
                        break;
                    default:
                        beforeCaption = CaptionMarginSpace;
                        afterCaption = CaptionMarginSpace;
                        break;
                }
            }

            // Lines
            if (_LineOrientation == LineOrientation.Horizontal)
            {
                // -------
                // |      ...caption...
                e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
                    new Point[] {
                                new Point(0, ym + 1),
                                new Point(0, ym),
                                new Point(beforeCaption, ym)
                            }
                    );

                //                  -------
                //	      ...caption...
                e.Graphics.DrawLines(new Pen(SystemColors.ControlDark, 1),
                    new Point[] {
                                new Point(afterCaption, ym),
                                new Point(this.Width, ym)
                            }
                    );

                //        ...caption...
                // -------
                e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
                    new Point[] {
                                new Point(0, ym + 1),
                                new Point(beforeCaption, ym + 1)
                            }
                    );

                //        ...caption...       |
                //                  -------
                e.Graphics.DrawLines(new Pen(SystemColors.ControlLightLight, 1),
                    new Point[] {
                                new Point(afterCaption, ym + 1),
                                new Point(this.Width, ym + 1),
                                new Point(this.Width, ym)
                            }
                    );
            }
            else if (_LineOrientation == LineOrientation.Vertical)
            {
                System.Drawing.Drawing2D.HatchBrush aHatchBrush = new
                    System.Drawing.Drawing2D.HatchBrush
                    (System.Drawing.Drawing2D.HatchStyle.Vertical,
                    SystemColors.ControlLightLight,
                    SystemColors.ControlDark);

                Pen myPen = new Pen(aHatchBrush, 2);
                e.Graphics.DrawLine(myPen, beforeCaption / 2, 0, beforeCaption / 2, ClientRectangle.Height);
                myPen.Dispose(); aHatchBrush.Dispose();
            }

            // Render caption
            if (Caption != "")
            {
                switch (_LineOrientation)
                {
                    case LineOrientation.Horizontal:
                        e.Graphics.FillRectangle(new SolidBrush(BackColor),
                            beforeCaption + CaptionPadding, 1,
                            e.Graphics.MeasureString(Caption, this.Font).Width,
                            e.Graphics.MeasureString(Caption, this.Font).Height
                            );
                        e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), beforeCaption + CaptionPadding, 1);
                        break;
                    case LineOrientation.Vertical:
                        e.Graphics.FillRectangle(new SolidBrush(BackColor),
                            0, beforeCaption + CaptionPadding,
                            e.Graphics.MeasureString(Caption, this.Font).Height,
                            e.Graphics.MeasureString(Caption, this.Font).Width
                            );
                        e.Graphics.DrawString(Caption, this.Font, new SolidBrush(this.ForeColor), 0, beforeCaption + CaptionPadding, new StringFormat(StringFormatFlags.DirectionVertical));
                        break;
                }
            }

            //			e.Graphics.DrawLines(new Pen(Color.Red, 1),
            //				new Point[] {
            //								new Point(0, 0),
            //								new Point(this.Width-1, 0),
            //								new Point(this.Width-1, this.Height-1),
            //								new Point(0, this.Height-1),
            //								new Point(0, 0)
            //							}
            //				);
        }
Beispiel #12
0
 internal void DrawPlusMinusLines(Graphics g)
 {
     if(!IsInATreeListView) return;
     if(TreeListView._updating) return;
     Debug.Assert(!TreeListView.InvokeRequired);
     if(!TreeListView.ShowPlusMinus || TreeListView.Columns.Count == 0) return;
     int itemLevel = Level;
     Rectangle plusminusRect = GetBounds(TreeListViewItemBoundsPortion.PlusMinus);
     Rectangle entireRect = GetBounds(TreeListViewItemBoundsPortion.Entire);
     System.Drawing.Drawing2D.HatchBrush hb = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50, TreeListView.PlusMinusLineColor, BackColor);
     Pen pen = new Pen(hb);
     Point point1, point2;
     #region Vertical line
     point1 = new Point(
         plusminusRect.Right - SystemInformation.SmallIconSize.Width / 2 - 1,
         entireRect.Top);
     point2 = new Point( point1.X, entireRect.Bottom);
     // If ListView has no items that have the same level before this item
     if(!HasLevelBeforeItem(itemLevel)) point1.Y += SystemInformation.SmallIconSize.Height / 2;
     // If ListView has no items that have the same level after this item
     if(!HasLevelAfterItem(itemLevel)) point2.Y -= SystemInformation.SmallIconSize.Height / 2 + 1;
     if(TreeListView.Columns[0].Width > (Level + 1) * SystemInformation.SmallIconSize.Width)
         g.DrawLine(pen, point1, point2);
     #endregion
     #region Horizontal line
     point1 = new Point(
         plusminusRect.Right - SystemInformation.SmallIconSize.Width / 2 - 1,
         GetBounds(TreeListViewItemBoundsPortion.Entire).Top + SystemInformation.SmallIconSize.Height /2);
     point2 = new Point(plusminusRect.Right + 1, point1.Y);
     if(TreeListView.Columns[0].Width > (Level + 1) * SystemInformation.SmallIconSize.Width)
         g.DrawLine(pen, point1, point2);
     #endregion
     #region Lower Level lines
     for(int level = Level - 1; level > -1; level--)
         if(HasLevelAfterItem(level))
         {
             point1 = new Point(
                 SystemInformation.SmallIconSize.Width * (2*level + 1) / 2 + entireRect.X,
                 entireRect.Top);
             point2 = new Point(
                 point1.X, entireRect.Bottom);
             if(TreeListView.Columns[0].Width > (level + 1) * SystemInformation.SmallIconSize.Width)
                 g.DrawLine(pen, point1, point2);
         }
     #endregion
     pen.Dispose();
     hb.Dispose();
 }
Beispiel #13
0
        protected void GenerateImage()
        {
            Random random = new Random();
            // Create a new 32-bit bitmap image.
            Bitmap bitmap = new Bitmap(230,37,PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle rect = new Rectangle(0, 0, 230, 37);

            // Fill in the background.
            System.Drawing.Drawing2D.HatchBrush hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent50, Color.LightGray, Color.White);
            //SolidBrush hatchBrush = new SolidBrush(Color.Red);

            String strText =(String)Session["ImageSID"];
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.
            SizeF size;
            float fontSize = rect.Height -12;
            // Adjust the font size until the text fits within the image.
             Font font = new Font("Century Schoolbook",fontSize, FontStyle.Bold);
                size = g.MeasureString(strText, font);
            // Set up the text format.
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Create a path using the text and warp it randomly.
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(
              strText,
              font.FontFamily,
              (int)font.Style,
              font.Size, rect,
              format);
            float v = 12F;
            PointF[] points =
              {
            new PointF(
              random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              random.Next(rect.Height) / v),
            new PointF(
              random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v),
            new PointF(
              rect.Width - random.Next(rect.Width) / v,
              rect.Height - random.Next(rect.Height) / v)
              };
            //points={0,0,0,0};
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);

            // Draw the text.
            hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
              System.Drawing.Drawing2D.HatchStyle.Percent50,
              Color.LightSlateGray,
              Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.
            int m = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = random.Next(rect.Width);
                int y = random.Next(rect.Height);
                int w = random.Next(m / 50);
                int h = random.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.
            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            // Set the image.
            this.Response.Clear();
            this.Response.ContentType = "image/jpeg";

            // Write the image to the response stream in JPEG format.
            bitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);

            // Dispose of the CAPTCHA image object.
            bitmap.Dispose();
        }
Beispiel #14
0
        // ====================================================================
        // Creates the bitmap image.
        // ====================================================================
        private void GenerateImage()
        {
            // Create a new 32-bit bitmap image.
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(this.width, this.height
                                                                     , System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.width, this.height);

            // Fill in the background.
            System.Drawing.Drawing2D.HatchBrush hatchBrush =
                new System.Drawing.Drawing2D.HatchBrush(
                    System.Drawing.Drawing2D.HatchStyle.SmallConfetti
                    , System.Drawing.Color.LightGray
                    , System.Drawing.Color.White);
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.
            System.Drawing.SizeF size;
            float fontSize = rect.Height + 1;

            System.Drawing.Font font;
            // Adjust the font size until the text fits within the image.
            do
            {
                fontSize--;
                font = new System.Drawing.Font(this.familyName, fontSize, System.Drawing.FontStyle.Bold);
                size = g.MeasureString(this.text, font);
            } while (size.Width > rect.Width);

            // Set up the text format.
            System.Drawing.StringFormat format = new System.Drawing.StringFormat();
            format.Alignment     = System.Drawing.StringAlignment.Center;
            format.LineAlignment = System.Drawing.StringAlignment.Center;

            // Create a path using the text and warp it randomly.
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
            float v = 4F;

            System.Drawing.PointF[] points =
            {
                new System.Drawing.PointF(this.random.Next(rect.Width) / v,              this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(this.random.Next(rect.Width) / v,              rect.Height - this.random.Next(rect.Height) / v),
                new System.Drawing.PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
            };
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0F);

            // Draw the text.
            hatchBrush = new System.Drawing.Drawing2D.HatchBrush(
                System.Drawing.Drawing2D.HatchStyle.LargeConfetti
                , System.Drawing.Color.LightGray
                , System.Drawing.Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.
            int m = System.Math.Max(rect.Width, rect.Height);

            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = this.random.Next(rect.Width);
                int y = this.random.Next(rect.Height);
                int w = this.random.Next(m / 50);
                int h = this.random.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.
            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            // Set the image.
            this.image = bitmap;
        }
Beispiel #15
0
        private byte[] GenerateCaptchaImage(string text, int width, int height)
        {
            Random random = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));

            System.Drawing.Font font;
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height);
            System.Drawing.Drawing2D.HatchBrush brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallConfetti, System.Drawing.Color.LightGray, System.Drawing.Color.White);
            graphics.FillRectangle(brush, rect);

            float emSize = rect.Height + 1;

            do {
                emSize--;
                font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, emSize, System.Drawing.FontStyle.Bold);
            } while (graphics.MeasureString(text, font).Width > rect.Width);

            System.Drawing.StringFormat format = new System.Drawing.StringFormat {
                Alignment = System.Drawing.StringAlignment.Center,
                LineAlignment = System.Drawing.StringAlignment.Center
            };

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddString(text, font.FontFamily, (int)font.Style, 75f, rect, format);
            float num2 = 4f;
            System.Drawing.PointF[] destPoints = new System.Drawing.PointF[] { new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), ((float)random.Next(rect.Height)) / num2), new System.Drawing.PointF(((float)random.Next(rect.Width)) / num2, rect.Height - (((float)random.Next(rect.Height)) / num2)), new System.Drawing.PointF(rect.Width - (((float)random.Next(rect.Width)) / num2), rect.Height - (((float)random.Next(rect.Height)) / num2)) };
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.Translate(0f, 0f);
            path.Warp(destPoints, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f);
            brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Percent10, System.Drawing.Color.Black, System.Drawing.Color.SkyBlue);
            graphics.FillPath(brush, path);

            int num3 = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < ((int)(((float)(rect.Width * rect.Height)) / 30f)); i++) {
                int x = random.Next(rect.Width);
                int y = random.Next(rect.Height);
                int w = random.Next(num3 / 50);
                int h = random.Next(num3 / 50);
                graphics.FillEllipse(brush, x, y, w, h);
            }
            font.Dispose();
            brush.Dispose();
            graphics.Dispose();

            System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
            bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] imageContent = new Byte[imageStream.Length];
            imageStream.Position = 0;
            imageStream.Read(imageContent, 0, (int)imageStream.Length);
            return imageContent;
        }