转换像素数据到位图数组- WP8 - c#

本文关键字:数组 WP8 位图 像素 像素数 数据 转换 | 更新日期: 2023-09-27 18:07:17

我想做一个应用程序,使用相机,从它获得帧,转换它,并把它放在屏幕上。我从微软找到了一个教程,可以转换为灰度,但我真的不需要。相反,我需要int数组,我必须从一个8b位图,所以我的转换函数可以正常工作。所以主要的问题是我如何将像素数据数组转换为位图数组然后将其转回来,以便我可以将其显示在屏幕上?另一个解决方案是直接获得位图int数组从相机,但我怎么能做到这一点?

我需要处理以下代码:

    void PumpARGBFrames()
    {
        // Create capture buffer.
        Width = (int)cam.PreviewResolution.Width;
        Height = (int)cam.PreviewResolution.Height;
        int[] ARGBPx = new int[Width * Height];
        try
        {
            PhotoCamera phCam = (PhotoCamera)cam;
            while (pumpARGBFrames)
            {
                pauseFramesEvent.WaitOne();
                phCam.GetPreviewBufferArgb32(ARGBPx);
                //here i need to do the conversion back and forward
                pauseFramesEvent.Reset();
                Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    ARGBPx.CopyTo(wb.Pixels, 0);
                    wb.Invalidate();
                    pauseFramesEvent.Set();
                });
            }
        }
        catch (Exception e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                // Display error message.
                txtDebug.Text = e.Message;
            });
        }
    }

所以,事实是我不需要一个位图,而是一个int数组作为8b位图的来源。我从微软得到的教程在这里。谢谢。

转换像素数据到位图数组- WP8 - c#

试试这样写:

Bitmap bmp = new Bitmap(Width, Height);
for (int i = 0; i < ARGBPx.Length; ++i)
{
    bmp.SetPixel(
        i % Width,
        i / Width,
        /* something to create a Color object from the pixel int value */
}

好的,我意识到像素数据数组实际上是一个颜色数组,即32b。所以我必须把32b的颜色转换成8b的颜色,没有我想象的那么难。

谢谢。