Click to See Complete Forum and Search --> : cropping and stretching


aamir55
September 7th, 2008, 10:59 PM
i want to crop the picture but when i draw a rectangle on the picture and releases the mouse,picture is croped but picture is stretched
and picture quality becomes bad.
i m worry due this problem...i can't understand it
i m using a picture box and a panel
i m attaching the code along database files...bcoz picture are stored in database

Private Sub pb_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb.MouseDown
Try
If e.Button = Windows.Forms.MouseButtons.Left Then
cropX = e.X
cropY = e.Y
cropPen = New Pen(cropPenColor, cropPenSize)
cropPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Cursor = Cursors.Cross

End If
pb.Refresh()
Catch ex As Exception

End Try
End Sub

Private Sub pb_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb.MouseMove
Try
If pb.Image Is Nothing Then Exit Sub
If e.Button = Windows.Forms.MouseButtons.Left Then
pb.Refresh()
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
pb.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)

End If
Catch ex As Exception

End Try
End Sub

Private Sub pb_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb.MouseUp
Try
Cursor = Cursors.Default
Try
If cropWidth < 1 Then
Exit Sub

End If
Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)
Dim bit As Bitmap = New Bitmap(pb.Image, pb.Width, pb.Height)
cropBitmap = New Bitmap(cropWidth, cropHeight)
Dim g As Graphics = Graphics.FromImage(cropBitmap)

g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
pb.Image = cropBitmap


Catch ex As Exception
End Try
Catch ex As Exception

End Try
End Sub

kindly help me........thanks

aamir55
September 7th, 2008, 11:05 PM
i m building an application in which i have to resize and crop the picture
i have one picture box on the form
but i have two tasks
1.resize through mouse events
2.crop through mouse events
but problem is that i have to use picture box mouse events like mouse move,mouse down and mouse up
for both functions,if i mix both functions in same mouse event then few problems come
i want to seperate these two tasks
if i want to resize the picture then i can only move and resize the picture
if i want to crop the picture then i can only crop the picture
if i have two buttons on the tool bar,one for resize and other for crop
if i just press crop button then only croping should be performed and if i click resize button then i can resize the picture

Private Enum Actions
actNone = 0
actSizePic = 1
actMovePic = 2
actCropPic = 4
End Enum

Private Const DefaultAction As Actions = Actions.actMovePic
Private Const EdgeSize As Integer = 6

Private CurrentAction As Actions = Actions.actNone
Private NextAction As Actions = DefaultAction
Private ActionStart As System.Drawing.Point

Private nEdge As Boolean, sEdge As Boolean, wEdge As Boolean, eEdge As Boolean


Private CropPen As New System.Drawing.Pen(Color.Gray, 2)

Private Sub picMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
ActionStart = e.Location
CurrentAction = NextAction
NextAction = DefaultAction
picMain_MouseMove(sender, e)
End If
End Sub

Private Sub picMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseMove
Select Case CurrentAction
Case Actions.actNone
If NextAction = Actions.actCropPic Then
picMain.Cursor = Cursors.Cross
Else
nEdge = (e.Location.Y < EdgeSize And e.Location.Y > 0)
eEdge = (e.Location.X > picMain.Size.Width - EdgeSize And e.Location.X < picMain.Size.Width)
wEdge = (e.Location.X < EdgeSize And e.Location.X > 0)
sEdge = (e.Location.Y > picMain.Size.Height - EdgeSize And e.Location.Y < picMain.Size.Height)

If (nEdge Or sEdge) Then
If (wEdge Or eEdge) Then
If (nEdge And eEdge) Or (sEdge And wEdge) Then
picMain.Cursor = Cursors.SizeNESW
ElseIf (nEdge And wEdge) Or (sEdge And eEdge) Then
picMain.Cursor = Cursors.SizeNWSE
End If
Else
picMain.Cursor = Cursors.SizeNS
End If
ElseIf (wEdge Or eEdge) Then
picMain.Cursor = Cursors.SizeWE
Else
picMain.Cursor = Cursors.Default
End If

