Click here to Skip to main content
15,886,799 members
Home / Discussions / C#
   

C#

 
Questionreading pixel's value in an image Pin
wasifmuneer8-May-09 8:53
wasifmuneer8-May-09 8:53 
AnswerRe: reading pixel's value in an image Pin
molesworth8-May-09 9:05
molesworth8-May-09 9:05 
GeneralRe: reading pixel's value in an image Pin
Dan Neely8-May-09 9:27
Dan Neely8-May-09 9:27 
AnswerRe: reading pixel's value in an image Pin
Dave Kreskowiak8-May-09 9:14
mveDave Kreskowiak8-May-09 9:14 
AnswerRe: reading pixel's value in an image Pin
terradtc8-May-09 10:50
terradtc8-May-09 10:50 
GeneralRe: reading pixel's value in an image Pin
wasifmuneer8-May-09 12:28
wasifmuneer8-May-09 12:28 
GeneralRe: reading pixel's value in an image Pin
Christian Graus8-May-09 14:53
protectorChristian Graus8-May-09 14:53 
GeneralRe: reading pixel's value in an image [modified] Pin
terradtc9-May-09 0:40
terradtc9-May-09 0:40 
You'll want to do some filtering and maybe change the average a bit since this is very sensitive to minor color differences on space images but the basic idea here works. It also doesn't deserve a beauty prize but for 10 minutes of work, I wasn't expecting it to. Furthermore this code will fail on any image that's not 24 bits per pixel.

class StarGazer
{
// this function works on pictures of up to 1.3 billion pixels in width
public bool[,] toBool(Bitmap bitmap)
{
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
bool[,] boolBitmap = new bool[ bitmap.Height,bitmap.Width];
unsafe
{//first we calculate the average value of a pixel
byte* ptr = (byte*)(bmpData.Scan0);
ptr--;
ulong overallAvg = 0UL;
for(int y = 0;y<bitmap.Height;y++)
{
ulong lineAvg = 0UL;
for (int x = 0; x < bitmap.Width; x++)
{
lineAvg+= *(++ptr);
lineAvg+= *(++ptr);
lineAvg+= *(++ptr);
}
overallAvg += lineAvg /(ulong) bitmap.Width;
}
overallAvg /= (ulong)bitmap.Height;
int average = (int)overallAvg;
//we've got the average value a collection of three pixels needs to have to become an object
ptr = (byte*)(bmpData.Scan0);
ptr--;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
boolBitmap[y, x] = (*(++ptr) + *(++ptr) + *(++ptr) > average);
}
}
}
bitmap.UnlockBits(bmpData);
return boolBitmap;
}

public Bitmap boolBitmap(bool[,] boolBitmap)
{
Bitmap temp = new Bitmap(boolBitmap.GetLength(1),boolBitmap.GetLength(0));
System.Drawing.Imaging.BitmapData bmpData = temp.LockBits(new Rectangle(0, 0, boolBitmap.GetLength(1), boolBitmap.GetLength(0)), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int bytes = (temp.Width * temp.Height) * 3;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
ptr--;
int x = 0;
while (x < temp.Width * temp.Height)
{

*(++ptr) = *(++ptr) = *(++ptr) = (byte)(boolBitmap[ x /temp.Width,x % temp.Width] ? 255 : 0);
x++;
}
}
temp.UnlockBits(bmpData);
return temp;
}
}

modified on Saturday, May 9, 2009 6:54 AM

Questionmsgbox show for a set time Pin
Sokka938-May-09 8:48
Sokka938-May-09 8:48 
AnswerRe: msgbox show for a set time Pin
Dave Kreskowiak8-May-09 9:00
mveDave Kreskowiak8-May-09 9:00 
AnswerRe: msgbox show for a set time Pin
Ravi Bhavnani8-May-09 11:31
professionalRavi Bhavnani8-May-09 11:31 
AnswerRe: msgbox show for a set time Pin
Atif Shahbaz8-May-09 21:05
Atif Shahbaz8-May-09 21:05 
AnswerRe: msgbox show for a set time Pin
Rajdeep.NET is BACK8-May-09 21:32
Rajdeep.NET is BACK8-May-09 21:32 
GeneralRe: msgbox show for a set time Pin
Sokka938-May-09 22:43
Sokka938-May-09 22:43 
QuestionSearching randomly Pin
Rajdeep.NET is BACK8-May-09 8:39
Rajdeep.NET is BACK8-May-09 8:39 
AnswerRe: Searching randomly Pin
fly9048-May-09 9:02
fly9048-May-09 9:02 
GeneralRe: Searching randomly Pin
Luc Pattyn8-May-09 9:17
sitebuilderLuc Pattyn8-May-09 9:17 
GeneralRe: Searching randomly Pin
fly9048-May-09 9:23
fly9048-May-09 9:23 
GeneralRe: Searching randomly Pin
Luc Pattyn8-May-09 9:39
sitebuilderLuc Pattyn8-May-09 9:39 
GeneralRe: Searching randomly Pin
fly9048-May-09 9:53
fly9048-May-09 9:53 
GeneralRe: Searching randomly Pin
jas0n2310-May-09 8:25
jas0n2310-May-09 8:25 
AnswerRe: Searching randomly Pin
Dan Neely8-May-09 11:00
Dan Neely8-May-09 11:00 
AnswerRe: Searching randomly Pin
terradtc8-May-09 11:38
terradtc8-May-09 11:38 
QuestionClickOnce Query String Problem Pin
mishigun8-May-09 7:49
mishigun8-May-09 7:49 
QuestionHow to parse JSON response string Pin
yogesh_softworld1238-May-09 6:04
yogesh_softworld1238-May-09 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.