Results 1 to 18 of 18

Thread: Crop Filter: Crop Picture

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Resolved Crop Filter: Crop Picture

    This is a tested code, which is perfect. If there is a better way, you can also communicate together.

    Code:
    Function CutImg(PicFile As String, SaveAs As String, Left As Long, Top As Long, newWidth As Long, newHeight As Long) As Boolean
    ' Intercept area, how much space is left on the left, how much space is above, width, height
    On Error GoTo ERR1
        Dim Img As ImageFile
        Dim IP As ImageProcess
        Set Img = CreateObject("WIA.ImageFile")
        Set IP = CreateObject("WIA.ImageProcess")
        Dim ImgWidth As Long, ImgHeight As Long
         Img.LoadFile PicFile
        ImgWidth = Img.Width
        ImgHeight = Img.Height
        Dim Right As Long, Bottom As Long
        If Left >= ImgWidth Or Right >= ImgHeight Then Exit Function
        
        Right = ImgWidth - Left - newWidth
        If Right <= 0 Then Right = 0
        
        Bottom = ImgHeight - Top - newHeight
        If Bottom <= 0 Then Bottom = 0
        
        
        IP.Filters.Add IP.FilterInfos("Crop").FilterID
        IP.Filters(1).Properties("Left") = Left
        IP.Filters(1).Properties("Top") = Top
        IP.Filters(1).Properties("Right") = Right
        IP.Filters(1).Properties("Bottom") = Bottom
        Set Img = IP.Apply(Img)
    
        CutImg = True
        On Error Resume Next
        Kill SaveAs
        Err.Clear
        Img.SaveFile SaveAs
        CutImg = Err.Number = 0
        Exit Function
    ERR1:
    MsgBox Err.Description
    End Function
    
    Function CutImg2(PicFile As String, SaveAs As String, Left As Long, Top As Long, Right As Long, Bottom As Long) As Boolean
    ' Intercept area, how much empty left, how much empty above, how much empty right, how much empty below
    
    On Error GoTo ERR1
        Dim Img As ImageFile
        Dim IP As ImageProcess
        Set Img = CreateObject("WIA.ImageFile")
        Set IP = CreateObject("WIA.ImageProcess")
        Img.LoadFile PicFile
        Dim ImgWidth As Long, ImgHeight As Long
    
        ImgWidth = Img.Width
        ImgHeight = Img.Height
        If Left >= ImgWidth Or Right >= ImgWidth Or Bottom >= ImgHeight Or Top >= ImgHeight Then Exit Function
        IP.Filters.Add IP.FilterInfos("Crop").FilterID
        IP.Filters(1).Properties("Left") = Left
        IP.Filters(1).Properties("Top") = Top
        IP.Filters(1).Properties("Right") = Right
        IP.Filters(1).Properties("Bottom") = Bottom
        Set Img = IP.Apply(Img)
        CutImg2 = True
        On Error Resume Next
        Kill SaveAs
        Err.Clear
        Img.SaveFile SaveAs
        CutImg2 = Err.Number = 0
        Exit Function
    ERR1:
    End Function
    Last edited by xiaoyao; Sep 5th, 2021 at 02:39 AM. Reason: Please move to VB6 code bank

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    The saved picture becomes 672*265, I don’t know why

    Code:
    Private Sub Form_Load()
    MsgBox CutImg(App.Path & "\cat.jpg", App.Path & "\test.bmp", 100, 100, 400, 400)
    End Sub
    Function CutImg(PicFile As String, SaveAs As String, Left As Long, Top As Long, Right As Long, Bottom As Long) As Boolean
    On Error GoTo ERR1
        Dim Img As ImageFile
        Dim IP As ImageProcess
        Set Img = CreateObject("WIA.ImageFile")
        Set IP = CreateObject("WIA.ImageProcess")
        Img.LoadFile PicFile
        IP.Filters.Add IP.FilterInfos("Crop").FilterID
        IP.Filters(1).Properties("Left") = Left
        IP.Filters(1).Properties("Top") = Top
        IP.Filters(1).Properties("Right") = Right
        IP.Filters(1).Properties("Bottom") = Bottom
        Set Img = IP.Apply(Img)
        CutImg = True
        On Error Resume Next
        Kill SaveAs
        Err.Clear
        Img.SaveFile SaveAs
        CutImg = Err.Number = 0
        Exit Function
    ERR1:
    End Function

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Crop Filter: Crop Picture

    You told it to crop 100 pixels from the left and top and 400 pixels from the right and bottom.

    Code:
          |                     |
          | 100 ↓               |
    ------+---------------------+----------------
    100 → |#####################| ← 400
          |#####################|
          |##### Retained ######|
          |##### Portion  ######|
          |#####################|
          |#####################|
    ------+---------------------+----------------
          | 400 ↑               |
          |                     |
          |                     |
          |                     |
          |                     |

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Quote Originally Posted by dilettante View Post
    You told it to crop 100 pixels from the left and top and 400 pixels from the right and bottom.
    Understand, just checked the online information, thank you very much
    The general purpose is to crop the image from a certain area, like the RECT attribute. So I wrote 2 interface functions
    Last edited by xiaoyao; Sep 5th, 2021 at 02:43 AM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Using WIA objects for image cropping can retain the original horizontal resolution. If you use GDIPLUS to crop the image, I don’t know how to do this. Which method is faster?

    I wonder if this article is useful

    It is not too difficult for VFP to use GDI to process pictures. The GdiPlus class is specifically to do this, please refer to other information about its properties. The code examples for handling image scaling and resolution are posted below. These codes are from a long time ago, because the Xinguanzhai family remembered nothing, so they transferred it out.

    Local lcFile_S, lcFile_T, lnW_T, lnH_T, lnXDpi_T, lnYDpi_T

    m.lcFile_S ='D:\Temp\Source.jpg'
    m.lcFile_T ='D:\Temp\Target.jpg'

    m.lnW_T = 160 && target image width and height
    m.lnH_T = 240
    m.lnXDpi_T = 72 && target image resolution
    m.lnYDpi_T = 72

    Set Classlib To "_GDIPlus.Vcx" Additive

    oGraphics=CreateObject("gpGraphics") && Create a working image

    oImage=CreateObject("gpImage") && Source image
    oImage.CreateFromFile(m.lcFile_S) && Create from source image
    m.lnW = oImage.ImageWidth && width
    m.lnH = oImage.ImageHeight && high
    m.lnXDpi = oImage.HorizontalResolution && Horizontal resolution
    m.lnYDpi = oImage.VerticalResolution && vertical resolution

    oBitMap=CreateObject("gpBitMap") && target map
    oBitMap.Create(m.lnW_T, m.lnH_T) && Create according to the target map size

    oGraphics.CreateFromImage(oBitMap) && The working image is created according to the target image
    oBitMap.SetResolution(m.lnXDpi_T, m.lnYDpi_T) && Set the resolution of the target image

    oGraphics.DrawImageScaled(oImage, 0, 0, m.lnW_T, m.lnH_T) && The source image is scaled according to the specified rectangular area

    m.lnQuality = 90 && 100 && Picture preservation quality
    oBitMap.SaveToFile(m.lcFile_T, oImage.GetEncoderCLSID("image/jpeg"), "quality="+Transform(m.lnQuality)) && Save to the target file according to the source file type

    Release oBitMap && Release
    Release oImage
    Release oGraphics

    Release Classlib _GdiPlus

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Crop Filter: Crop Picture

    You asked that this be moved to the CodeBank, but it was a question until you edited it, and it still has a question in it, though now it appears to be resolved. If you feel that the code should be a CodeBank submission, then start a thread with the code in the CodeBank. As long as you are asking questions about it, then it is a question thread, not a CodeBank thread.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Quote Originally Posted by Shaggy Hiker View Post
    You asked that this be moved to the CodeBank, but it was a question until you edited it, and it still has a question in it, though now it appears to be resolved. If you feel that the code should be a CodeBank submission, then start a thread with the code in the CodeBank. As long as you are asking questions about it, then it is a question thread, not a CodeBank thread.
    There are many custom controls on CodeBank, and many people have asked questions in the past 10 years. The code is correct, but another question is simply raised, but it does not necessarily need to be resolved. If you post a new question again, you will have to paste all of these codes, causing a lot of duplication.

    If the related problem in the corresponding topic is serious, a topic will be republished.
    For example, the image rotation problem I was researching 2 days ago. I also posted a separate post on how to find the coordinates of the 4 points after the rectangle is rotated.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Using this API to crop images is faster than WIA, and the problem of this topic has been resolved. Please move to vb6 code bank

    Code:
    Declare Function GdipDrawImageRectRectI Lib "GdiPlus.dll" (ByVal hGraphics As Long, ByVal hImage As Long, ByVal dstX As Long, ByVal dstY As Long, ByVal dstWidth As Long, ByVal dstHeight As Long, ByVal srcX As Long, ByVal srcY As Long, ByVal srcWidth As Long, ByVal srcHeight As Long, ByVal srcUnit As Long, ByVal imageAttributes As Long, ByVal Callback As Long, ByVal callbackData As Long) As Long

  9. #9
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Crop Filter: Crop Picture

    Quote Originally Posted by xiaoyao View Post
    Using this API to crop images is faster than WIA, and the problem of this topic has been resolved.
    Code:
    Declare Function GdipDrawImageRectRectI Lib "GdiPlus.dll" (ByVal hGraphics As Long, ByVal hImage As Long, ByVal dstX As Long, ByVal dstY As Long, ByVal dstWidth As Long, ByVal dstHeight As Long, ByVal srcX As Long, ByVal srcY As Long, ByVal srcWidth As Long, ByVal srcHeight As Long, ByVal srcUnit As Long, ByVal imageAttributes As Long, ByVal Callback As Long, ByVal callbackData As Long) As Long
    Please move to vb6 code bank
    The Codebank contains fully working examples (usually written by yourself).

    May I ask, what exactly your contribution is in this case?

    I mean, just copying an API-Declaration out of someone elses GitHub-Repo -
    or out of an already existing Codebank-entry is not really the way to go.

    Olaf

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Crop Filter: Crop Picture

    Quote Originally Posted by xiaoyao View Post
    Using this API to crop images is faster than WIA
    Probably not by much, since WIA 2.0's ImageFile, ImageProcess, etc. are thin wrappers around those GDI+ calls. There is some overhead, but it is mostly added value.

    Use whichever you want. WIA is just there to support scanners and still images. Using it as a general image manipulation tool is just a convenience.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Image conversion to JPG format, gdiplus is 10-50% faster than WIA The larger the picture file, the greater the difference in speed.

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Crop Filter: Crop Picture

    Quote Originally Posted by xiaoyao View Post
    Image conversion to JPG format, gdiplus is 10-50% faster than WIA The larger the picture file, the greater the difference in speed.
    Do both conversions produces same sized output?

    Do both same sized JPGs have similar (equal) PSNR compared to original image?

    Please, don't benchmark anything already!

    cheers,
    </wqw>

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Use the same picture size.3840 * 2160, 4K HD imageFor example, desktop screenshots, a small number of regional changes.

    Four pictures were intercepted, and then two methods were used to operate twice.I don't think gdiplus is fast enough.Dx technology is more convenient for graphics card acceleration estimation。

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    Cut out the larger part of the 31.6MB bmp file and save it as JPG, only 740KB

    the speed difference is about 50-70%, the time used for GDIPLUS is 72 milliseconds, and WIA=121 milliseconds.
    It can process 10 super-large pictures per second. This time consumption should be acceptable.

    QueryPerformanceCounter CPUv1
    WiaCutImg App.Path & "\4.bmp", App.Path & "\Cut04.jpg", 100, 100, 3000, 2000
    QueryPerformanceCounter CPUv2
    UsedTime4 = (CPUv2 - CPUv1) / MsCount

    cutimg, gdi++,/wia
    4 bmp files:1-4.bmp
    3840*2160 dpi=96

    cut img to 3000*2000
    ---------------------------
    UsedTime=
    73.1637 Milliseconds,140.6002 Milliseconds
    73.7471 Milliseconds,125.9543 Milliseconds
    ---------
    UsedTime=gdiplus,wia
    71.6373 Milliseconds,131.8394 Milliseconds
    72.4218 Milliseconds,121.7703 Milliseconds
    74.9797 Milliseconds,130.5286 Milliseconds,CreateObjectUsedTime=7.7552
    71.6462 Milliseconds,118.2659 Milliseconds,CreateObjectUsedTime=0.0811

    =======================
    cutimg, gdi++,/wia to 481*481
    ---------------------------
    UsedTime=11.7764 Milliseconds,79.0965 Milliseconds

    Code:
    Function WiaCutImg(PicFile As String, SaveAs As String _
    , Left As Long, Top As Long, newWidth As Long, newHeight As Long, Optional JpgQuality As Long = 85) As Boolean
    '作者:逍遥爱迪生
    '设计时间:2021-9-5
    
    ' 截取区域,左边空多少,上面空多少,宽度,高度
    On Error GoTo Err1
        Dim Img As ImageFile
        Dim IP As ImageProcess
        Set Img = CreateObject("WIA.ImageFile")
        Set IP = CreateObject("WIA.ImageProcess")
        Dim ImgWidth As Long, ImgHeight As Long
        Img.LoadFile PicFile
        
        ImgWidth = Img.Width
        ImgHeight = Img.Height
        If newWidth = -1 Then newWidth = ImgWidth - Left
        If newHeight = -1 Then newHeight = ImgHeight - Top
        
        Dim Right As Long, Bottom As Long
        If Left >= ImgWidth Or Right >= ImgHeight Then Exit Function
        
        Right = ImgWidth - Left - newWidth
        If Right < 0 Then Right = 0
        
        Bottom = ImgHeight - Top - newHeight
        If Bottom < 0 Then Bottom = 0
        
        
        IP.Filters.Add IP.FilterInfos("Crop").FilterID
        IP.Filters(1).Properties("Left") = Left
        IP.Filters(1).Properties("Top") = Top
        IP.Filters(1).Properties("Right") = Right
        IP.Filters(1).Properties("Bottom") = Bottom
        
    IP.Filters.Add IP.FilterInfos("Convert").FilterID
    IP.Filters(2).Properties("FormatID").value = wiaFormatJPEG
    IP.Filters(2).Properties("Quality").value = JpgQuality
    
        Set Img = IP.Apply(Img)
    
        WiaCutImg = True
        On Error Resume Next
        Kill SaveAs
        Err.Clear
        
        
        Img.SaveFile SaveAs
        WiaCutImg = Err.Number = 0
        Exit Function
    Err1:
    MsgBox Err.Description
    End Function
    Last edited by xiaoyao; Sep 6th, 2021 at 07:12 AM.

  15. #15
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Crop Filter: Crop Picture

    How to save in different formats? For example, .jpg, .gif and .bmp.

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Crop Filter: Crop Picture

    I am not arguing WIA is slower tha GDI+ for some kind of operations but your bullshit approach to benchmarking anything is abomination.

    Always comparing apples to oranges e.g. you’re including BMP load times in the code above.

    Always bombastic inacurate estimates e.g. first it was 10-50% faster, now it’s 50-70% - you are completely unaware how to express mean result and std deviation because your math education is not enough.

    Will you stop it already?

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    You Didn't read my words carefully.

    If it's a very small picture. This time error may be 10%.But the bigger the picture, the more the difference in time scale.
    It's like deduplication. The larger the amount of data, it is multiplied according to the square algorithm.

    Use the same size picture file.
    Because there is no WIA component source code, so this time is wasted in loading the file or where we do not know.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Crop Filter: Crop Picture

    I just need to understand that gdiplus is faster.Ordinary small files can improve the speed, 10% -30% is enough.
    The advantage of WIA is that the code is so simple that almost 90% of people can understand it.Gdiplus has a lot of optimizations.

    There may be other better methods or components.
    This is a screenshot of the image format conversion. Thumbnail or image reduction. Comparing these algorithms, WIA is relatively slow.

    Code:
     Img.LoadFile PicFile
    GdipCreateBitmapFromFile
    GdipLoadImageFromFile
    Perhaps the main time is in the place where the file is loaded.
    I've come to the conclusion. Gdiplus runs faster than WIA.If you can increase the speed by 20%, you can.
    Last edited by xiaoyao; Sep 8th, 2021 at 10:58 AM.

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