Remove surrounding whitespace from an image

前端 未结 8 868
时光说笑
时光说笑 2020-12-02 15:54

I have a block of product images we received from a customer. Each product image is a picture of something and it was taken with a white background. I would like to crop a

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 16:29

    I've written code to do this myself - it's not too difficult to get the basics going.

    Essentially, you need to scan pixel rows/columns to check for non-white pixels and isolate the bounds of the product image, then create a new bitmap with just that region.

    Note that while the Bitmap.GetPixel() method works, it's relatively slow. If processing time is important, you'll need to use Bitmap.LockBits() to lock the bitmap in memory, and then some simple pointer use inside an unsafe { } block to access the pixels directly.

    This article on CodeProject gives some more details that you'll probably find useful.

提交回复
热议问题