Here I Have 3 small Programs , to show you some uses I give to clipping and graphics path.
SetClip(Binoculars):

This small program will draw on the form a graphics path consisting of 2 circles(ellipses) that will emulate the movement of a pair of binoculars scanning horizontally and vertically the landscape. The image is on a picturebox that is below the border of the form, but that also could be set to invisible.
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim Tooltip1 As New ToolTip
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Dim G As Graphics = Me.CreateGraphics
Dim R As New Rectangle(30.0F, 0.0F, 200.0F, 200.0F)
Dim R1 As New Rectangle(230.0F, 0.0F, 200.0F, 200.0F)
Dim path As New GraphicsPath()
Dim L, T As Integer
'Set View Area Limits according to image size
L = 5 * e.X - 2400
If e.X < 0 Then L = -2400
If e.X > Me.ClientRectangle.Width Then L = -2400
T = -(2 * e.Y) - 30
If e.Y > Me.ClientRectangle.Height Then T = -400
If e.Y < Me.ClientRectangle.Top Then T = -10
'Add The lenses
path.AddEllipse(R)
path.AddEllipse(R1)
G.DrawPath(Pens.Black, path)
G.SetClip(path)
G.DrawImage(PictureBox1.Image, L, T, PictureBox1.Image.Width, PictureBox1.Image.Height)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tooltip1.SetToolTip(Me, "Move Mouse On Picture, Please")
End Sub
End Class
That's it!
The Second program LabelClip is the same that the first, except that I use scrollbars instead of the mouse to view the picture:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim ToolTip1 As New ToolTip
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll, HScrollBar1.Scroll
Dim G As Graphics = Label1.CreateGraphics
Dim R As New Rectangle(30.0F, 0.0F, 200.0F, 200.0F)
Dim R1 As New Rectangle(230.0F, 0.0F, 200.0F, 200.0F)
Dim path As New GraphicsPath()
path.AddEllipse(R)
path.AddEllipse(R1)
G.DrawPath(Pens.Black, path)
G.SetClip(path)
G.DrawImage(PictureBox1.Image, HScrollBar1.Value, -VScrollBar1.Value, PictureBox1.Image.Width, PictureBox1.Image.Height)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolTip1.SetToolTip(Me, "Scroll or Click either Bar, Please")
End Sub
End Class
Both include a tooltip, just in case...
The third one, GraphicsPath, is different, and with a little more code.

In there I left click and drag the mouse over the picture to signal an irregular path. The points of the path are saved in an array of points that is redimentioned as the mouse is dragged:
' Do nothing if we're not selecting a region.
If m_Points Is Nothing Then Exit Sub
' Save the new point.
m_MaxPoint += 1
ReDim Preserve m_Points(m_MaxPoint) 'Keep existing values
m_Points(m_MaxPoint).X = e.X
m_Points(m_MaxPoint).Y = e.Y
And Clipping highlighted:
Dim gr As Graphics = PictureBox1.CreateGraphics
gr.DrawLine(Pens.White, _
m_Points(m_MaxPoint).X, _
m_Points(m_MaxPoint).Y, _
m_Points(m_MaxPoint - 1).X, _
m_Points(m_MaxPoint - 1).Y)
When we release the mouse, a new point is added to the array, that is the initial point, and the pathe is closed with lines.
Dim XX, YY As Integer
'If CheckBox1.Checked = True Then
' Do nothing if we're not selecting a region.
If m_Points Is Nothing Then Exit Sub
' Close the region.
If (m_Points(0).X <> m_Points(m_MaxPoint).X) Or _
(m_Points(0).Y <> m_Points(m_MaxPoint).Y) _
Then
' Save the new point.
m_MaxPoint += 1
ReDim Preserve m_Points(m_MaxPoint)
m_Points(m_MaxPoint).X = m_Points(0).X
m_Points(m_MaxPoint).Y = m_Points(0).Y
End If
' Make the points into a Path.
selected_path.AddLines(m_Points)
And find the clipping containing rectangle:
Dim r As RectangleF
r = selected_path.GetBounds()
XX = r.Width
YY = r.Height
And locate the image and the clipping on the top left corner of the label.
This is done in two operations by calling the function Align twice:
Private Function Align(ByVal source As Image) As Image
Dim newBitmap As New Bitmap(source.Width, source.Height)
Dim graphic As Graphics = Graphics.FromImage(newBitmap)
graphic.DrawImage(source, 0, 0, W, H)
graphic.Dispose()
Return newBitmap
End Function
It is important to preserve and use the original image's width and heigth in orden to have the proper scale and alignment on the resulting image and clipping.
Then we eliminate the image ouside the clipping area:
gr_result.SetClip(selected_path, CombineMode.Replace)
Please take a look,
Thank you.
Attached File(s)
-
GraphicsPath.zip (543.07K)
Number of downloads: 290 -
LabelClip.zip (542.01K)
Number of downloads: 248 -
SetClip.zip (542.27K)
Number of downloads: 268





MultiQuote



|