Results 1 to 5 of 5

Thread: [RESOLVED] How to make an elliptical bitmap?

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Resolved [RESOLVED] How to make an elliptical bitmap?

    Hi!
    How can I "crop" out an elliptical portion of a bitmap? The code I posted here only draws the bitmap and the circle. I want to make something that will copy only what is within the circle. (Do I need to use the circle formula for this?)
    Thanks. ~NinjaNic

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim BitmapLocation As New Point(0, 0)
    4.     Dim B As New Bitmap(My.Resources.picture)
    5.  
    6.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    7.         e.Graphics.DrawImage(B, New Point(0, 0))
    8.         e.Graphics.DrawEllipse(Pens.Black, CreateRectangle)
    9.         'I need the part of the bitmap inside the circle.
    10.     End Sub
    11.  
    12.     Private Function CreateRectangle() As Rectangle
    13.         Dim R As New Rectangle
    14.         R.Location = BitmapLocation
    15.         If B.Width > B.Height Then
    16.             R.Width = B.Height
    17.             R.Height = B.Height
    18.             R.X += CInt((B.Width - B.Height) / 2)
    19.         Else
    20.             R.Width = B.Width
    21.             R.Height = B.Width
    22.             R.Y += CInt((B.Height - B.Width) / 2)
    23.         End If
    24.         Return R
    25.     End Function
    26.  
    27. End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to make an elliptical bitmap?

    You mean copy to the clipboard?
    I'm not sure how you would do that...

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: How to make an elliptical bitmap?

    You can use Color.Transparent for pixels outside of the ellipse

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How to make an elliptical bitmap?

    Quote Originally Posted by NinjaNic View Post
    Hi!
    How can I "crop" out an elliptical portion of a bitmap? The code I posted here only draws the bitmap and the circle. I want to make something that will copy only what is within the circle. (Do I need to use the circle formula for this?)
    Thanks. ~NinjaNic
    Here's how:
    Code:
    Public Class Form1
    
        Dim BitmapLocation As New Point(0, 0)
        Dim B As New Bitmap(My.Resources.picture)
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim rect as Rectangle = CreateRectangle
            Dim gp As New Drawing2D.GraphicsPath
            gp.AddEllipse(rect)
            e.Graphics.SetClip(gp)
            e.Graphics.DrawImage(B, New Point(0, 0))
            e.Graphics.ResetClip
            e.Graphics.DrawEllipse(Pens.Black, rect)
        End Sub
    
        Private Function CreateRectangle() As Rectangle
            Dim R As New Rectangle
            R.Location = BitmapLocation
            If B.Width > B.Height Then
                R.Width = B.Height
                R.Height = B.Height
                R.X += CInt((B.Width - B.Height) / 2)
            Else
                R.Width = B.Width
                R.Height = B.Width
                R.Y += CInt((B.Height - B.Width) / 2)
            End If
            Return R
        End Function
    
    End Class
    BB

  5. #5

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: How to make an elliptical bitmap?

    Thank you boops! That was exactly what I was looking for.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width