C# 将图像像素放入数组
我正在尝试将以下代码从silverlight重写为wpf。在这里找到 我的问题是wpf中缺少可写位图。像素。如何做到这一点?我知道它是如何工作的,但我是从C#开始的,就像一周前一样 你能给我指一下正确的方向吗C# 将图像像素放入数组,c#,wpf,silverlight,C#,Wpf,Silverlight,我正在尝试将以下代码从silverlight重写为wpf。在这里找到 我的问题是wpf中缺少可写位图。像素。如何做到这一点?我知道它是如何工作的,但我是从C#开始的,就像一周前一样 你能给我指一下正确的方向吗 public WriteableBitmap GetMotionBitmap(WriteableBitmap current) { if (_previousGrayPixels != null && _previousGrayPixels.
public WriteableBitmap GetMotionBitmap(WriteableBitmap current)
{
if (_previousGrayPixels != null && _previousGrayPixels.Length > 0)
{
WriteableBitmap motionBmp = new WriteableBitmap(current.PixelWidth, current.PixelHeight);
int[] motionPixels = motionBmp.Pixels;
int[] currentPixels = current.Pixels;
int[] currentGrayPixels = ToGrayscale(current).Pixels;
for (int index = 0; index < current.Pixels.Length; index++)
{
byte previousGrayPixel = BitConverter.GetBytes(_previousGrayPixels[index])[0];
byte currentGrayPixel = BitConverter.GetBytes(currentGrayPixels[index])[0];
if (Math.Abs(previousGrayPixel - currentGrayPixel) > Threshold)
{
motionPixels[index] = _highlightColor;
}
else
{
motionPixels[index] = currentPixels[index];
}
}
_previousGrayPixels = currentGrayPixels;
return motionBmp;
}
else
{
_previousGrayPixels = ToGrayscale(current).Pixels;
return current;
}
}
public WriteableBitmap ToGrayscale(WriteableBitmap source)
{
WriteableBitmap gray = new WriteableBitmap(source.PixelWidth, source.PixelHeight);
int[] grayPixels = gray.Pixels;
int[] sourcePixels = source.Pixels;
for (int index = 0; index < sourcePixels.Length; index++)
{
int pixel = sourcePixels[index];
byte[] pixelBytes = BitConverter.GetBytes(pixel);
byte grayPixel = (byte)(0.3 * pixelBytes[2] + 0.59 * pixelBytes[1] + 0.11 * pixelBytes[0]);
pixelBytes[0] = pixelBytes[1] = pixelBytes[2] = grayPixel;
grayPixels[index] = BitConverter.ToInt32(pixelBytes, 0);
}
return gray;
}
public WriteableBitmap GetMotionBitmap(WriteableBitmap当前)
{
如果(_previousGrayPixels!=null&&u previousGrayPixels.Length>0)
{
WriteableBitmap motionBmp=新的WriteableBitmap(current.PixelWidth,current.PixelHeight);
int[]motionPixels=motionBmp.Pixels;
int[]currentPixels=current.Pixels;
int[]currentGrayPixels=ToGrayscale(当前).Pixels;
对于(int index=0;index阈值)
{
motionPixels[索引]=\u highlightColor;
}
其他的
{
运动像素[索引]=当前像素[索引];
}
}
_previousGrayPixels=当前灰度像素;
返回运动;
}
其他的
{
_previousGrayPixels=ToGrayscale(当前).Pixels;
回流;
}
}
公共WriteableBitmap ToGrayscale(WriteableBitmap源)
{
WriteableBitmap gray=新的WriteableBitmap(source.PixelWidth,source.PixelHeight);
int[]灰度像素=灰度像素;
int[]sourcePixels=source.Pixels;
对于(int index=0;index
`为了获取位图的原始像素数据,您可以使用以下方法之一,例如:
var bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8;
var stride = source.PixelWidth * bytesPerPixel;
var bufferSize = source.PixelHeight * stride;
var buffer = new byte[bufferSize];
source.CopyPixels(buffer, stride, 0);
var grayscaleBitmap = new FormatConvertedBitmap(source, PixelFormats.Gray8, null, 0d);
写入WriteableBitmap
可以通过其方法之一完成
或者,您可以通过WriteableBitmap的属性访问位图缓冲区
要将位图转换为灰度,可以使用如下方法:
var bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8;
var stride = source.PixelWidth * bytesPerPixel;
var bufferSize = source.PixelHeight * stride;
var buffer = new byte[bufferSize];
source.CopyPixels(buffer, stride, 0);
var grayscaleBitmap = new FormatConvertedBitmap(source, PixelFormats.Gray8, null, 0d);