0

我正在创建一个应用程序,允许用户上传/拍摄图像,然后从该图像中的像素查看 rgb 代码。ATM 我正在​​尝试获取像素 rgb 代码以显示在图像下方的 ui 中。但我正在努力获取任何数据。目前我点击按钮,它似乎在运行,但我已经离开了 1 小时,仍然没有。我不认为它正在访问任何位图数据。我所有的代码都在下面。任何帮助将不胜感激!谢谢。编辑好像我正在创建一个空的位图图像而不是图像。

    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);
        }


    }
}
4

0 回答 0