Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,167 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




C# image analysing + compiler option in VS

 
Reply to this topicStart new topic

C# image analysing + compiler option in VS, special class for it?

thejasper
4 Jan, 2008 - 06:45 AM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
Hello, new question smile.gif
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!! tongue.gif
Jasper

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

This post has been edited by thejasper: 4 Jan, 2008 - 07:35 AM
User is offlineProfile CardPM
+Quote Post

thejasper
RE: C# Image Analysing + Compiler Option In VS
4 Jan, 2008 - 07:29 AM
Post #2

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
CODE
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?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C# Image Analysing + Compiler Option In VS
4 Jan, 2008 - 07:30 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Yes...more particularly System.Drawing.Imaging:

http://www.c-sharpcorner.com/UploadFile/Sh...Processing.aspx
http://www.codersource.net/csharp_image_Processing.aspx
http://www.bobpowell.net/imageprocessing.htm
http://msdn.microsoft.com/library/default....tpixeltopic.asp


QUOTE(thejasper @ 4 Jan, 2008 - 10:29 AM) *



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?
User is offlineProfile CardPM
+Quote Post

thejasper
RE: C# Image Analysing + Compiler Option In VS
4 Jan, 2008 - 07:36 AM
Post #4

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
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

User is offlineProfile CardPM
+Quote Post

baavgai
RE: C# Image Analysing + Compiler Option In VS
4 Jan, 2008 - 08:12 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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:
CODE

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.syncfusion.com/FAQ/WindowsForms/FAQ_c85c.aspx, which has lots of good stuff.

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





Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

thejasper
RE: C# Image Analysing + Compiler Option In VS
4 Jan, 2008 - 08:48 AM
Post #6

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
thx, pretty good example to learn from smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:13AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month