gpt4 book ai didi

c# - 穿越 BMP 中的像素

转载 作者:IT王子 更新时间:2023-10-29 04:20:02 27 4
gpt4 key购买 nike

enter image description here

您好,我有一个 bmp 加载到 BMP 对象,我需要像上图那样从 (1,1) 像素到 (100,100) px 。使用 getpixel() 方法。我使用的是 ONE 循环,但没有成功。

如果我使用多维数组的概念,变量值应该是什么?

最佳答案

When you want to doing image processing on huge images GetPixel() method takes long time but I think my algorithm takes less time than other answers , for example you can test this code on 800 * 600 pixels image.


Bitmap bmp = new Bitmap("SomeImage");

// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
byte[] r = new byte[bytes / 3];
byte[] g = new byte[bytes / 3];
byte[] b = new byte[bytes / 3];

// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, bytes);

int count = 0;
int stride = bmpData.Stride;

for (int column = 0; column < bmpData.Height; column++)
{
for (int row = 0; row < bmpData.Width; row++)
{
b[count] = (byte)(rgbValues[(column * stride) + (row * 3)]);
g[count] = (byte)(rgbValues[(column * stride) + (row * 3) + 1]);
r[count++] = (byte)(rgbValues[(column * stride) + (row * 3) + 2]);
}
}

关于c# - 穿越 BMP 中的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020406/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com