问题描述
我制作了一个用户控件,可以用作圆形/方形/三角形形状,控件可以绘制边框(外部)并填充它在边框内部的内容(内部)
了解它:
边框是黑色的,内部是红色的。
好,现在我想在我的控件中添加一个功能,以使用图像填充图形的内部。
但是当我尝试仅使用图像填充该空间时,形状的外部区域也被绘制:
我不是任何使用GDI +的专家,我该如何解决这个问题?
我需要用一个圆圈,一个正方形和一个三角形来做这件事,我不知道这可能是一个停止的事情。
这是我的代码正在用图像填充圆圈:
公共属性InnerImage As Image = Image.FromFile(C:\random.jpg)
'''< summary> ;
'''在指定的< see cref =System.Drawing.Graphics/>上绘制一个圆。目的。
'''< / summary>
'''< param name =g>< see cref =System.Drawing.Graphics/>要绘制的对象。< / param>
使用笔作为新笔(Me._BorderColor,Me._BorderWidth)
Dim rect As Rectangle = Me.GetFigueRectangle(pen)
'使用图像填充圆。
如果Me.InnerImage IsNot Nothing Then
.DrawImage(Me.InnerImage,rect)
Else'使用纯色填充圆。
如果不是Me._InnerColor = Color.Transparent然后
使用画笔作为新的SolidBrush(Me._InnerColor)
.FillEllipse(brush,rect)
结束使用
End If'Not Me._InnerColor = Color.Transparent
End If
'绘制圆形边框。
.DrawEllipse(pen,rect)
结束使用笔作为新笔(Me._BorderColor,Me._BorderWidth)
结束'g
End Sub
和 GetFigueRectangle
函数(它用于Circle,Square和Triangle)是这样的:
'''< summary>
'''获取< see cref =System.Drawing.Rectangle/>边界根据指定的< see cref =System.Drawing.Pen.Width/>进行修复。
'''< / summary>
'''< param name =pen>< see cref =System.Drawing.Pen/>。< / param>
'''< returns>< see cref =System.Drawing.Rectangle/>界限是固定的。< / returns>
Private Function GetFigueRectangle(ByVal pen As Pen)As Rectangle
使用
{
.x = CInt(0.0F +(pen.Width / 2.0)返回新的矩形F)),
.y = CInt(0.0F +(pen.Width / 2.0F)),
.width = CInt(MyBase.Width - pen.Width),
.height = CInt(MyBase.Height - pen.Width)
}
End Function
Graphics.SetClip
来解决这个问题。 >设置剪辑区域。 此处更多信息:
这将允许您将绘图剪辑到任何可能的形状,你可以想到的。只有路径内的像素才会被绘制出来。
I've made an user control that can be used as a circle/square/triangle shape, the control can draw a border (outer) and fill what its inside that border (inner)
To understand it:
The border is the black, and the inner is the red.
Well, now I would like to add a feature in my control to fill the inner of the shape using an image.
but when I try to fill only that space using an image, the outside area of the shape is also drawn:
I'm not any expert using GDI+, how I can fix this?
I need to do this with a circle, an square and a triangle, I don't know if that could be an stop thing.
This is the code that I'm using to fill the circle with an image:
Public Property InnerImage As Image = Image.FromFile("C:\random.jpg")
''' <summary>
''' Draws a circle on the specified <see cref="System.Drawing.Graphics"/> object.
''' </summary>
''' <param name="g">The <see cref="System.Drawing.Graphics"/> object to draw.</param>
Private Sub DrawCircle(ByRef g As Graphics)
With g
Using pen As New Pen(Me._BorderColor, Me._BorderWidth)
Dim rect As Rectangle = Me.GetFigueRectangle(pen)
' Fill the circle using the image.
If Me.InnerImage IsNot Nothing Then
.DrawImage(Me.InnerImage, rect)
Else ' Fill the circle using a solid colour.
If Not Me._InnerColor = Color.Transparent Then
Using brush As New SolidBrush(Me._InnerColor)
.FillEllipse(brush, rect)
End Using
End If ' Not Me._InnerColor = Color.Transparent
End If
' Draw the circle border.
.DrawEllipse(pen, rect)
End Using ' pen As New Pen(Me._BorderColor, Me._BorderWidth)
End With ' g
End Sub
And GetFigueRectangle
function (which a use it for both Circle, Square and Triangle) is this:
''' <summary>
''' Gets a <see cref="System.Drawing.Rectangle"/> with the bounds fixed according to the specified <see cref="System.Drawing.Pen.Width"/>.
''' </summary>
''' <param name="pen">The <see cref="System.Drawing.Pen"/>.</param>
''' <returns>The <see cref="System.Drawing.Rectangle"/> with the bounds fixed.</returns>
Private Function GetFigueRectangle(ByVal pen As Pen) As Rectangle
Return New Rectangle With
{
.x = CInt(0.0F + (pen.Width / 2.0F)),
.y = CInt(0.0F + (pen.Width / 2.0F)),
.width = CInt(MyBase.Width - pen.Width),
.height = CInt(MyBase.Height - pen.Width)
}
End Function
You can use Graphics.SetClip
that takes a GraphicsPath
to set a clipping region.More info here: http://msdn.microsoft.com/en-us/library/0z994t06(v=vs.110).aspx
This will allow you to clip your drawing to any possible shape you can think of. Only pixels inside the path will actually be drawn,.
这篇关于将图像绘制成形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!