If eEdge Or wEdge Or nEdge Or sEdge Then
NextAction = Actions.actSizePic
Else
NextAction = Actions.actMovePic
End If
End If
Case Actions.actMovePic
picMain.Top += (e.Location.Y - ActionStart.Y)
picMain.Left += (e.Location.X - ActionStart.X)

Case Actions.actSizePic
Dim Size As System.Drawing.Size = picMain.Size
Dim Location As System.Drawing.Point = picMain.Location

If eEdge Then Size.Width = e.Location.X
If sEdge Then Size.Height = e.Location.Y
If wEdge Then
Location.X -= (ActionStart.X - e.Location.X)
Size.Width += (ActionStart.X - e.Location.X)
End If
If nEdge Then
Location.Y -= (ActionStart.Y - e.Location.Y)
Size.Height += (ActionStart.Y - e.Location.Y)
End If

If Size.Height < 0 Then
Location.Y += Size.Height
Size.Height = Math.Abs(Size.Height)
nEdge = Not nEdge
sEdge = Not sEdge
ActionStart.Y = 0
End If
If Size.Width < 0 Then
Location.X = Location.X + Size.Width
Size.Width = Math.Abs(Size.Width)
eEdge = Not eEdge
wEdge = Not wEdge
ActionStart.X = 0
End If

picMain.Location = Location
picMain.Size = Size

Case Actions.actCropPic
If Not (picMain.Image Is Nothing) Then
Dim g As System.Drawing.Graphics = picMain.CreateGraphics
picMain.Refresh()
g.DrawRectangle(CropPen, ActionStart.X, ActionStart.Y, e.X - ActionStart.X, e.Y - ActionStart.Y)
g.Dispose()
g = Nothing
End If
End Select
End Sub
Private Sub picMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseUp
Select Case CurrentAction
Case Actions.actMovePic
'Placeholder
Case Actions.actSizePic
'placeholder
Case Actions.actCropPic
If (ActionStart.X <> e.X) And (ActionStart.Y <> e.Y) Then
Dim Size As System.Drawing.Size = New System.Drawing.Size(e.X - ActionStart.X, e.Y - ActionStart.Y)
Dim Rect As Rectangle = New Rectangle(ActionStart, Size)
Dim mBitmap As Bitmap = New Bitmap(picMain.Image, picMain.Width, picMain.Height)

picMain.Image = mBitmap.Clone(Rect, Imaging.PixelFormat.DontCare)
End If
mnuCrop1.Checked = False
End Select
Cursor = Cursors.Default
CurrentAction = Actions.actNone
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
If ToolStripButton1.Checked Then 'Check on click is on!
NextAction = Actions.actCropPic
Else
NextAction = DefaultAction
End If
End Sub


i m also attaching the code file
can any body help me......thanks

dglienna
September 8th, 2008, 12:44 AM
set a flag, and then duplicate the code (to an extent) in the mousedown event, to do one thing for crop and the other when not set.

HanneSThEGreaT
September 8th, 2008, 12:48 AM
Have a look at this article, it shows how to crop, zoom, resize etc. Based on a selection :
http://www.codeguru.com/vb/gen/vb_graphics/gdi/article.php/c14007/

I hope it helps! :)

aamir55
September 8th, 2008, 10:51 PM
i didn't understand bcoz i m beginner
i don't know how to two actions but one at a time
default operaton should be moving picture only.
if resize button is active then i can resize it
when crop button is active then i can crop it
and i have tried it
when i select crop button then i can only crop the picture
if this button is pressed the default action is performed which is moving and resizing.

but when i crop the picture then my program shows strange output.
when i crop my picture then it crops only image inside the picturebox but picture box remains same
and croped picture goes top left corner of the picture box
i m also uploading the code file where u can see it and then tell me what is the problem.
here i can't explain all detail....thanks