/// <summary> /// Draw the background image of the given box in the given rectangle.<br/> /// Handle background-repeat and background-position values. /// </summary> /// <param name="g">the device to draw into</param> /// <param name="box">the box to draw its background image</param> /// <param name="imageLoadHandler">the handler that loads image to draw</param> /// <param name="rectangle">the rectangle to draw image in</param> public static void DrawBackgroundImage(Canvas g, CssBox box, ImageBinder imageBinder, RectangleF rectangle) { var image = imageBinder.Image; //temporary comment image scale code var imgSize = image.Size; //new Size(imageLoadHandler.Rectangle == Rectangle.Empty ? imageLoadHandler.Image.Width : imageLoadHandler.Rectangle.Width, // imageLoadHandler.Rectangle == Rectangle.Empty ? imageLoadHandler.Image.Height : imageLoadHandler.Rectangle.Height); // get the location by BackgroundPosition value var location = GetLocation(box.BackgroundPositionX, box.BackgroundPositionY, rectangle, imgSize); //var srcRect = imageLoadHandler.Rectangle == Rectangle.Empty // ? new Rectangle(0, 0, imgSize.Width, imgSize.Height) // : new Rectangle(imageLoadHandler.Rectangle.Left, imageLoadHandler.Rectangle.Top, imgSize.Width, imgSize.Height); var srcRect = new Rectangle(0, 0, image.Width, image.Height); // initial image destination rectangle var destRect = new Rectangle(location, imgSize); // need to clip so repeated image will be cut on rectangle var prevClip = g.CurrentClipRect; PixelFarm.Drawing.Rectangle copyRect = new PixelFarm.Drawing.Rectangle( (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height); copyRect.Intersect(prevClip); g.SetClipRect(copyRect); switch (box.BackgroundRepeat) { case CssBackgroundRepeat.NoRepeat: g.DrawImage(image, new RectangleF(location, imgSize), new RectangleF(0, 0, image.Width, image.Height)); break; case CssBackgroundRepeat.RepeatX: DrawRepeatX(g, image, rectangle, srcRect, destRect, imgSize); break; case CssBackgroundRepeat.RepeatY: DrawRepeatY(g, image, rectangle, srcRect, destRect, imgSize); break; default: DrawRepeat(g, image, rectangle, srcRect, destRect, imgSize); break; } g.SetClipRect(prevClip); }