WriteableBitmapFloodFillExtensions usage

Oct 14, 2013 at 10:07 AM
Hi,

I wanted to use the FloodFill extention for WriteableBitmapFloodFillExtensions. I tried using it but it did not work. The functionality I require is that when I click at a point, the closed area should be colored with the selected color. So, it is a kind of coloring functionality that I want to implement.

Please help !!!
Coordinator
Oct 14, 2013 at 5:48 PM
Sorry, it seems like there is no sample included. I can help with specific problems, but other than that you might need to figure it out by yourself, since right now I'm focusing on some other work. I think there are some classes to convert a Color struct to an int, so you would just load a WriteableBitmap and use one of the extensions to fill it. What is not working, what have you tried and what's your code? I'd try Stack Overflow too - http://stackoverflow.com/questions/tagged/winrt-toolkit
Oct 15, 2013 at 5:44 AM
This was the code I used,
    private async void img_PointerPressed_1(object sender, PointerRoutedEventArgs e)
    {
        WriteableBitmap bitmap = new WriteableBitmap((int)img.Height, (int)img.Width);
        // load a jpeg, be sure to have the Pictures Library capability in your manifest
        var folder = KnownFolders.PicturesLibrary;
        var file = await folder.GetFileAsync("1.png");
        var data = await FileIO.ReadBufferAsync(file);

        // create a stream from the file
        var ms = new InMemoryRandomAccessStream();
        var dw = new Windows.Storage.Streams.DataWriter(ms);
        dw.WriteBuffer(data);
        await dw.StoreAsync();
        ms.Seek(0);

        // find out how big the image is, don't need this if you already know
        var bm = new BitmapImage();
        await bm.SetSourceAsync(ms);

        // create a writable bitmap of the right size
        var wb = new WriteableBitmap(bm.PixelWidth, bm.PixelHeight);
        ms.Seek(0);

        // load the writable bitpamp from the stream
        await wb.SetSourceAsync(ms);

        WriteableBitmapFloodFillExtensions.FloodFill(wb, (int)e.GetCurrentPoint(img).Position.X, (int)e.GetCurrentPoint(img).Position.Y
            , 255, 100);

        img1.Source = wb;

    }
}
So basically I converted the image to writable bitmap, and then used FloodFill method. It simply colored the whole image and not a part of it. Is there something i am doing wrong ?
Coordinator
Oct 15, 2013 at 6:14 AM
Edited Oct 15, 2013 at 6:14 AM
There are multiple flavors of the FloodFill method in that class. Assuming you got the coordinates right (you should check these by using local variables and checking their values in the debugger), you are trying to fill an area bounded by integer color value of 255 with an integer color value of 100. Since the color format is something like ARGB - these values are basically different flavors of transparency.
To make it a bit more realistic you might want to do something like
var x = (int)e.GetCurrentPoint(img).Position.X;
var y = (int)e.GetCurrentPoint(img).Position.Y;
int boundingColor = Colors.Black.AsInt(); // this is an extension method from ColorExtensions that you can use to convert a Jupiter Color type to an int value
int fillColor = Color.FromArgb(255, 255, 0, 0).AsInt();
wb.FloodFill(x, y, boundingColor, fillColor);
To make it useful in a typical scenario you would need a different version of the method though and start by getting the color of the pixel where you start the filling operation:
var pbi = new PixelBufferInfo(wb.PixelBuffer);
int currentColor = pbi[x + y * wb.PixelWidth * 4];
byte threshold = 10;
wb.FloodFillScanlineReplace(x, y, currentColor, fillColor, threshold);
The threshold argument is optional and defines the maximum difference of any of the ARGB byte components of a single pixel can be from the currentColor value to be included in the filled region.
Marked as answer by winnyg10 on 10/15/2013 at 12:41 AM
Oct 15, 2013 at 6:28 AM
Thank you so much for your prompt reply.
I hope I am able to fix it.

Thank you so much again
Oct 15, 2013 at 8:41 AM
Thanks.. It fixed the problem
Coordinator
Oct 15, 2013 at 7:47 PM
Glad I could help!