【问题标题】:Crop an image from a byte array从字节数组中裁剪图像
【发布时间】:2013-10-02 09:10:55
【问题描述】:

我有一个图像(它是一个 Sprite),我将它存储在一个字节数组中。

我想只提取与此字节数组中特定位置和大小相关的字节,以便创建新图像,基本上是裁剪。

我正在使用 C# 和紧凑的 cf。我可以使用获取像素并将每个值保存到一个字节数组中,然后“读取”我感兴趣的部分。我知道我可以使用LockBitmap() 来加快速度。我通常会使用Aforge 和/或Emgu,但正如我所说,我使用的是紧凑型 cf 框架 2。

我会对任何已知的方法感兴趣。

谢谢


附加。

按照下面的链接,我想知道这段迭代代码是否有替代方案(如缓冲区副本)?

//Iterate the selected area of the original image, and the full area of the new image
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width * BPP; j += BPP)
    {
        int origIndex = (startX * rawOriginal.Stride) + (i * rawOriginal.Stride) + (startY * BPP) + (j);
        int croppedIndex = (i * width * BPP) + (j);

        //copy data: once for each channel
        for (int k = 0; k < BPP; k++)
        {
            croppedBytes[croppedIndex + k] = origBytes[origIndex + k];
        }
    }
}

【问题讨论】:

  • @Vaibs_Cool 您好,感谢您的链接。昨晚我确实看过,但返回的图像是黑白的。我搞砸了像素格式,但收到一个错误,说只读。显然昨晚很累,因为我只是做了一个简单的复制和粘贴,它就可以了。所以,我想我有点厚。你应该得到一个“打勾”,因为你让我重新审视它。谢谢

标签: arrays bitmap


【解决方案1】:

我知道这是一个老问题,但这是我的看法:

public static byte[] CropImageArray(byte[] pixels, int sourceWidth, int bitsPerPixel, Int32Rect rect)
{
    var blockSize = bitsPerPixel / 8;
    var outputPixels = new byte[rect.Width * rect.Height * blockSize];

    //Create the array of bytes.
    for (var line = 0; line <= rect.Height - 1; line++)
    {
        var sourceIndex = ((rect.Y + line) * sourceWidth + rect.X) * blockSize;
        var destinationIndex = line * rect.Width * blockSize;

        Array.Copy(pixels, sourceIndex, outputPixels, destinationIndex, rect.Width * blockSize);
    }

    return outputPixels;
}

您需要知道每像素的位数和宽度。您将使用一个而不是两个。

【讨论】:

  • 块大小和通道大小一样吗?
  • 这应该是公认的答案。
【解决方案2】:

我有更多的链接给你

试试你是否找到了解决方案,或者它对你有任何帮助

1)http://www.codeproject.com/Articles/33838/Image-Processing-using-C

2)http://codenicely.blogspot.in/2012/03/how-to-crop-image-in-c.html

【讨论】:

  • 感谢其他链接。我以前看过那些,但我需要直接处理字节数据。原因是我正在使用一个非常大的 Sprite,这会给我在移动设备上的内存不足异常。所以,流程是我的服务器上有一个大图像。它以字节数组的形式发送到设备。然后我动态提取我想要的图像。
  • 嗨,我使用了 1455x352 的图像,我尝试提取一个 873,0,291,172 的矩形,但我在这一行得到“索引超出范围异常:croppedBytes[croppedIndex + k] = origBytes[origIndex + k];
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 2014-04-29
  • 2010-10-10
  • 2011-05-05
  • 1970-01-01
相关资源
最近更新 更多