Zeus
Zeus

Reputation: 129

Bitmap manipulation in Windows Phone: How to deal with GetPixel, SetPixel, PixelFormat, etc?

im trying to convert one of fully functional desktop apps to a Windows phone app, i was managed to convert most of the things..but one function is not working due to some syntax error.anyone know replacements for

GetPixel, FromArgb, SetPixel, MemoryStream, Marshal, ImageLockMode, PixelFormat

the code is this

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;


    }

Upvotes: 2

Views: 3399

Answers (2)

CHARAF MEFTAH
CHARAF MEFTAH

Reputation: 31

you have to change the type int into byte

Upvotes: 3

Zeus
Zeus

Reputation: 129

By using this WriteableBitmapEx GetPixel and SetPixel method extensions can be used

Upvotes: 0

Related Questions