C# 位图中的步幅是什么意思?带有注释的小型颜色选择器代码,用于指定有问题的行
C# 位图中的步幅是什么意思?带有注释的小型颜色选择器代码,用于指定有问题的行,c#,color-picker,C#,Color Picker,CopyPixels方法中的stride参数做什么? 如果你愿意的话,请随意帮我看其他有评论的行 BitmapSource bitmapSource = ColorSquare.Source as BitmapSource; if (bitmapSource != null) { double x = Mouse.GetPosition(ColorSquare).X; //This
CopyPixels
方法中的stride
参数做什么?
如果你愿意的话,请随意帮我看其他有评论的行
BitmapSource bitmapSource = ColorSquare.Source as BitmapSource;
if (bitmapSource != null)
{
double x = Mouse.GetPosition(ColorSquare).X;
//This next line is confusing. It seems like I'm just dividing an image by itself
//say the image width is 4 and since I'm using it as the bitmap source the
//both the PixelWidth and the ActualWidth will = 4, right? Basically x *= 1;
x *= bitmapSource.PixelWidth / ColorSquare.ActualWidth;
if ((int)x > bitmapSource.PixelWidth - 1)
x = bitmapSource.PixelWidth - 1;
else if (x < 0) x = 0;
double y = Mouse.GetPosition(ColorSquare).Y;
y *= bitmapSource.PixelHeight / ColorSquare.ActualHeight;
if ((int)y > bitmapSource.PixelHeight - 1)
y = bitmapSource.PixelHeight - 1;
else if (y < 0) y = 0;
CroppedBitmap cb = new CroppedBitmap(bitmapSource, new Int32Rect((int)x, (int)y, 1, 1));
byte[] pixels = new byte[4];
//What is the stride and why does it need to be 4?
cb.CopyPixels(pixels, 4, 0);
if (pixels[3] == byte.MaxValue)
{
//So it seems we are passing all 4 pixels we copied earlier into the array
//here. This would be the Alpha,Red,Green,Blue of a color. So is that what stride does?
color = Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);
}
}
BitmapSource BitmapSource=ColorSquare.Source作为BitmapSource;
if(bitmapSource!=null)
{
double x=Mouse.GetPosition(ColorSquare).x;
//下一行很混乱,好像我只是把一个图像本身分割开来
//假设图像宽度为4,因为我使用它作为位图源
//像素宽度和实际宽度都将=4,对吗?基本上x*=1;
x*=位图源.PixelWidth/ColorSquare.ActualWidth;
如果((int)x>bitmapSource.PixelWidth-1)
x=bitmapSource.PixelWidth-1;
如果(x<0)x=0,则为else;
双y=鼠标.GetPosition(ColorSquare).y;
y*=bitmapSource.PixelHeight/ColorSquare.ActualHeight;
如果((int)y>bitmapSource.PixelHeight-1)
y=bitmapSource.PixelHeight-1;
如果(y<0)y=0,则为else;
CroppedBitmap cb=新的CroppedBitmap(位图源,新的Int32Rect((int)x,(int)y,1,1));
字节[]像素=新字节[4];
//步幅是多少?为什么步幅必须是4?
cb.复制像素(像素,4,0);
if(像素[3]==字节.MaxValue)
{
//因此,我们似乎正在将之前复制的所有4个像素传递到数组中
//这里。这是阿尔法,红,绿,蓝的颜色。那么这就是迈步的功能吗?
color=color.FromArgb(像素[3],像素[2],像素[1],像素[0]);
}
}
要回答您的问题,它是4,因为您要求的是4像素(看起来)。然而,步幅(如method参数中所述)只是指您想要多少像素
然而,让我们深入挖掘
步幅(行步幅)本质上是内存中扫描行的长度(填充或其他)。也就是说,如果您必须迭代图像的一维数组,您可以使用它来计算像素和线条(以及其他内容)
与像素步长相反,像素在内存中占用的字节数。每像素32位的图像,每像素将有4个字节(32位)或Uint
摘要
- 像素地址-内存中的像素地址
- 像素深度-每个像素的位数(包含有价值的信息)
- 像素步长-图像像素在内存中占用的字节数
- 数据开始-内存中第一个图像像素的地址
- 通道-图像通道数(RGB图像为3个)
- 通道\ U地址-内存中特定像素通道的地址
- 高度-以像素为单位的图像高度
- 宽度-以像素为单位的图像宽度
- 行跨距-图像行在内存中占用的字节数
其他资源 将位图像素数据复制到具有 指定的步幅,从指定的偏移开始 参数
- 像素-
目标阵列阵列
- 步幅-
位图的步幅Int32
- 偏移量-
开始复制的像素位置Int32
我的天哪,落选票怎么了。文档中没有任何关于stride的信息,只是说它是CopyPixels的第二个参数。我不喜欢魔法代码,我想了解这个特殊参数的作用。文档中没有关于stride的任何信息哦,是吗?看见首先,请注意,在来到这里之前,您至少要做一些研究!好的,那么在我的例子中,这是否意味着我实际上在数组中存储了4个像素?@luigi,因为在这种情况下,你只需要4个像素,至于代码在做什么或者为什么,我不知道你在干什么,伙计。我想我现在明白了。我很感激