/// <summary> /// Draws the image using the specified data and alignes the image using the /// specified alignments within the specified bounds. /// </summary> /// <param name="g">The graphics to draw into.</param> /// <param name="index">The index of the image to draw.</param> /// <param name="destRect">The target bounds to draw into.</param> /// <param name="srcRect">The bounds of the image part to draw</param> /// <param name="horizontal">The horizontal alignment of the part image within the specified bounds.</param> /// <param name="vertical">The vertical alignment of the part image within the specified bounds.</param> /// <returns></returns> public void Draw(Graphics g, int index, Rectangle destRect, Rectangle srcRect, DrawingAlign horizontal, DrawingAlign vertical) { if (srcRect.IsEmpty) srcRect = new Rectangle(0, 0, ImageSize.Width, ImageSize.Height); if (srcRect.IsEmpty) return; Rectangle dest = destRect; // If no stretching adjust destination bounds if (vertical != DrawingAlign.Stretch) { bool update = true; switch (vertical) { // to top case DrawingAlign.Near: dest.Height = srcRect.Height; break; // at bottom case DrawingAlign.Far: dest.Y = dest.Bottom - srcRect.Height; break; // center case DrawingAlign.Center: dest.Y += (dest.Height - srcRect.Height) / 2; dest.Height = srcRect.Height; break; default: update = false; break; } // atjust rectangle if (update) { Rectangle rt = dest; dest.Intersect(destRect); if (dest.Height > 0 && dest.Height != srcRect.Height) { srcRect.Y += dest.Y - rt.Y; srcRect.Height = Math.Min(dest.Height, srcRect.Height); } } } // Calculate real destination bounds if no stretching if (horizontal != DrawingAlign.Stretch) { bool update = true; switch (horizontal) { // Keep position adjust width case DrawingAlign.Near: dest.Width = srcRect.Width; break; // Align to the right case DrawingAlign.Far: dest.X = dest.Right - srcRect.Width; dest.Width = srcRect.Width; break; // Calculate middle case DrawingAlign.Center: dest.X += (dest.Width - srcRect.Width) / 2; dest.Width = srcRect.Width; break; default: update = false; break; } // Adjust rectangles if (update) { Rectangle rt = dest; dest.Intersect(destRect); if (dest.Width > 0 && dest.Width != srcRect.Width) { srcRect.X += dest.X - rt.X; srcRect.Width = Math.Min(dest.Width, srcRect.Width); } } } // Can Paint? if (!srcRect.IsEmpty && !dest.IsEmpty) Draw(g, index, dest, srcRect); }
/// <summary> /// Draws the image using the specified data and alignes the image using the /// specified alignments within the specified bounds. /// </summary> /// <param name="g">The graphics to draw into.</param> /// <param name="index">The index of the image to draw.</param> /// <param name="destRect">The target bounds to draw into.</param> /// <param name="srcRect">The bounds of the image part to draw</param> /// <param name="horizontal">The horizontal alignment of the part image within the specified bounds.</param> /// <param name="vertical">The vertical alignment of the part image within the specified bounds.</param> /// <returns></returns> public void Draw(Graphics g, int index, Rectangle destRect, Rectangle srcRect, DrawingAlign horizontal, DrawingAlign vertical) { if (srcRect.IsEmpty) { srcRect = new Rectangle(0, 0, ImageSize.Width, ImageSize.Height); } if (srcRect.IsEmpty) { return; } Rectangle dest = destRect; // If no stretching adjust destination bounds if (vertical != DrawingAlign.Stretch) { bool update = true; switch (vertical) { // to top case DrawingAlign.Near: dest.Height = srcRect.Height; break; // at bottom case DrawingAlign.Far: dest.Y = dest.Bottom - srcRect.Height; break; // center case DrawingAlign.Center: dest.Y += (dest.Height - srcRect.Height) / 2; dest.Height = srcRect.Height; break; default: update = false; break; } // atjust rectangle if (update) { Rectangle rt = dest; dest.Intersect(destRect); if (dest.Height > 0 && dest.Height != srcRect.Height) { srcRect.Y += dest.Y - rt.Y; srcRect.Height = Math.Min(dest.Height, srcRect.Height); } } } // Calculate real destination bounds if no stretching if (horizontal != DrawingAlign.Stretch) { bool update = true; switch (horizontal) { // Keep position adjust width case DrawingAlign.Near: dest.Width = srcRect.Width; break; // Align to the right case DrawingAlign.Far: dest.X = dest.Right - srcRect.Width; dest.Width = srcRect.Width; break; // Calculate middle case DrawingAlign.Center: dest.X += (dest.Width - srcRect.Width) / 2; dest.Width = srcRect.Width; break; default: update = false; break; } // Adjust rectangles if (update) { Rectangle rt = dest; dest.Intersect(destRect); if (dest.Width > 0 && dest.Width != srcRect.Width) { srcRect.X += dest.X - rt.X; srcRect.Width = Math.Min(dest.Width, srcRect.Width); } } } // Can Paint? if (!srcRect.IsEmpty && !dest.IsEmpty) { Draw(g, index, dest, srcRect); } }