예제 #1
0
        /// <summary>
        /// 创建填充背景使用的画刷对象
        /// </summary>
        /// <param name="left">要绘制背景区域的左端坐标</param>
        /// <param name="top">要绘制背景区域的顶端坐标</param>
        /// <param name="width">要绘制背景区域的宽度</param>
        /// <param name="height">要绘制背景区域的高度</param>
        /// <param name="unit">绘制图形使用的单位</param>
        /// <returns>创建的画刷对象</returns>
        /// <remarks>
        /// 若设置了背景图片则创建图片样式的画刷对象,若设置了图案则创建带图案的画刷对象,
        /// 若设置了渐变设置则创建带渐变的画刷对象,否则创建纯色画刷对象。
        /// </remarks>
        public Brush CreateBrush(
            float left,
            float top,
            float width,
            float height,
            System.Drawing.GraphicsUnit unit)
        {
            if (intStyle == XBrushStyleConst.Disabled)
            {
                return(null);
            }

            if (myImage != null && myImage.Value != null)
            {
                System.Drawing.TextureBrush brush = new TextureBrush(myImage.Value);
                if (bolRepeat)
                {
                    brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
                }
                else
                {
                    brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Clamp;
                }
                float rate = (float)GraphicsUnitConvert.GetRate(
                    unit,
                    System.Drawing.GraphicsUnit.Pixel);
                //brush.Transform.Translate(fOffsetX, fOffsetY);
                brush.TranslateTransform(fOffsetX, fOffsetY);
                brush.ScaleTransform(rate, rate);
                return(brush);
            }
            if (intStyle == XBrushStyleConst.Solid)
            {
                return(new SolidBrush(intColor));
            }
            else
            {
                if (( int )intStyle < 1000)
                {
                    System.Drawing.Drawing2D.HatchStyle style = (System.Drawing.Drawing2D.HatchStyle)intStyle;
                    return(new System.Drawing.Drawing2D.HatchBrush(
                               (System.Drawing.Drawing2D.HatchStyle)intStyle,
                               intColor,
                               intColor2));
                }
                else
                {
                    return(new System.Drawing.Drawing2D.LinearGradientBrush(
                               new RectangleF(left, top, width, height),
                               intColor,
                               intColor2,
                               (System.Drawing.Drawing2D.LinearGradientMode)(intStyle - 1000)));
                }
            }
            //return new SolidBrush(intColor);
        }
예제 #2
0
        private void ChangeColorFromColorPicker(Control colorPicker, EventArgs e)
        {
            if (colorPicker is SolidColorPicker)
            {
                SolidColorPicker solidColorPicker = (SolidColorPicker)colorPicker;
                Color            newColor         = solidColorPicker.SelectedColor;
                GeoColor         newGeoColor      = GeoColor2MediaColorConverter.ConvertBack(newColor);

                hatchColorPicker.SyncBaseColor(DrawingColorToMediaColorConverter.ConvertBack(newColor));
                gradientPicker.SyncBaseColor(DrawingColorToMediaColorConverter.ConvertBack(newColor));

                SelectedBrush = new GeoSolidBrush(newGeoColor);
            }
            else if (colorPicker is HatchPicker)
            {
                HatchPicker hatchPicker = (HatchPicker)colorPicker;
                System.Drawing.Drawing2D.HatchStyle drawingHatchStyle = hatchPicker.SelectedHatchStyle;
                GeoHatchStyle geoHatchStyle = GeoHatchStyle2DrawingHatchStyle.ConvertBack(drawingHatchStyle);

                //solidColorPicker.SyncBaseColor(hatchPicker.ForegroundColor);
                gradientPicker.SyncBaseColor(hatchPicker.ForegroundColor);

                SelectedBrush = new GeoHatchBrush(geoHatchStyle, GeoColor2DrawingColorConverter.ConvertBack(hatchPicker.ForegroundColor)
                                                  , GeoColor2DrawingColorConverter.ConvertBack(hatchColorPicker.BackgroundColor));
            }
            else if (colorPicker is TexturePicker)
            {
                PropertyChangedEventArgs args          = (PropertyChangedEventArgs)e;
                TexturePicker            texturePicker = (TexturePicker)colorPicker;
                if (args.PropertyName == "SelectedBrush")
                {
                    ImageBrush mediaBrush = texturePicker.SelectedBrush as ImageBrush;
                    if (mediaBrush != null)
                    {
                        BitmapImage imageSource = (BitmapImage)mediaBrush.ImageSource;
                        if (imageSource != null)
                        {
                            SelectedBrush = new GeoTextureBrush(new GeoImage(imageSource.UriSource.LocalPath));
                        }
                    }
                }
            }
            else if (colorPicker is DrawingLinearGradientBrushPicker)
            {
                DrawingLinearGradientBrushPicker linearPicker = (DrawingLinearGradientBrushPicker)colorPicker;
                LinearGradientBrushEntity        brushEntity  = linearPicker.SelectedBrush;

                solidColorPicker.SyncBaseColor(brushEntity.StartColor);
                hatchColorPicker.SyncBaseColor(brushEntity.StartColor);

                SelectedBrush = new GeoLinearGradientBrush(GeoColor2DrawingColorConverter.ConvertBack(brushEntity.StartColor),
                                                           GeoColor2DrawingColorConverter.ConvertBack(brushEntity.EndColor),
                                                           brushEntity.Angle);
            }
        }
        public HatchBrush(System.Drawing.Drawing2D.HatchStyle hatchstyle, Color foreColor, Color backColor)
        {
            IntPtr zero   = IntPtr.Zero;
            int    status = SafeNativeMethods.Gdip.GdipCreateHatchBrush((int)hatchstyle, foreColor.ToArgb(), backColor.ToArgb(), out zero);

            if (status != 0)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
            base.SetNativeBrushInternal(zero);
        }
예제 #4
0
            public static System.Drawing.Drawing2D.HatchStyle GetRandomHatchStyle(int hash)
            {
                //Random randomGen = new Random(hash.GetHashCode());
                //int randomhatchindex = randomGen.Next(0, 6);
                int randomhatchindex = hash > 6 ? 0 : hash;

                System.Drawing.Drawing2D.HatchStyle style = new System.Drawing.Drawing2D.HatchStyle();

                switch (randomhatchindex)
                {
                case 0:
                    style = System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal;
                    break;

                case 1:
                    style = System.Drawing.Drawing2D.HatchStyle.DashedVertical;
                    break;

                case 2:
                    style = System.Drawing.Drawing2D.HatchStyle.Cross;
                    break;

                case 3:
                    style = System.Drawing.Drawing2D.HatchStyle.DiagonalCross;
                    break;

                case 4:
                    style = System.Drawing.Drawing2D.HatchStyle.HorizontalBrick;
                    break;

                case 5:
                    style = System.Drawing.Drawing2D.HatchStyle.LightDownwardDiagonal;
                    break;

                case 6:
                    style = System.Drawing.Drawing2D.HatchStyle.LightUpwardDiagonal;
                    break;

                default:
                    break;
                }

                return(style);
            }
