Created
November 28, 2012 23:05
-
-
Save FabienArcellier/4165396 to your computer and use it in GitHub Desktop.
Class in C# to extract pixel map from a specific channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
namespace PictureHistogram | |
{ | |
/// <summary> | |
/// Class that expose method to get pixel map from a specific channel | |
/// </summary> | |
class BitmapInfo | |
{ | |
private Bitmap m_bitmap; | |
public enum Channel | |
{ | |
Red, | |
Green, | |
Blue, | |
Alpha | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="bitmap">Instance of bitmap object</param> | |
public BitmapInfo(Bitmap bitmap) | |
{ | |
this.m_bitmap = bitmap; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="path">Path of bitmap picture</param> | |
public BitmapInfo(string path) | |
{ | |
this.m_bitmap = new Bitmap(path); | |
} | |
/// <summary> | |
/// Extract a map of picture (2D Array) for a specific channel | |
/// </summary> | |
/// <param name="channel_index">Channel to extract</param> | |
/// <returns>Pixel Map</returns> | |
public byte[,] pixelMap(Channel channel_index) | |
{ | |
int size = this.m_bitmap.Width * this.m_bitmap.Height; | |
int picture_width = this.m_bitmap.Width; | |
int picture_height = this.m_bitmap.Height; | |
byte[,] pixels_map = new byte[picture_width, picture_height]; | |
for (int i = 0; i < picture_height; i++) | |
{ | |
for (int j = 0; j < picture_width; j++) | |
{ | |
Color color = this.m_bitmap.GetPixel(j, i); | |
byte color_intensity = 0; | |
switch (channel_index) | |
{ | |
case Channel.Red: | |
color_intensity = color.R; | |
break; | |
case Channel.Green: | |
color_intensity = color.G; | |
break; | |
case Channel.Blue: | |
color_intensity = color.B; | |
break; | |
case Channel.Alpha: | |
color_intensity = color.A; | |
break; | |
} | |
pixels_map[j, i] = color_intensity; | |
} | |
} | |
return pixels_map; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
namespace PictureHistogram | |
{ | |
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
static void Main() | |
{ | |
Bitmap bitmap = new Bitmap(500, 500); | |
bitmap.SetPixel(0, 2, Color.Azure); | |
BitmapInfo bitmap_info = new BitmapInfo(bitmap); | |
byte[,] array_red = bitmap_info.pixelMap(BitmapInfo.Channel.Red); | |
byte[,] array_green = bitmap_info.pixelMap(BitmapInfo.Channel.Green); | |
byte[,] array_blue = bitmap_info.pixelMap(BitmapInfo.Channel.Blue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment