5 Replies - 2092 Views - Last Post: 04 January 2008 - 09:48 AM Rate Topic: -----

#1 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

C# image analysing + compiler option in VS

Posted 04 January 2008 - 07:45 AM

Hello, new question :)
I want to analyse the pixels of a PNG picture, i didn't find much information about it. I want to find the color of all pixels in the picture and do some calculations. Who knows something to do this?

thx for all the help!! :P
Jasper

edit: Could I do this with the functions within System.Drawing?

This post has been edited by thejasper: 04 January 2008 - 08:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# image analysing + compiler option in VS

#2 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Re: C# image analysing + compiler option in VS

Posted 04 January 2008 - 08:29 AM

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace WindowsFormsApplication6
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			Bitmap image = new Bitmap("C:\\Documents and Settings\\Beheerder\\Bureaublad\\PNG.png");
			pictureBox1.Image = image;

			BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
			unsafe
			{
				byte* ptr = (byte*)(data.Scan0);
				for (int i = 0; i < data.Height; i++)
				{
					for (int j = 0; j < data.Width; j++)
					{
						// write the logic implementation here
						ptr += 3;
					}
					ptr += data.Stride - data.Width * 3;
				}
			}
		}
	}
}



As you can see I already have some code, but there is 1 error remaining, I should use the compiler option '/unsafe' because I use pointers, but how do I enable this?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C# image analysing + compiler option in VS

Posted 04 January 2008 - 08:30 AM

Yes...more particularly System.Drawing.Imaging:

http://www.c-sharpco...Processing.aspx
http://www.codersour...Processing.aspx
http://www.bobpowell...eprocessing.htm
http://msdn.microsof...tpixeltopic.asp


View Postthejasper, on 4 Jan, 2008 - 10:29 AM, said:

As you can see I already have some code, but there is 1 error remaining, I should use the compiler option '/unsafe' because I use pointers, but how do I enable this?

What's the error?
Was This Post Helpful? 0
  • +
  • -

#4 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Re: C# image analysing + compiler option in VS

Posted 04 January 2008 - 08:36 AM

Error 1 Unsafe code may only appear if compiling with /unsafe D:\Mijn Documenten\Visual Studio 2008\Projects\WindowsFormsApplication6\WindowsFormsApplication6\Form1.cs 22 13 WindowsFormsApplication6
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: C# image analysing + compiler option in VS

Posted 04 January 2008 - 09:12 AM

Seriously, how hard did you try looking for this?

I don't like the lockbits code above, btw. You see it a lot, but unmanagedness of it bothers me. Here's a simple test I wrote:
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApplication1 {
	class Program {
		static void Main(string[] args) {
			Bitmap srcBmp = new Bitmap("logo.gif");
			Bitmap dstBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
			for (int y = 0; y < srcBmp.Height; y++) {
				for (int x = 0; x < srcBmp.Width; x++) {
					Color pix = srcBmp.GetPixel(x, y);
					double greyLevel = pix.R*0.299 + pix.G*0.587 + pix.B*0.144;
					if (greyLevel > 255) { greyLevel = 255; }
					int g = (int)(greyLevel);
					dstBmp.SetPixel(x, y, Color.FromArgb(0,g,0));
				}
			}
			dstBmp.Save("logo.png", ImageFormat.Png);
		}
	}
}


I lifted the greyLevel code from http://www.syncfusio...s/FAQ_c85c.aspx, which has lots of good stuff.

The result, well, I thought our logo wanted some green...

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Re: C# image analysing + compiler option in VS

Posted 04 January 2008 - 09:48 AM

thx, pretty good example to learn from :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1