예제 #5
0
            public surface_store(int i_surf_id, List <point2d> i_surface_nodes, List <edge2d> i_surface_edges, int surf_count)
            {
                this._surface_id = i_surf_id; // add surface id
                surface_nodes.AddRange(i_surface_nodes);
                surface_edges.AddRange(i_surface_edges);

                List <PointF> temp_sur_pts = new List <PointF>();

                //foreach (point2d pt in this.surface_nodes)
                //{
                //    temp_sur_pts.Add(pt.get_point());
                //}

                temp_sur_pts.Add(this.surface_edges[0].start_pt.get_point()); // Add the first point of the surface edge
                foreach (edge2d ed in this.surface_edges)
                {
                    temp_sur_pts.Add(ed.end_pt.get_point()); // since all the surface edges are interconnected only store the end points
                    //surface_path.AddLine(ed.start_pt.get_point(), ed.end_pt.get_point());
                }



                // Set the path of outter surface
                System.Drawing.Drawing2D.GraphicsPath surface_path = new System.Drawing.Drawing2D.GraphicsPath();
                surface_path.StartFigure();
                surface_path.AddPolygon(temp_sur_pts.ToArray());
                surface_path.CloseFigure();

                // set region
                surface_region = new Region(surface_path);


                Color hatch_color = Form1.the_static_class.GetRandomColor(surf_count);

                System.Drawing.Drawing2D.HatchStyle hatch_style = Form1.the_static_class.GetRandomHatchStyle(surf_count);
                Color trans_color = Color.FromArgb(0, 10, 10, 10);

                tri_brush = new System.Drawing.Drawing2D.HatchBrush(hatch_style, hatch_color, trans_color);

                signed_area_chk = this.SignedPolygonArea();
                surface_area    = Math.Abs(signed_area_chk);
            }
예제 #6
0
        public HatchBrush GetHatchBrush(HatchStyle style, SolidColor foreColor, SolidColor backColor)
        {
            HatchStyleBrushInfo info = new HatchStyleBrushInfo(style, foreColor, backColor);

            lock (this.hatchBrushes)
            {
                if (hatchBrushes.TryGetValue(info, out var hb))
                {
                    return(hb);
                }
                else
                {
                    HatchBrush b = new HatchBrush(style, foreColor, backColor);
                    hatchBrushes.Add(info, b);

                    Logger.Log("resource pool", "add hatch brush, count: " + hatchBrushes.Count);
                    return(b);
                }
            }
        }
예제 #7
0
 public HatchStyleBrushInfo(HatchStyle style, SolidColor foreColor, SolidColor backgroundColor)
 {
     this.style           = style;
     this.foreColor       = foreColor;
     this.backgroundColor = backgroundColor;
 }
 public HatchBrush(System.Drawing.Drawing2D.HatchStyle hatchstyle, Color foreColor) : this(hatchstyle, foreColor, Color.FromArgb(-16777216))
 {
 }
예제 #9
0
        private void SetSelectedBrush(GeoBrush geoBrush)
        {
            if (!isTemplateApplied)
            {
                return;
            }

            if (geoBrush is GeoSolidBrush)
            {
                SelectTabItem(solidColorBrushTabItem);
                Color selectedMediaColor = GeoColor2MediaColorConverter.Convert(((GeoSolidBrush)geoBrush).Color);
                if (solidColorPicker.SelectedColor != selectedMediaColor)
                {
                    solidColorPicker.SelectedColor = selectedMediaColor;
                }
            }
            else if (geoBrush is GeoLinearGradientBrush)
            {
                SelectTabItem(gradientTabItem);
                GeoLinearGradientBrush geoGradientBrush  = (GeoLinearGradientBrush)geoBrush;
                System.Drawing.Color   drawingStartColor = GeoColor2DrawingColorConverter.Convert(geoGradientBrush.StartColor);
                System.Drawing.Color   drawingEndColor   = GeoColor2DrawingColorConverter.Convert(geoGradientBrush.EndColor);

                if (gradientPicker.SelectedBrush.StartColor != drawingStartColor ||
                    gradientPicker.SelectedBrush.EndColor != drawingEndColor ||
                    gradientPicker.SelectedBrush.Angle - geoGradientBrush.DirectionAngle > 1)
                {
                    gradientPicker.SelectedBrush = new LinearGradientBrushEntity
                    {
                        StartColor = drawingStartColor,
                        EndColor   = drawingEndColor,
                        Angle      = (int)geoGradientBrush.DirectionAngle
                    }
                }
                ;
            }
            else if (geoBrush is GeoHatchBrush)
            {
                GeoHatchBrush geoHatchBrush = (GeoHatchBrush)geoBrush;
                SelectTabItem(hatchBrushTabItem);
                System.Drawing.Drawing2D.HatchStyle drawingHatchStyle = GeoHatchStyle2DrawingHatchStyle.Convert(geoHatchBrush.HatchStyle);
                System.Drawing.Color drawingBackgroundColor           = GeoColor2DrawingColorConverter.Convert(geoHatchBrush.BackgroundColor);
                System.Drawing.Color drawingForegroundColor           = GeoColor2DrawingColorConverter.Convert(geoHatchBrush.ForegroundColor);

                if (hatchColorPicker.SelectedHatchStyle != drawingHatchStyle ||
                    drawingBackgroundColor != hatchColorPicker.BackgroundColor ||
                    drawingForegroundColor != hatchColorPicker.ForegroundColor)
                {
                    hatchColorPicker.BackgroundColor    = drawingBackgroundColor;
                    hatchColorPicker.ForegroundColor    = drawingForegroundColor;
                    hatchColorPicker.SelectedHatchStyle = drawingHatchStyle;
                }
            }
            else if (geoBrush is GeoTextureBrush)
            {
                SelectTabItem(textureTabItem);
                textureTabItem.IsSelected = true;
                GeoTextureBrush geoTextureBrush = (GeoTextureBrush)geoBrush;
                if (textureColorPicker.SelectedBrush == null || !textureColorPicker.SelectedBrush.GetValue(Canvas.TagProperty).Equals(geoTextureBrush.GeoImage.GetPathFilename()))
                {
                    textureColorPicker.SelectedBrush = GeoTextureBrushToImageBrushConverter.Convert(geoTextureBrush);
                }
            }
        }
