如何从bmp图像C#获取RGB代码

时间:2015-03-25 13:48:29

标签: c# image windows-phone pixel

我正在努力从上传的图片中获取RGB代码。这是一款Windows手机应用。目前我所能做的就是上传并拍照,但是当用户点击图片并将其显示在图片下方时,我无法理解如何获取rgb代码。以下是我必须允许的图片上传。任何帮助将非常感激!!感谢。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
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 FilePicker81ContinuationManager
{
    /// <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 struct PixelColor
  {
      public byte Blue;
      public byte Green;
      public byte Red;
      public byte Alpha;
  }

  public PixelColor GetPixel(int x, int y, byte[] rawpixel,int width,int hight)
  {
      PixelColor pointpixel;
      int offset = y * width * 4 + x * 4;
      pointpixel.Blue = rawpixel[offset + 0];
      pointpixel.Green = rawpixel[offset + 1];
      pointpixel.Red = rawpixel[offset + 2];
      pointpixel.Alpha = rawpixel[offset + 3];  
      return pointpixel;
  }








    }
}

0 个答案:

没有答案