Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / WPF

Displaying Raw Images Using WPF

,
Rate me:
Please Sign up or sign in to vote.
4.65/5 (13 votes)
27 Apr 2010CPOL3 min read 94.7K   8.7K   37  
An article describing how to display 8-bit and 16-bit rectangular raw images using WPF.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

// Program to display a 16-bit and 8-bit raw image files.
// Written by Harsha T, Amarnath S, Bangalore, India, April 2010.
namespace Image16Wpf
{
    /// <summary>
    /// Interaction logic for ImageDimensions.xaml
    /// 
    /// This is the dialog to get the width and height of the image from the user.
    /// This dialog displays an initial guess for the width and height based on the 
    /// factors of the total number of pixels. After this, the user can modify these
    /// values. Simple error handling is also provided.
    /// 
    /// </summary>
    public partial class ImageDimensions : Window
    {
        private int numberOfPixels;       
        public int width;
        public int height;
        private List<int> listOfFactors;
        
        /// <summary>
        /// Many raw images are square. So, we first determine whether the given 
        /// number has an integral square, and display it. If the number is not a 
        /// square, we take recourse to factorization.
        /// </summary>
        /// <param name="NOP"></param>
        public ImageDimensions(int NOP)
        {
            InitializeComponent();
            numberOfPixels = NOP;

            tbSize.Text = numberOfPixels.ToString();

            int Factor =0;

            if (SquareRoot(out Factor))
                  width = height = Factor;            
            else
            {
                listOfFactors = Factors(numberOfPixels);
                int noFactors = listOfFactors.Count;
                width = listOfFactors[noFactors - 2];
                height = listOfFactors[noFactors - 1];                
            }

            tbWidth.Text = width.ToString();
            tbHeight.Text = height.ToString();                
        }

        public bool SquareRoot(out int Factor)
        {
            int temp = (int)Math.Floor(Math.Sqrt(numberOfPixels));
            Factor = 0;
            if (temp * temp == numberOfPixels)
            {   Factor = temp;
                return true;
            }
            return false;           
        }

        /// This method is not ours. We acknowledge that 
        /// http://stackoverflow.com/questions/239865/best-way-to-find-all-factors-of-a-given-number-in-c
        /// is the source of this method.             
        public List<int> Factors(int number)
        {
            List<int> factors = new List<int>();
            int max = (int)Math.Sqrt(number);  //round down 
            for (int factor = 1; factor <= max; ++factor)
            { //test from 1 to the square root, or the int below it, inclusive. 
                if (number % factor == 0)
                {
                    factors.Add(factor);
                    if (factor != max)
                    { // Don't add the square root twice!  Thanks Jon 
                        factors.Add(number / factor);
                    }
                }
            }
            return factors;
        }

        private void bnOK_Click(object sender, RoutedEventArgs e)
        {
            int num1 = Convert.ToInt32(tbWidth.Text);
            int num2 = Convert.ToInt32(tbHeight.Text);

            if (num1 * num2 != numberOfPixels)
            {
                MessageBox.Show("The dimensions you entered do not seem to match with the number of pixels. Please re-enter.", 
                    "Image Dimension", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                this.DialogResult = true;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Programming computers since about 1987, my first computer language was Fortran 77. Later I learnt C, C++ and C#. Also programmed a little in VB .Net. Worked with Enterprise Java for a short while. I love watching Kannada movies, and listening to Kannada songs. Currently studying and understanding the Bhagavad Geetha and teaching Sanskrit on YouTube.



Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions