Page 1 of 1

Clipping, GraphicsPath, Binocular Effect Rate Topic: -----

#1 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 31 October 2011 - 05:11 AM

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

Attached Image


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.

Attached Image

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)



Is This A Good Question/Topic? 1
  • +

Replies To: Clipping, GraphicsPath, Binocular Effect

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1065
  • View blog
  • Posts: 4,170
  • Joined: 02-July 08

Posted 18 November 2011 - 10:32 PM

Always a treat!
Was This Post Helpful? 0
  • +
  • -

#3 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 19 November 2011 - 05:25 AM

Thank you _HAWK_ . Good to have you as a friend.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1