Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf BitmapSource.CopyPixels-什么';步幅的价值是多少?_Wpf_Image - Fatal编程技术网

Wpf BitmapSource.CopyPixels-什么';步幅的价值是多少?

Wpf BitmapSource.CopyPixels-什么';步幅的价值是多少?,wpf,image,Wpf,Image,我试图从WPFBitmapSource对象获取像素数据。据我所知,这可以通过调用其CopyPixels方法来实现。这种方法需要一个步幅参数,我不知道如何获得。据我所知,stride是在读取或复制期间在数组中单步执行时使用的值。对于任何位图源,什么是合适的步长值?您可以使用步长=像素大小*图像宽度值。例如,对于100像素宽的RGBA位图,步幅=400 某些应用可能需要特殊的线路对齐。例如,Windows GDI位图需要32位的行对齐。在这种情况下,对于宽度为33的RGB位图,跨距值33*3=99应

我试图从WPF
BitmapSource
对象获取像素数据。据我所知,这可以通过调用其
CopyPixels
方法来实现。这种方法需要一个步幅参数,我不知道如何获得。据我所知,stride是在读取或复制期间在数组中单步执行时使用的值。对于任何位图源,什么是合适的步长值?

您可以使用步长=像素大小*图像宽度值。例如,对于100像素宽的RGBA位图,步幅=400

某些应用可能需要特殊的线路对齐。例如,Windows GDI位图需要32位的行对齐。在这种情况下,对于宽度为33的RGB位图,跨距值33*3=99应更改为100,以便在目标阵列中具有32位的线对齐

通常,您应该知道目标阵列的要求。在没有特殊要求的情况下,使用默认像素大小*图像宽度

var stride = ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) / 32) * 4;


var stride=((bitmapSource.PixelWidth*bitmapSource.Format.BitsPerPixel+31)>>5)所以我猜它是以字节为单位的像素大小?是的,这是以字节为单位的像素大小。Format属性返回PixelFormat结构。例如,Bgr32的像素大小是4,Bgr24-3,Gray8-1等。将位图数据复制到数组中,您需要确切地知道结果数组结构,它是由Format属性定义的。我个人使用这样的代码进行32位对齐(以便我可以从wpf图像创建System.Drawing.bitmap):
var stride=width*(bitmapSource.Format.BitsPerPixel/8);stride+=(4-stride%4);
Oups,上面有一个错误。如果宽度已经是32位对齐的,我会在跨距上额外添加4个字节…因此工作代码应该是
var stride=width*(bitmapSource.Format.BitsPerPixel/8);var mod=stride%4;If(mod!=0)stride+=4-mod;
@odalet-事实上,您的步幅计算可能可以使用GDI+,但WPF BitmapSource是错误的。对于BitmapSource.CopyPixels,您不能添加32位填充。Alex的答案是正确的,对于WPF,它只是PixelWidth*BitsPerPixel
var stride = ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) >> 5) << 2;