c# - Windows Phone 中的位图操作 : How to deal with GetPixel, SetPixel、PixelFormat 等?

标签 c# silverlight windows-phone-7 windows-phone

我正在尝试将功能齐全的桌面应用程序之一转换为 Windows 手机应用程序,我成功地转换了大部分内容..但由于某些语法错误,有一个功能无法正常工作。任何人都知道

获取像素, 从Argb, 设置像素, 内存流, 编码(marshal), 图像锁定模式, 像素格式

代码是这样

private void tresh()
    {
        int hight = image1.Source.Height;
        int width = image1.Source.Width;
        BitmapImage img = new BitmapImage(image1.Source);
        BitmapImage newImg = new BitmapImage(width, hight);
        int threshold = 0;

        for (int i = 0; i < hight; i++)
        {
            for (int j = 0; j < width; j++)
            {

                int grayScale = (int)((img.GetPixel(j, i).R * 0.3) + (img.GetPixel(j, i).G * 0.59) + (img.GetPixel(j, i).B * 0.11));
                Color nc = Color.FromArgb(grayScale, grayScale, grayScale);
                newImg.SetPixel(j, i, nc);

            }
        }

        image1.Source = newImg;

        MemoryStream ms = new MemoryStream();
        newImg.Save(ms, ImageFormat.Bmp);

        byte[] bmpBytes = ms.GetBuffer();

        threshold = getOtsuNumber(bmpBytes);





        byte[] newBytArr = new byte[bmpBytes.Length];




        for (int i = 0; i < bmpBytes.Length; i++)
        {


            if ((0xFF & bmpBytes[i]) >= threshold)
            {
                newBytArr[i] = ((byte)255);

            }
            else
            {
                newBytArr[i] = ((byte)0);
            }
        }


        BitmapImage oldBmp = newImg;
        width = oldBmp.Width;
        int height = oldBmp.Height;
        BitmapData oldData = oldBmp.LockBits(
                new Rectangle(0, 0, oldBmp.Width, oldBmp.Height),
                        ImageLockMode.WriteOnly,
                        oldBmp.PixelFormat);
        int length = oldData.Stride * oldBmp.Height;
        byte[] stream = new byte[length];
        Marshal.Copy(oldData.Scan0, stream, 0, length);
        oldBmp.UnlockBits(oldData);


        BitmapImage bmp = new Bitmap(width, height, oldBmp.PixelFormat);
        BitmapData bmpData = bmp.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.WriteOnly,
                bmp.PixelFormat);


        for (int n = 0; n < length; n++)
        {
            if ((0xFF & stream[n]) >= 57)
            {
                Marshal.WriteByte(bmpData.Scan0, n, ((byte)255));

            }
            else
            {
                Marshal.WriteByte(bmpData.Scan0, n, ((byte)0));
            }

        }

        bmp.UnlockBits(bmpData);
        image1.Source = bmp;


    }

最佳答案

您必须将类型 int 更改为 byte

关于c# - Windows Phone 中的位图操作 : How to deal with GetPixel, SetPixel、PixelFormat 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175356/

相关文章:

c# - Windows Phone 横向模式下的坐标

c# - 在树层次结构中具有深度限制的通用深度克隆

c# - 创建可用作函数/方法的类型

c# - 使用泛型接口(interface)的 WCF RESTful 服务

C# Silverlight Datagrid - 行颜色更改

c# 如何使用 xelement 读取 xml 属性

c# - Windows Phone 7 信息

c# - 在 C# 中为循环中的方法创建线程

silverlight - 如何将标记插入 Silverlight LIVE 视频流?

.net - 在 Silverlight 中显示格式化文本