public void FillOffCanvas() { ImageSharp.Rectangle bounds = new ImageSharp.Rectangle(-100, -10, 10, 10); Mock <IBrush <Rgba32> > brush = new Mock <IBrush <Rgba32> >(); Mock <Region> region = new Mock <Region>(); region.Setup(x => x.Bounds).Returns(bounds); region.Setup(x => x.MaxIntersections).Returns(10); region.Setup(x => x.Scan(It.IsAny <float>(), It.IsAny <Span <float> >())) .Returns <float, Span <float> >((y, span) => { if (y < 5) { span[0] = -10f; span[1] = 100f; return(2); } return(0); }); GraphicsOptions options = new GraphicsOptions(true) { }; FillRegionProcessor <Rgba32> processor = new FillRegionProcessor <Rgba32>(brush.Object, region.Object, options); Image <Rgba32> img = new Image <Rgba32>(10, 10); processor.Apply(img, bounds); }
public void ConvertRectangle(float x, float y, float width, float height, int expectedX, int expectedY, int expectedWidth, int expectedHeight) { SixLabors.Shapes.Rectangle src = new SixLabors.Shapes.Rectangle(x, y, width, height); ImageSharp.Rectangle actual = src.Convert(); Assert.Equal(expectedX, actual.X); Assert.Equal(expectedY, actual.Y); Assert.Equal(expectedWidth, actual.Width); Assert.Equal(expectedHeight, actual.Height); }
[InlineData(false, 16, 4)] // we always do 4 sub=pixels when antialising is off. public void MinimumAntialiasSubpixelDepth(bool antialias, int antialiasSubpixelDepth, int expectedAntialiasSubpixelDepth) { ImageSharp.Rectangle bounds = new ImageSharp.Rectangle(0, 0, 1, 1); Mock <IBrush <Rgba32> > brush = new Mock <IBrush <Rgba32> >(); Mock <Region> region = new Mock <Region>(); region.Setup(x => x.Bounds).Returns(bounds); GraphicsOptions options = new GraphicsOptions(antialias) { AntialiasSubpixelDepth = 1 }; FillRegionProcessor <Rgba32> processor = new FillRegionProcessor <Rgba32>(brush.Object, region.Object, options); Image <Rgba32> img = new Image <Rgba32>(1, 1); processor.Apply(img, bounds); region.Verify(x => x.Scan(It.IsAny <float>(), It.IsAny <Span <float> >()), Times.Exactly(4)); }
public FileStream IdentifyingCode([FromQuery] int numbers = 4) { var tempTTFName = $"{DateTime.Now.Ticks}.ttf"; System.IO.File.Copy("hyqh.ttf", tempTTFName); var tempCodeName = $"{DateTime.Now.Ticks}.jpg"; var code = RndNum(numbers); Random random = new Random(); //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //创建画布 var imageWidth = code.Length * 18; using (Image image = new Image(imageWidth, 32)) using (var pixels = image.Lock()) using (FileStream output = System.IO.File.OpenWrite(tempCodeName)) { //背景设为白色 pixels[imageWidth, 32] = Color.White; //向画板中绘制贝塞尔样条 for (int i = 0; i < 2; i++) { var p1 = new Vector2(0, random.Next(image.Height)); var p2 = new Vector2(random.Next(image.Width), random.Next(image.Height)); var p3 = new Vector2(random.Next(image.Width), random.Next(image.Height)); var p4 = new Vector2(image.Width, random.Next(image.Height)); Vector2[] p = { p1, p2, p3, p4 }; Color clr = color[random.Next(color.Length)]; Pen pen = new Pen(clr, 1); image.DrawBeziers(pen, p); } //画噪点 for (int i = 0; i < 50; i++) { GraphicsOptions noneDefault = new GraphicsOptions(); ImageSharp.Rectangle rectangle = new ImageSharp.Rectangle(random.Next(image.Width), random.Next(image.Height), 1, 1); image.Draw(Color.Gray, 1f, rectangle, noneDefault); } //画验证码字符串 for (int i = 0; i < code.Length; i++) { int cindex = random.Next(7); //随机颜色索引值 int findex = random.Next(5); //随机字体索引值 var fontCollection = new FontCollection(); var fontTemple = fontCollection.Install(tempTTFName); var font = new Font(fontTemple, 16); var brush = new SolidBrush(color[cindex]);//颜色 //var textColor = color[cindex];//颜色 int ii = 4; if ((i + 1) % 2 == 0)//控制验证码不在同一高度 { ii = 2; } image.DrawText(code.Substring(i, 1), font, brush, new System.Numerics.Vector2(3 + (i * 12), ii));//绘制一个验证字符 } image.Save(output); } if (System.IO.File.Exists(tempTTFName)) { System.IO.File.Delete(tempTTFName); } return(System.IO.File.OpenRead(tempCodeName)); }
/// <summary> /// Dithers the image reducing it to two colors using error diffusion. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="source">The image this method extends.</param> /// <param name="diffuser">The diffusion algorithm to apply.</param> /// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param> /// <param name="rectangle"> /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter. /// </param> /// <returns>The <see cref="Image"/>.</returns> public static Image<TPixel> Dither<TPixel>(this Image<TPixel> source, IErrorDiffuser diffuser, float threshold, Rectangle rectangle) where TPixel : struct, IPixel<TPixel> { source.ApplyProcessor(new ErrorDiffusionDitherProcessor<TPixel>(diffuser, threshold), rectangle); return source; }
/// <summary> /// Dithers the image reducing it to two colors using ordered dithering. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="source">The image this method extends.</param> /// <param name="dither">The ordered ditherer.</param> /// <param name="rectangle"> /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter. /// </param> /// <param name="index">The component index to test the threshold against. Must range from 0 to 3.</param> /// <returns>The <see cref="Image"/>.</returns> public static Image<TPixel> Dither<TPixel>(this Image<TPixel> source, IOrderedDither dither, Rectangle rectangle, int index = 0) where TPixel : struct, IPixel<TPixel> { source.ApplyProcessor(new OrderedDitherProcessor<TPixel>(dither, index), rectangle); return source; }