2

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

GetPixel、FromArgb、SetPixel、MemoryStream、元帅、ImageLockMode、PixelFormat

代码是这样的

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;


    }
4

2 回答 2

3

您必须将类型更改intbyte

于 2014-07-04T11:51:52.593 回答
0

通过使用这个WriteableBitmapEx可以使用 GetPixel 和 SetPixel 方法扩展

于 2012-06-24T09:51:02.473 回答