Why Filling Drawed Shape (with image) not Replace in Stretched Mode ?
I try To Fill RectangleShape with image but when I try to fill İmage than it is not fit into the Drawed Shape.
Code:
Public Sub FillRectangle(ByVal g As Graphics, ByVal brush As Brush, ByVal x As Decimal, ByVal y As Decimal, ByVal width As Decimal, ByVal height As Decimal, ByVal radius As Decimal)
Dim area As Rectangle = New Rectangle(x, y, width, height)
Dim path As New Drawing2D.GraphicsPath
'Add the corners
path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90) 'Upper-Left
path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90) 'Upper-Right
path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90) 'Lower-Right
path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90) 'Lower-Left
'Fill the rounded rectangle
g.FillPath(brush, path)
End Sub
Code:
Private Sub PrintDocument2_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument2.PrintPage
e.Graphics.PageUnit = GraphicsUnit.Millimeter
e.Graphics.ResetTransform()
PrintDocument2.OriginAtMargins = True
Dim grpX As Graphics = e.Graphics
Dim newImage As Image = Drawing.Image.FromFile("C:\Users\Yaman\Desktop\Frames\243.png")
Dim brsh As New TextureBrush(newImage)
FillRectangle(grpX, brsh, CDec(4.65), CDec(12.9), CDec(99.1), CDec(33.9), 5)
End Sub
Re: Why Filling Drawed Shape (with image) not Replace in Stretched Mode ?
A texture brush will normally tile the image to fill the area, and the orientation of the start of the tiling is the 0,0 point of the clent being drawn in. The reason for that is that if you fill various portions of a client area, and the brush "strokes" overlap, they will mesh, i.e. be seamless.
If you want to orient the brush so it starts at the x,y point of the rectangle, then you need to TranslateTransform the brush by that x,y value first.
And if you want the image that was used to create the brush to "stretch" to fill the rectangle, they you will have to ScaleTransform the brush to fill the desired area, which means you need to know the original size of the image, so you can scale the x and y to fit the destination.
Another option, would be to define a region of the appropriate rounded rectangle shape, and then just use a Scaling version of DrawImage to draw the image into that region. The drawing will be clipped so it doesn't exceed the region.
The attached example uses regions a couple of times, one to draw within the bounds of the region, and a second time to protect the region (only allow drawing outside the region).
More description of the demo can be found this post.
Last edited by passel; May 2nd, 2020 at 12:00 AM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930