Results 1 to 4 of 4

Thread: How to remove a frame/border around a picture?

  1. #1

    Thread Starter
    Fanatic Member Mith's Avatar
    Join Date
    Jul 2017
    Location
    Thailand
    Posts
    540

    How to remove a frame/border around a picture?

    Im looking for a solution to automatically remove a frame/border around a picture:

    Black frame:
    Name:  bear_black_frame_20px_small.jpg
Views: 900
Size:  3.5 KB

    White frame:
    Name:  bear_white_frame_20px_small.jpg
Views: 834
Size:  3.5 KB

    Is there maybe a GDI function to remove such frames? I dont know the color and thickness of the frame/border.

    Info: I use GdipLoadImageFromFile to load a picture and convert them with GdipCreateHBITMAPFromBitmap.
    Last edited by Mith; Jan 29th, 2022 at 07:53 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: How to remove a frame/border around a picture?

    If the borders are of a fixed width simply use StretchBlt (or similar): https://docs.microsoft.com/en-us/win...gdi-stretchblt to copy the bordered portion of the image to a new image. If the borders are of a variable width you will need to manually scan the image line by line. This will probably be a slow and tedious process though.

    EDIT:
    I dont know the color of thickness of the frame/border.
    I overlooked that part, sorry. If you're interested I have something written in vb.net that might work. EDIT: see my last post.
    Last edited by Peter Swinkels; Jan 31st, 2022 at 03:44 AM.

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: How to remove a frame/border around a picture?

    Quote Originally Posted by Mith View Post
    I dont know the color of thickness of the frame/border.
    I assume you already know about the appropriate CutOut (Blit-, Crop-, PaintPicture-calls) -
    and that the main-problem is, to determine the Line-thickness (or if there is one at all)?

    If yes, a relative fast working approach would be, to just "walk" a few pixels from each side of the image
    (I'd start in the middle on each side, then walk towards the center - that's a bit more reliable than the corners)

    And since these are only a few "Pixel-Checks" (4 or 5 on each side) -
    you can even use the relatively slow VB6-Point(x,y) function to get the ColorValue.

    E.g. I've downloaded your Bear-Image with the black border -
    placed it in: "c:\temp\bear.bmp" and get a bordersize of 3 on each side, this way:
    Code:
    Private Sub Form_Load()
      Dim P As VB.PictureBox, i As Long
      Set P = Controls.Add("VB.PictureBox", "picAnalyze") 'let's use an invisible, Autoredraw-PicBox for analyzing
      Set P.Picture = LoadPicture("c:\temp\bear.bmp")
          P.BorderStyle = 0: P.ScaleMode = vbPixels
          P.AutoSize = True: P.AutoRedraw = True
    
      Dim UBx: UBx = P.ScaleWidth - 1
      Dim UBy: UBy = P.ScaleHeight - 1
      
      For i = 0 To 3
         Debug.Print Get4BitColor(P.Point(UBx \ 2, i)) 'walk from Top downwards (in the middle of the img-width)
         Debug.Print Get4BitColor(P.Point(UBx \ 2, UBy - i)) 'walk from Bottom upwards (in the middle of the img-width)
         Debug.Print Get4BitColor(P.Point(i, UBy \ 2)) 'walk from left to right (in the middle of the img-height)
         Debug.Print Get4BitColor(P.Point(UBx - i, UBy \ 2)) 'walk from right to left (in the middle of the img-height)
      Next
    End Sub
     
    Private Function Get4BitColor(Color) As Long
      Dim S6 As String, R4 As Byte, G4 As Byte, B4 As Byte
      S6 = Right("000000" & Hex$(Color), 6) 'ensure 6 hex-digits
      
      R4 = CByte("&H" & Mid$(S6, 5, 2)) \ 2 ^ 4
      G4 = CByte("&H" & Mid$(S6, 3, 2)) \ 2 ^ 4
      B4 = CByte("&H" & Mid$(S6, 1, 2)) \ 2 ^ 4
     
      Get4BitColor = RGB(R4 * 2 ^ 4, G4 * 2 ^ 4, B4 * 2 ^ 4)
    End Function
    The Get4BitColor is used, to account for slight color-differences in the potential BorderColor
    (e.g. from JPG-Encoding).

    HTH

    Olaf

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: How to remove a frame/border around a picture?

    I threw a program together that detects borders and calculates the rectangular area contained. See the attachment.
    Attached Files Attached Files

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