c# - Convert a dynamic BitmapImage to a grayscale BitmapImage in a Windows Phone application -
c# - Convert a dynamic BitmapImage to a grayscale BitmapImage in a Windows Phone application -
i convert bitmapimage grayscale bitmapimage: method , hence - width , height unknown me. have tried looking options such writablebitmapex , static extension methods haven't been helpful me homecoming info type bitmapimage need add together list.
is possible in windows phone app using c#? appreciate if shed lite this. give thanks you.
the algorithm pretty simple:
using system.windows.media.imaging; using system.io; private writeablebitmap converttograyscale(bitmapimage source) { writeablebitmap wb = new writeablebitmap(source); // create writablebitmap using source int[] graypixels = new int[wb.pixelwidth * wb.pixelheight]; // lets utilize average algo (int x = 0; x < wb.pixels.length; x++) { // pixel int pixel = wb.pixels[x]; // component int reddish = (pixel & 0x00ff0000) >> 16; int bluish = (pixel & 0x0000ff00) >> 8; int greenish = (pixel & 0x000000ff); // average int average = (byte)((red + bluish + green) / 3); // assign grayness values maintain alpha unchecked { graypixels[x] = (int)((pixel & 0xff000000) | average << 16 | average << 8 | average); } } // re-create graypixels pixels buffer.blockcopy(graypixels, 0, wb.pixels, 0, (graypixels.length * 4)); homecoming wb; } private bitmapimage convertwbtobi(writeablebitmap wb) { bitmapimage bi; using (memorystream ms = new memorystream()) { wb.savejpeg(ms, wb.pixelwidth, wb.pixelheight, 0, 100); bi = new bitmapimage(); bi.setsource(ms); } homecoming bi; } <image x:name="myimage" source="/assets/alignmentgrid.png" stretch="none" /> private void phoneapplicationpage_loaded(object sender, routedeventargs e) { writeablebitmap wb = converttograyscale((bitmapimage)this.myimage.source); bitmapimage bi = convertwbtobi(wb); myimage.source = bi; } code in action:
c# windows-phone-8 bitmapimage grayscale
Comments
Post a Comment