none
confuse image RRS feed

  • Question

  • HI

    I want to select a part of the picture with a rectangular or square frame and then confuse the selected part.

    and save edit in PictureBox

    by code

    IMAGE

    SELECT PART by mouse

    blur select



    • Edited by ahmeddc Tuesday, September 24, 2019 1:26 PM
    Monday, September 23, 2019 8:16 PM

Answers

  • Ahmed,

    Yes I noticed and already updated the example last post.

    You should learn how it works and then do the custom yourself.

    :)

    • Marked as answer by ahmeddc Tuesday, September 24, 2019 1:25 PM
    Tuesday, September 24, 2019 1:05 PM

All replies

  • You can use BlurParams structure

    Test (with GdipBitmapApplyEffect) with Radius = 14 :

    Test with Radius = 20 :



    • Edited by Castorix31 Monday, September 23, 2019 9:17 PM
    Monday, September 23, 2019 9:05 PM
  • Hi Ahmed,

    Here is a start on the mouse part of it. You can change the blur to Castors method or whatever...

    The example makes the controls just cut and paste the code to an empty form. Change the form name as required.

    Imports System.Drawing.Imaging
    
    Public Class Form2
        Private WithEvents PictureBox1 As New PictureBox With {.Parent = Me, .Dock = DockStyle.Fill, .BackColor = Color.Black}
        Private MouseDownStage As Integer
        Private MouseDownPt, MouseMovePt, MouseDownOffsetPt As PointF
        Private FenceRect As RectangleF
        Private SourceImg As Image = Image.FromFile("c:\bitmaps\jolee.jpg")
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            MouseDownStage = GetMouseOverType(e.X, e.Y)
            Select Case MouseDownStage
                Case 1      'nothing under mouse down begin drawing tracer
                    MouseDownPt = e.Location
                    MouseMovePt = e.Location
                Case 2, 3   'mouse down on rectangle or handle begine moving
                    MouseDownPt = FenceRect.Location
                    MouseDownOffsetPt.X = e.X - FenceRect.X
                    MouseDownOffsetPt.Y = e.Y - FenceRect.Y
            End Select
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            MouseMovePt = e.Location
    
            If MouseDownStage > 0 Then
                Dim dx, dy As Single
    
                Select Case MouseDownStage
                    Case 1                      'drawing rectangle 
                        FenceRect = GetFenceRect(MouseDownPt, New SizeF(MouseMovePt.X - MouseDownPt.X,
                                                                        MouseMovePt.Y - MouseDownPt.Y))
                    Case 2                      'moving fence
                        dx = e.X - MouseDownPt.X
                        dy = e.Y - MouseDownPt.Y
                        FenceRect.X = MouseDownPt.X + dx - MouseDownOffsetPt.X
                        FenceRect.Y = MouseDownPt.Y + dy - MouseDownOffsetPt.Y
                    Case 3                      'moving handle
                        FenceRect.Width = Math.Abs(FenceRect.X - e.X)
                        FenceRect.Height = Math.Abs(FenceRect.Y - e.Y)
                End Select
                PictureBox1.Invalidate()
            Else
                Select Case GetMouseOverType(e.X, e.Y)
                    Case 2  'fence
                        Cursor = Cursors.Hand
                    Case 3 'handle
                        Cursor = Cursors.SizeNESW
                    Case Else
                        Cursor = Cursors.Default
                End Select
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    
            Select Case MouseDownStage
                Case 1      'save the rectangle
                    FenceRect = GetFenceRect(MouseDownPt,
                                             New SizeF(MouseMovePt.X - MouseDownPt.X,
                                                       MouseMovePt.Y - MouseDownPt.Y))
            End Select
    
            MouseDownStage = 0
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            Dim BlurForce As Integer = 2
    
            With e.Graphics
                .DrawImage(SourceImg, 0, 0, SourceImg.Width, SourceImg.Height)
    
                'draw the saved rectangle
                If FenceRect.Width > 0 Then
    
                    Using p As New Pen(Color.White, 1),
                        br As New SolidBrush(Color.FromArgb(100, Color.AntiqueWhite)),
                        att As New ImageAttributes
    
                        'draw the blur rect
                        Dim FenceRectI As Rectangle = Rectangle.Round(FenceRect)
                        Dim mtx As New ColorMatrix
                        mtx.Matrix33 = 0.7
                        att.SetColorMatrix(mtx)
                        For x As Integer = FenceRectI.X - BlurForce To FenceRectI.X + FenceRectI.Width
                            .DrawImage(SourceImg,
                                       New Rectangle(x, FenceRectI.Y - BlurForce, BlurForce, FenceRectI.Height),
                                       FenceRectI.X, FenceRectI.Y, FenceRectI.Width, FenceRectI.Height,
                                       GraphicsUnit.Pixel, att)
                        Next
    
                        'fill and outline rect
                        '.FillRectangle(br, FenceRectI)
                        .DrawRectangle(p, FenceRectI)
    
                        'draw the handle solid green
                        Dim h As Integer = 5
                        Dim rect2 As New Rectangle(FenceRectI.X + FenceRectI.Width - h,
                                                   FenceRectI.Y + FenceRectI.Height - h, 2 * h, 2 * h)
                        p.Width = 2
                        p.Color = Color.LimeGreen
                        .DrawRectangle(p, rect2)
    
                    End Using
                End If
            End With
        End Sub
    
        Private Function GetMouseOverType(x As Integer, y As Integer) As Integer
            'determine if the mouse pointer is over the handle or fence
            Dim h As Integer = 5
            Dim handleRect As New Rectangle(CInt(FenceRect.X + FenceRect.Width - h),
                                            CInt(FenceRect.Y + FenceRect.Height - h),
                                            2 * h, 2 * h)
    
            If handleRect.Contains(x, y) Then
                GetMouseOverType = 3              'mouse over handle
            ElseIf FenceRect.Contains(x, y) Then
                GetMouseOverType = 2              'mouse over fence FenceRect
            Else
                GetMouseOverType = 1              'mouse not over anything
            End If
        End Function
    
        Private Function GetFenceRect(locPt As PointF, sz As SizeF) As RectangleF
            'creates proper FenceRect from any direction for point and size 
            Dim X As Single = locPt.X
            Dim X1 As Single = sz.Width
    
            If X1 < 0 Then
                X1 = Math.Abs(X1)
                X -= X1
            End If
    
            Dim Y As Single = locPt.Y
            Dim Y1 As Single = sz.Height
    
            If Y1 < 0 Then
                Y1 = Math.Abs(Y1)
                Y -= Y1
            End If
    
            GetFenceRect = New RectangleF(X, Y, X1, Y1)
    
        End Function
    End Class

    Tuesday, September 24, 2019 10:47 AM
  • Hi Ahmed,

    Here is a start on the mouse part of it. You can change the blur to Castors method or whatever...

    The example makes the controls just cut and paste the code to an empty form. Change the form name as required.


    Thanks Tommy  for your reply
    I have another part of my question which is
    How to save the new blur addition in the image and save the image as a whole after modificationcode

    code not work

    copy original image only

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                   PictureBox2.Image = PictureBox1.Image.Clone 'Image To Be Cloned
        End Sub
    Tuesday, September 24, 2019 11:47 AM
  • Thanks Tommy  for your reply
    I have another part of my question which is
    How to save the new blur addition in the image and save the image as a whole after modificationcode

    code not work

    copy original image only

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                   PictureBox2.Image = PictureBox1.Image.Clone 'Image To Be Cloned
        End Sub

    You should know that one by now !!!  :)

    See where I added a sub routine DrawScene to draw the image? Click the button1 and it makes a new bitmap and draws the image in the new bitmap. Then you can use the new bmp image where you want. I put it picturebox 2 and show how to save to disc.

    THe DrawScene is also used in picturebox1 paint to draw the active image.

    Edit: removed the select fence outline and handle from final image etc.

    Imports System.Drawing.Imaging
    
    Public Class Form2
        Private WithEvents PictureBox1 As New PictureBox With {.Parent = Me, .BackColor = Color.Black,
            .Location = New Point(10, 10), .Size = New Size(250, 250)}
        Private WithEvents PictureBox2 As New PictureBox With {.Parent = Me, .BackColor = Color.Black,
            .Location = New Point(10, 280), .Size = New Size(250, 250)}
    
        Private WithEvents Button1 As New Button With {.Parent = Me, .Text = "Copy",
            .Location = New Point(100, 550)}
    
        Private MouseDownStage As Integer
        Private MouseDownPt, MouseMovePt, MouseDownOffsetPt As PointF
        Private FenceRect As RectangleF
        Private SourceImg As Image = Image.FromFile("c:\bitmaps\jolee.jpg")
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 600)
            PictureBox2.BackgroundImageLayout = ImageLayout.None
    
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            MouseDownStage = GetMouseOverType(e.X, e.Y)
            Select Case MouseDownStage
                Case 1      'nothing under mouse down begin drawing tracer
                    MouseDownPt = e.Location
                    MouseMovePt = e.Location
                Case 2, 3   'mouse down on rectangle or handle begine moving
                    MouseDownPt = FenceRect.Location
                    MouseDownOffsetPt.X = e.X - FenceRect.X
                    MouseDownOffsetPt.Y = e.Y - FenceRect.Y
            End Select
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            MouseMovePt = e.Location
    
            If MouseDownStage > 0 Then
                Dim dx, dy As Single
    
                Select Case MouseDownStage
                    Case 1                      'drawing rectangle 
                        FenceRect = GetFenceRect(MouseDownPt, New SizeF(MouseMovePt.X - MouseDownPt.X,
                                                                        MouseMovePt.Y - MouseDownPt.Y))
                    Case 2                      'moving fence
                        dx = e.X - MouseDownPt.X
                        dy = e.Y - MouseDownPt.Y
                        FenceRect.X = MouseDownPt.X + dx - MouseDownOffsetPt.X
                        FenceRect.Y = MouseDownPt.Y + dy - MouseDownOffsetPt.Y
                    Case 3                      'moving handle
                        FenceRect.Width = Math.Abs(FenceRect.X - e.X)
                        FenceRect.Height = Math.Abs(FenceRect.Y - e.Y)
                End Select
                PictureBox1.Invalidate()
            Else
                Select Case GetMouseOverType(e.X, e.Y)
                    Case 2  'fence
                        Cursor = Cursors.Hand
                    Case 3 'handle
                        Cursor = Cursors.SizeNESW
                    Case Else
                        Cursor = Cursors.Default
                End Select
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    
            Select Case MouseDownStage
                Case 1      'save the rectangle
                    FenceRect = GetFenceRect(MouseDownPt,
                                             New SizeF(MouseMovePt.X - MouseDownPt.X,
                                                       MouseMovePt.Y - MouseDownPt.Y))
            End Select
    
            MouseDownStage = 0
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub DrawScene(g As Graphics)
    
            Dim BlurForce As Integer = 2
    
            With g
                .DrawImage(SourceImg, 0, 0, SourceImg.Width, SourceImg.Height)
    
                'draw the saved rectangle
                If FenceRect.Width > 0 Then
    
                    Dim att As New ImageAttributes
    
                    'draw the blur rect
                    Dim FenceRectI As Rectangle = Rectangle.Round(FenceRect)
                    Dim mtx As New ColorMatrix
                    mtx.Matrix33 = 0.7
                    att.SetColorMatrix(mtx)
                    For x As Integer = FenceRectI.X - BlurForce To FenceRectI.X + FenceRectI.Width
                        .DrawImage(SourceImg,
                                       New Rectangle(x, FenceRectI.Y - BlurForce, BlurForce, FenceRectI.Height),
                                       FenceRectI.X, FenceRectI.Y, FenceRectI.Width, FenceRectI.Height,
                                       GraphicsUnit.Pixel, att)
                    Next
    
    
                End If
            End With
    
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    
            DrawScene(e.Graphics)
    
            Dim FenceRectI As Rectangle = Rectangle.Round(FenceRect)
            Using p As New Pen(Color.White, 1),
                br As New SolidBrush(Color.FromArgb(100, Color.AntiqueWhite))
    
                e.Graphics.DrawRectangle(p, FenceRectI)
    
                'draw the handle solid green
                Dim h As Integer = 5
                Dim rect2 As New Rectangle(FenceRectI.X + FenceRectI.Width - h,
                                                   FenceRectI.Y + FenceRectI.Height - h, 2 * h, 2 * h)
                p.Width = 2
                p.Color = Color.LimeGreen
                e.Graphics.DrawRectangle(p, rect2)
            End Using
    
        End Sub
    
        Private Function GetMouseOverType(x As Integer, y As Integer) As Integer
            'determine if the mouse pointer is over the handle or fence
            Dim h As Integer = 5
            Dim handleRect As New Rectangle(CInt(FenceRect.X + FenceRect.Width - h),
                                            CInt(FenceRect.Y + FenceRect.Height - h),
                                            2 * h, 2 * h)
    
            If handleRect.Contains(x, y) Then
                GetMouseOverType = 3              'mouse over handle
            ElseIf FenceRect.Contains(x, y) Then
                GetMouseOverType = 2              'mouse over fence FenceRect
            Else
                GetMouseOverType = 1              'mouse not over anything
            End If
        End Function
    
        Private Function GetFenceRect(locPt As PointF, sz As SizeF) As RectangleF
            'creates proper FenceRect from any direction for point and size 
            Dim X As Single = locPt.X
            Dim X1 As Single = sz.Width
    
            If X1 < 0 Then
                X1 = Math.Abs(X1)
                X -= X1
            End If
    
            Dim Y As Single = locPt.Y
            Dim Y1 As Single = sz.Height
    
            If Y1 < 0 Then
                Y1 = Math.Abs(Y1)
                Y -= Y1
            End If
    
            GetFenceRect = New RectangleF(X, Y, X1, Y1)
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bmp As New Bitmap(SourceImg.Width, SourceImg.Height)
    
            Using g As Graphics = Graphics.FromImage(bmp)
    
                DrawScene(g)
    
                If PictureBox2.BackgroundImage IsNot Nothing Then PictureBox2.BackgroundImage.Dispose()
    
                'make a copy of the image and add to picturebox2 or save to disc etc.
                PictureBox2.BackgroundImage = bmp
    
                'bmp.Save("c:\myfilepath")
            End Using
    
        End Sub
    End Class


    Tuesday, September 24, 2019 12:17 PM
  • last part

    How to delete the border Whiterectangles to determine the blur
    Delete the second resize green rectangle before saving blur

    Tuesday, September 24, 2019 12:57 PM
  • Ahmed,

    Yes I noticed and already updated the example last post.

    You should learn how it works and then do the custom yourself.

    :)

    • Marked as answer by ahmeddc Tuesday, September 24, 2019 1:25 PM
    Tuesday, September 24, 2019 1:05 PM
  • Ahmed,

    Yes I noticed and already updated the example last post.

    You should learn how it works and then do the custom yourself.

    :)

    You should learn how it works and then do the custom yourself.

     I will work on it 

    thanks for the help


    Tuesday, September 24, 2019 1:25 PM