예제 #10
0
        public static byte[] GetCaptchaImage(string sCaptchaText, System.Drawing.Imaging.ImageFormat format)
        {
            int iHeight = 80;
            int iWidth  = 190;

            System.Random oRandom = new System.Random();

            int[] aBackgroundNoiseColor = new int[] { 150, 150, 150 };
            int[] aTextColor            = new int[] { 0, 0, 0 };
            int[] aFontEmSizes          = new int[] { 15, 20, 25, 30, 35 };

            string[] aFontNames = new string[]
            {
                "Comic Sans MS",
                "Arial",
                "Times New Roman",
                "Georgia",
                "Verdana",
                "Geneva"
            };

            System.Drawing.FontStyle[] aFontStyles = new System.Drawing.FontStyle[]
            {
                System.Drawing.FontStyle.Bold,
                System.Drawing.FontStyle.Italic,
                System.Drawing.FontStyle.Regular,
                System.Drawing.FontStyle.Strikeout,
                System.Drawing.FontStyle.Underline
            };

            System.Drawing.Drawing2D.HatchStyle[] aHatchStyles = new System.Drawing.Drawing2D.HatchStyle[]
            {
                System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.Cross
                , System.Drawing.Drawing2D.HatchStyle.DashedDownwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.DashedHorizontal
                , System.Drawing.Drawing2D.HatchStyle.DashedUpwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.DashedVertical
                , System.Drawing.Drawing2D.HatchStyle.DiagonalBrick
                , System.Drawing.Drawing2D.HatchStyle.DiagonalCross
                , System.Drawing.Drawing2D.HatchStyle.Divot
                , System.Drawing.Drawing2D.HatchStyle.DottedDiamond
                , System.Drawing.Drawing2D.HatchStyle.DottedGrid
                , System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.Horizontal
                , System.Drawing.Drawing2D.HatchStyle.HorizontalBrick
                , System.Drawing.Drawing2D.HatchStyle.LargeCheckerBoard
                , System.Drawing.Drawing2D.HatchStyle.LargeConfetti
                , System.Drawing.Drawing2D.HatchStyle.LargeGrid
                , System.Drawing.Drawing2D.HatchStyle.LightDownwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.LightHorizontal
                , System.Drawing.Drawing2D.HatchStyle.LightUpwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.LightVertical
                , System.Drawing.Drawing2D.HatchStyle.Max
                , System.Drawing.Drawing2D.HatchStyle.Min
                , System.Drawing.Drawing2D.HatchStyle.NarrowHorizontal
                , System.Drawing.Drawing2D.HatchStyle.NarrowVertical
                , System.Drawing.Drawing2D.HatchStyle.OutlinedDiamond
                , System.Drawing.Drawing2D.HatchStyle.Plaid
                , System.Drawing.Drawing2D.HatchStyle.Shingle
                , System.Drawing.Drawing2D.HatchStyle.SmallCheckerBoard
                , System.Drawing.Drawing2D.HatchStyle.SmallConfetti
                , System.Drawing.Drawing2D.HatchStyle.SmallGrid
                , System.Drawing.Drawing2D.HatchStyle.SolidDiamond
                , System.Drawing.Drawing2D.HatchStyle.Sphere
                , System.Drawing.Drawing2D.HatchStyle.Trellis
                , System.Drawing.Drawing2D.HatchStyle.Vertical
                , System.Drawing.Drawing2D.HatchStyle.Wave
                , System.Drawing.Drawing2D.HatchStyle.Weave
                , System.Drawing.Drawing2D.HatchStyle.WideDownwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.WideUpwardDiagonal
                , System.Drawing.Drawing2D.HatchStyle.ZigZag
            };



            //Creates an output Bitmap
            System.Drawing.Bitmap oOutputBitmap = new System.Drawing.Bitmap(iWidth, iHeight
                                                                            , System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(oOutputBitmap);
            oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            //Create a Drawing area
            System.Drawing.RectangleF oRectangleF =
                new System.Drawing.RectangleF(0, 0, iWidth, iHeight);
            System.Drawing.Brush oBrush = default(System.Drawing.Brush);


            //Draw background (Lighter colors RGB 100 to 255)
            oBrush = new System.Drawing.Drawing2D.HatchBrush(
                aHatchStyles[oRandom.Next(aHatchStyles.Length - 1)]
                , System.Drawing.Color.FromArgb
                (
                    oRandom.Next(100, 255)
                    , oRandom.Next(100, 255)
                    , oRandom.Next(100, 255)
                )
                , System.Drawing.Color.White
                );

            oGraphics.FillRectangle(oBrush, oRectangleF);

            System.Drawing.Drawing2D.Matrix oMatrix = new System.Drawing.Drawing2D.Matrix();
            int i = 0;

            for (i = 0; i <= sCaptchaText.Length - 1; i++)
            {
                oMatrix.Reset();
                int iChars = sCaptchaText.Length;
                int x      = iWidth / (iChars + 1) * i;
                int y      = iHeight / 2;

                //Rotate text Random
                oMatrix.RotateAt(oRandom.Next(-40, 40), new System.Drawing.PointF(x, y));
                oGraphics.Transform = oMatrix;

                //Draw the letters with Random Font Type, Size and Color
                oGraphics.DrawString
                (
                    //Text
                    sCaptchaText.Substring(i, 1),
                    //Random Font Name and Style
                    new System.Drawing.Font(aFontNames[oRandom.Next(aFontNames.Length - 1)],
                                            aFontEmSizes[oRandom.Next(aFontEmSizes.Length - 1)],
                                            aFontStyles[oRandom.Next(aFontStyles.Length - 1)]),
                    //Random Color (Darker colors RGB 0 to 100)
                    new System.Drawing.SolidBrush(
                        System.Drawing.Color.FromArgb(
                            oRandom.Next(0, 100)
                            , oRandom.Next(0, 100)
                            , oRandom.Next(0, 100)))
                    , x
                    , oRandom.Next(10, 40)
                );
                oGraphics.ResetTransform();
            }

            byte[] captchaBytes = null;

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                oOutputBitmap.Save(ms, format);
                captchaBytes = ms.ToArray();
            } // End Using ms

            return(captchaBytes);
        }