hi all,
i m making a window application in C# and i want crop an image in circle form from picture box with mouse event.
can any one help me ...
thanks in advance.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Manath Pathana86 reputationPosted Apr 19, 2009, 8:45 PM EDT
please write code below.
Imports System.Drawing
Public cropPenSize As Integer = 2
Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
Public cropPenColor As Color = Color.Aquamarine
Public c As Cursors
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Try
Dim openDLG As New OpenFileDialog
openDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"
If openDLG.ShowDialog = DialogResult.OK Then
p.Image = Image.FromFile(openDLG.FileName, True)
End If
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click
Try
If cropWidth < 1 Then
MessageBox.Show("You need to first select what portion of the image to crop.", " No cropping Cordinates!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim bit As Bitmap = New Bitmap(p.Image, p.Width, p.Height)
cropBitmap = New Bitmap(cropWidth, cropHeight)
Dim g As Graphics = Graphics.FromImage(cropBitmap)
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
pbCrop.Image = cropBitmap
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If pbCrop.Image Is Nothing Then
MessageBox.Show("You have not edited the original image. There is no new image to save.", " Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim saveDLG As SaveFileDialog = New SaveFileDialog
saveDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"
If saveDLG.ShowDialog = DialogResult.OK Then
If saveDLG.FileName.EndsWith("bmp") Then
pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Bmp)
ElseIf saveDLG.FileName.EndsWith("gif") Then
pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Gif)
Else
pbCrop.Image.Save(saveDLG.FileName, ImageFormat.Jpeg)
End If
saveDLG.Dispose()
End If
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub p_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseUp
Try
Cursor = c.Default
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub p_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles p.MouseMove
Try
If p.Image Is Nothing Then Exit Sub
If e.Button = MouseButtons.Left Then
p.Refresh()
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
p.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
End If
GC.Collect()
Catch exc As Exception
If Err.Number = 5 Then Exit Sub
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
Babita shivade7 reputationPosted Apr 19, 2009, 9:47 PM EDT
hi,
thank you very much...........
but i need this code in C# language can u help me....
thanks again.............
Manath Pathana86 reputationPosted Apr 23, 2009, 10:03 PM EDT
Sorry, I am not sure in C# code.
but you can go to website http://www.developerfusion.com/tools/convert/vb-to-csharp/
for convert from VB.Net code to C# code.
If you still can not do, please reply to me. I will try do it for you.
Good Luck.
Babita shivade7 reputationPosted Apr 24, 2009, 12:40 AM EDT
hi...
Thank you very much for helping me....
i want to ask one think this code is crop a image in rectangular form. do you have any idea about circle cropping. in which i can crop a image in circular form and save it as also in circular form .... if you have idea about it plz help me..
thanks again.
Manath Pathana86 reputationPosted Apr 24, 2009, 5:39 PM EDT
But if I have free time, I will search for you.
If you can do or search it, Don't forget tell me.
I want to know too.
OK?
Babita shivade7 reputationPosted Apr 24, 2009, 9:36 PM EDT
ok thanks..
if i m getting answer for this problem then i will tell you.
thanks again.
Ritesh Singh5 reputationPosted Jun 25, 2010, 2:44 AM EDT
For the time being you can follow this link.
http://forums.devx.com/showthread.php?t=160827