0

I'm creating an app that allows the user to upload/take an image and then see the rgb code from a pixel in that image. ATM I'm trying to get a pixel rgb code to show in the ui under the image. But I'm struggling to get any data at all. At the moment I click the button and it seems to run, but I have left it for 1 hour and still nothing. I don't think it is accessing any bitmap data. All my code is below. Any help would be greatly appreciated! thanks. EDIT Seems as though I'm creating an empty bitmap image instead of the image.

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
//using System.Windows.Media.Imaging;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.ApplicationModel.Activation;
using Windows.Storage.Streams; // for opening image file
using Windows.UI.Xaml.Media.Imaging;
using Windows.Graphics;
using Windows.Graphics.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace Colour_Find
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page, IFileOpenPickerContinuable  // IFileOpenPickerContinuable
    {

        // global memory for image manipulation
        WriteableBitmap originalImage;

        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }
        private void PickAFileButton_Click(object sender, RoutedEventArgs e)  // Lee
        {

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            // Launch file open picker and caller app is suspended 
            // and may be terminated if required
            openPicker.PickSingleFileAndContinue();


        }


        public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
        {
            StorageFile file = args.Files[0]; // get picked filename
            outputTextBlock.Text = file.Name; // show filename

            if (file != null)
            {
                // Open a stream for the selected file.
                IRandomAccessStream fileStream = await file.OpenReadAsync();

                // Set the image source to the selected bitmap.
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(fileStream);
                pictureBox.Source = bitmapImage;
            }


        }

         public void GetPixel(Image pictureBox)
    {
        //int x, y;

        WriteableBitmap modifiedImage = pictureBox.Source as WriteableBitmap;
        if (modifiedImage == null)
        {
            BitmapSource bs = pictureBox.Source as BitmapSource;
            modifiedImage = new WriteableBitmap(bs.PixelWidth, bs.PixelHeight);
        }


        for (x = 0; x < modifiedImage.PixelWidth; x++)
        {
           for (y = 0; y < modifiedImage.PixelHeight; y++)
           {

                Color pixelColor = modifiedImage.GetPixel(300,300);
                string myString = pixelColor.ToString();
                TextBlock1.Text = myString;
            }
        }
    }
        private void GetPixelBtn_Click(object sender, RoutedEventArgs e)
        {
            GetPixel(pictureBox);
        }


    }
}
  • 1
    What are you expecting it to do? There's a loop that scans through the width & height and gets two values and doesn't do anything with them. – WiredPrairie Apr 08 '15 at 15:55
  • @WiredPrairie I want it to send the rgb value to a textbox, is that possible? – Salman Ahmed Apr 08 '15 at 18:11
  • Yes. If you wanted to. But, you don't need to loop through all pixels to do that, do you? – WiredPrairie Apr 08 '15 at 18:26
  • @WiredPrairie I want to add I'm quite a novice with c#. What do I need to do to be able to do this? – Salman Ahmed Apr 08 '15 at 18:30
  • @SalmanAhmed no where in your code did you assign the output of your `myString` to a `TextBlock`, so ofcourse you wouldn't see anything. You need to learn how to use break points, if you put one in that for loop of yours, you will be able to determine if it was able to get the color at the certain (x, y) by watching the variable. – Chubosaurus Software Apr 08 '15 at 22:18
  • @ChubosaurusSoftware I have used a breakpoint, I don't think it is getting access to the bitmap data – Salman Ahmed Apr 08 '15 at 22:56
  • @WiredPrairie instead of looping would it just be better to state this? `Color pixelColor = modifiedImage.GetPixeli(50, 50);` – Salman Ahmed Apr 08 '15 at 22:56
  • Yes, that would be far better, if you're interested in `50,50`. – WiredPrairie Apr 09 '15 at 00:51
  • @WiredPrairie @ChubosaurusSoftware Okay ,I've got this. Once I click the button it shows the Hex Code "#000000" but even when I change the pixel height and width values it won't change.Any ideas? `public void GetPixel(Image lpictureBox){WriteableBitmap modifiedImage = lpictureBox.Source as WriteableBitmap; if (modifiedImage == null) { BitmapSource bs = lpictureBox.Source as BitmapSource; modifiedImage = new WriteableBitmap(bs.PixelWidth, bs.PixelHeight); }Color pixelColor = modifiedImage.GetPixel(100,100); string myString = pixelColor.ToString(); outputTextBlock.Text = myString;}` – Salman Ahmed Apr 09 '15 at 13:45
  • Please edit the question to include new details. Your `if` block creates an empty `WriteableBitmap` of a particular size. There isn't an image in it. I'd suggest you trace through your code line by line and make sure you understand what each line/function is doing (or not doing). – WiredPrairie Apr 09 '15 at 14:52
  • @WiredPrairie I'm struggling to see how I would load the bitmap image into a writeablebitmap – Salman Ahmed Apr 09 '15 at 16:02

0 Answers0