Click here to Skip to main content
12,298,085 members (52,908 online)
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads

Stats

27.6K views
704 downloads
34 bookmarked
Posted

ImageTraverser

, 19 Apr 2007 MIT
A pointer-based class for retrieving and settings individual image pixels
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using User.Drawing.Imaging;

namespace Demo
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			pictureBox1.AllowDrop = true;
			comboBox1.SelectedIndex = 0;
			Text = "default image (" + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + ")";
		}

		private void pictureBox1_DragEnter(object sender, DragEventArgs e)
		{
			if(e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				e.Effect = DragDropEffects.Copy;
			}
		}

		private void pictureBox1_DragDrop(object sender, DragEventArgs e)
		{
			try
			{
				pictureBox1.Image = Bitmap.FromFile(((string[])e.Data.GetData(DataFormats.FileDrop))[0]);
				Text = System.IO.Path.GetFileName(((string[])e.Data.GetData(DataFormats.FileDrop))[0]) + " (" + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + ")";
			}
			catch
			{
				pictureBox1.Image = pictureBox1.ErrorImage;
				Text = "(no image open)";
			}

			button2.Enabled = pictureBox1.Image != pictureBox1.ErrorImage;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if(openFileDialog1.InitialDirectory == "")
			{
				openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
			}

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					pictureBox1.Image = Bitmap.FromFile(openFileDialog1.FileName);
					Text = System.IO.Path.GetFileName(openFileDialog1.FileName) + " (" + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + ")";
				}
				catch
				{
					pictureBox1.Image = pictureBox1.ErrorImage;
					Text = "(no image open)";
				}
			}

			button2.Enabled = pictureBox1.Image != pictureBox1.ErrorImage;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			button2.Enabled = false;

			using(ImageTraverser it = new ImageTraverser(pictureBox1.Image))
			{
				TimeSpan duration = TimeSpan.Zero;
				DateTime start = DateTime.Now;
				DateTime end = DateTime.Now;

				for(int i = 0; i < numericUpDown1.Value; i++)
				{
					toolStripStatusLabel1.Text = "processing (trial " + (i + 1) + " of " + numericUpDown1.Value + ")";
					Refresh();

					switch(comboBox1.Text)
					{
						case "IT Enumerator":
						{
							start = DateTime.Now;
							foreach(Pixel p in it)
							{
								// invert only the RGB bits, not the Alpha
								it[p.Location] = (int)((uint)~p.ColorValue | 0xFF000000);
							}
							end = DateTime.Now;

							break;
						}

						case "IT Indexer":
						{
							start = DateTime.Now;
							for(int x = 0; x < it.ImageWidth; x++)
							{
								for(int y = 0; y < it.ImageHeight; y++)
								{
									it[x, y] = (int)((uint)~it[x, y] | 0xFF000000);
								}
							}
							end = DateTime.Now;

							break;
						}

						case "IT Pointer":
						{
							unsafe
							{
								start = DateTime.Now;
								int* ptr = it.ImageScan0AsIntPointer;

								for(int y = 0; y < it.ImageHeight; y++)
								{
									for(int x = 0; x < it.ImageWidth; x++)
									{
										it[x, y] = (int)((uint)~*ptr | 0xFF000000);
										ptr++;
									}
								}
							}
							end = DateTime.Now;

							break;
						}

						case "IT Rows":
						{
							start = DateTime.Now;
							for(int y = 0; y < it.ImageHeight; y++)
							{
								int[] row = it.GetRow(y);

								for(int x = 0; x < row.Length; x++)
								{
									it[x, y] = (int)((uint)~row[x] | 0xFF000000);
								}
							}
							end = DateTime.Now;

							break;
						}

						case "IT Array":
						{
							start = DateTime.Now;
							int[][] array = it.ToArray();
							for(int y = 0; y < it.ImageHeight; y++)
							{
								for(int x = 0; x < it.ImageWidth; x++)
								{
									it[x, y] = (int)((uint)~array[y][x] | 0xFF000000);
								}
							}
							end = DateTime.Now;
													

							break;
						}

						case "IT GetPixel":
						{
							start = DateTime.Now;
							for(int x = 0; x < it.ImageWidth; x++)
							{
								for(int y = 0; y < it.ImageHeight; y++)
								{
									it[x, y] = (int)((uint)~it.GetPixel(x, y).ToArgb() | 0xFF000000);
								}
							}
							end = DateTime.Now;

							break;
						}

						case "MS GetPixel":
						{
							using(Bitmap im = it.Image)
							{
								start = DateTime.Now;
								for(int x = 0; x < it.ImageWidth; x++)
								{
									for(int y = 0; y < it.ImageHeight; y++)
									{
										it[x, y] = (int)((uint)~im.GetPixel(x, y).ToArgb() | 0xFF000000);
									}
								}
								end = DateTime.Now;
							}

							break;
						}
					}
					duration += (end - start);

					pictureBox1.Image = it.Image;
				}

				toolStripStatusLabel1.Text = duration.ToString() + " seconds (avg = " + duration.TotalMilliseconds / (int)numericUpDown1.Value + " milliseconds per trial)";
			}

			button2.Enabled = true;
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			pictureBox1.Image = Demo.Properties.Resources.hannah;
			Text = "default image (" + pictureBox1.Image.Width + "x" + pictureBox1.Image.Height + ")";
			button2.Enabled = 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 MIT License

Share

About the Author

Jacob Klint
Software Developer
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 19 Apr 2007
Article Copyright 2007 by Jacob Klint
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid