在C#中裁剪图像白色空间 [英] Crop image white space in C#

查看:108
本文介绍了在C#中裁剪图像白色空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有请求在C#中裁剪图像空白区域,我从论坛搜索一些方法,但它无法满足我的要求。

I have the request that crop image white space in C#, and I search some methods from the forum, but it could not satisfy my request.

有原始图片,

这是我期望的结果,

任何帮助都表示赞赏。

推荐答案

您可以尝试获取第一个图像数据(有图像),然后将数据绘制到新图像中。试试这个方法。希望它可以帮到你。

You can try to get first image data(There is an image), and draw the data into a new image. Try this method. Hope it can help you.

private static Bitmap ImageTrim(Bitmap img)
{
    //get image data
    BitmapData bd= img.LockBits(new Rectangle(Point.Empty, img.Size),
    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    int[] rgbValues = new int[img.Height * img.Width];
    Marshal.Copy(bd.Scan0, rgbValues, 0, rgbValues.Length);
    img.UnlockBits(bd);


    #region determine bounds
    int left = bd.Width;
    int top = bd.Height;
    int right = 0;
    int bottom = 0;

    //determine top
    for (int i = 0; i < rgbValues.Length; i++)
    {
        int color = rgbValues[i] & 0xffffff;
        if (color != 0xffffff)
        {
            int r = i / bd.Width;
            int c = i % bd.Width;

            if (left > c)
            {
                left = c;
            }
            if (right < c)
            {
                right = c;
            }
            bottom = r;
            top = r;
            break;
        }
    }

    //determine bottom
    for (int i = rgbValues.Length - 1; i >= 0; i--)
    {
        int color = rgbValues[i] & 0xffffff;
        if (color != 0xffffff)
        {
            int r = i / bd.Width;
            int c = i % bd.Width;

            if (left > c)
            {
                left = c;
            }
            if (right < c)
            {
                right = c;
            }
            bottom = r;
            break;
        }
    }

    if (bottom > top)
    {
        for (int r = top + 1; r < bottom; r++)
        {
            //determine left
            for (int c = 0; c < left; c++)
            {
                int color = rgbValues[r * bd.Width + c] & 0xffffff;
                if (color != 0xffffff)
                {
                    if (left > c)
                    {
                        left = c;
                        break;
                    }
                }
            }

            //determine right
            for (int c = bd.Width - 1; c > right; c--)
            {
                int color = rgbValues[r * bd.Width + c] & 0xffffff;
                if (color != 0xffffff)
                {
                    if (right < c)
                    {
                        right = c;
                        break;
                    }
                }
            }
        }
    }

    int width = right - left + 1;
    int height = bottom - top + 1;
    #endregion

    //copy image data
    int[] imgData = new int[width * height];
    for (int r = top; r <= bottom; r++)
    {
        Array.Copy(rgbValues, r * bd.Width + left, imgData, (r - top) * width, width);
    }

    //create new image
    Bitmap newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    BitmapData nbd
        = newImage.LockBits(new Rectangle(0, 0, width, height),
            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(imgData, 0, nbd.Scan0, imgData.Length);
    newImage.UnlockBits(nbd);

    return newImage;
}            

这篇关于在C#中裁剪图像白色空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