473,547 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image Manipulation

I am using Visual C# 2005 and I need help creating a filter that will
turn a picture black and white. This code I have so far is:

byte red, green, blue, avg, newColor;
int x;
int y;

ImageArray img = new ImageArray(pict ureBoxMainImage .Image);

img.LockImage() ;

for (x = 0; x < img.Width; x++)
for (y = 0; y < img.Height; y++)
{
blue = img[x, y].B;
green = img[x, y].G;
red = img[x, y].R;

avg = (byte)((red + blue + green) / 3);

if (( red > 127) (blue > 127) (green > 127 ))
{
blue = img[255, 255].B;
green = img[255, 255].G;
red = img[255, 255].R;
//newColor = 0;
}
if ((red < 127) (blue < 127) (green < 127))
{
blue = img[0, 0].B;
green = img[0, 0].G;
red = img[0, 0].R;
//newColor = 255;
}

img[x, y] = System.Drawing. Color.FromArgb( avg, avg,
avg);
//img[x, y] = System.Drawing. Color.FromArgb( int red, int
blue, int green);
}

img.UnlockImage ();

labelOutput.Tex t = (img.Width * img.Height).ToS tring();
pictureBoxMainI mage.Image = img.ToBitmap();

}

Please help me with this code as I am a beginner in college and having
a terrible time.

Feb 20 '06 #1
16 1936
See if this helps:

http://www.bobpowell.net

Feb 20 '06 #2
First, it's not completely clear from your code what your algorithm is.
Do you want the colour to be black if the average of red, blue, and
green is < 127, otherwise white?

Anyway, some comments.

First, whenever you first mention the name of a variable, you have to
_declare_ it: say what type it is going to be. After that, the compiler
knows, so you don't have to do it again. So, where you first use blue,
red, and green, you have to say that they're ints:

int blue = img[x, y].B;
int green = img[x, y].G;
int red = img[x, y].R;

Then after that you don't, so where you have

//img[x, y] = System.Drawing. Color.FromArgb( int red, int
blue, int green);

commented out, that line would be wrong, because there's no need to say
that red is an "int" (again) because you've already said so, the first
time you mentioned "red".

Second, in your "if" statements, you have to join conditions with some
sort of operator. You can't just say "if red is less than 127 green is
less than 127...". Even in English that doesn't work: you have to say
whether you mean "if red is less than 127 OR green is less than 127
...." or whether you mean "if red is less than 127 AND green is less
than 127 ...". Therefore, your "if" would need to look like either

if ((red < 127) || (blue < 127) || (green < 127))

or

if ((red < 127) && (blue < 127) && (green < 127))

Either way, I don't think that's what you want. You calculated "avg"
just above (which needs to be declared as an "int", by the way). I
suspect that you want to be testing that, not red, blue, and green
individually.

Feb 20 '06 #3
This did not help me. I just don't understand where I'm going wrong in
getting the picture to turn black and white with the parameters above.
Could I do this with a "foreach" loo/statement?

Feb 20 '06 #4
I am not an expert but what I would do is the following:

I would in the code use Image instead of ImageArray

then use Color pixelColor = img.GetPixel(x, y);
to retrieve the color of position x, y

then instead of
if (( red > 127) (blue > 127) (green > 127 ))
(which can not work without operators between the conditions) simply use

pixelColor.GetB rightness()
(or the average) to see if larger or smaller than 127

In the end use
SetPixel(...)
to change the color of the pixel (in the image)

Concerning the control I am not sure if it will keep the changes (maybe
when it refreshes it reloads the original image again).

For debugging purpose I suggest that you save the image to disk
(with the img.Save(..) method) so that you can check your modified image
on the disk.
If it is black and white and your control is still not showing the image
than the problem is in the refreshing of the control.
good luck !
Nicky schreef:
I am using Visual C# 2005 and I need help creating a filter that will
turn a picture black and white. This code I have so far is:

byte red, green, blue, avg, newColor;
int x;
int y;

ImageArray img = new ImageArray(pict ureBoxMainImage .Image);

img.LockImage() ;

for (x = 0; x < img.Width; x++)
for (y = 0; y < img.Height; y++)
{
blue = img[x, y].B;
green = img[x, y].G;
red = img[x, y].R;

avg = (byte)((red + blue + green) / 3);

if (( red > 127) (blue > 127) (green > 127 ))
{
blue = img[255, 255].B;
green = img[255, 255].G;
red = img[255, 255].R;
//newColor = 0;
}
if ((red < 127) (blue < 127) (green < 127))
{
blue = img[0, 0].B;
green = img[0, 0].G;
red = img[0, 0].R;
//newColor = 255;
}

img[x, y] = System.Drawing. Color.FromArgb( avg, avg,
avg);
//img[x, y] = System.Drawing. Color.FromArgb( int red, int
blue, int green);
}

img.UnlockImage ();

labelOutput.Tex t = (img.Width * img.Height).ToS tring();
pictureBoxMainI mage.Image = img.ToBitmap();

}

Please help me with this code as I am a beginner in college and having
a terrible time.

Feb 20 '06 #5
We are working on filters. I have to create a filter that turns the
picture black & white, blur and inverse.

The instructions for black & white read:

this filter works by taking the average of RGB for a pixel and seeing
if it is above 127 or not. If it is above 127, set the pixel to white.
If it is below 127 set the pixel to black.

I have tried several different things and my picture keeps turning grey.

Feb 20 '06 #6
"Nicky" <ig*******@bell south.net> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I am using Visual C# 2005 and I need help creating a filter that will
turn a picture black and white. This code I have so far is:

<code snipped>


Here's a few things that jump out:

Code:
if (( red > 127) (blue > 127) (green > 127 ))

This isn't legal, and not really what you want. You calculated avg for a
reason, use it for the test.

Code:
blue = img[255, 255].B;
green = img[255, 255].G;
red = img[255, 255].R;

This code gets the pixel at the coordinate (255, 255) (if it even exists),
and assigns the corresponding red, green, and blue parts. What you probably
meant to do here is simply set red, green, and blue to 255.

Code:
if ((red < 127) (blue < 127) (green < 127))

Even if this was somehow legal and did what you wanted, you miss the miss
the case of exactly 127.

Code:
img[x, y] = System.Drawing. Color.FromArgb( avg, avg, avg);

This code will assign some shade of gray to the pixel, not necessarily black
or white.

Here's what I meant in code if I was unclear:

if (avg > 127)
{
blue = 255;
green = 255;
red = 255;
}
if (avg <= 127)
{
blue = 0;
green = 0;
red = 0;
}

img[x, y] = System.Drawing. Color.FromArgb( red, green, blue);

Alternatively, you could do:

img[x, y] = avg > 127 ? System.Drawing. Color.White :
System.Drawing. Color.Black;
Feb 20 '06 #7
Hi James

I tried this:
if (avg > 127)
{
blue = 255;
green = 255;
red = 255;
}
if (avg <= 127)
{
blue = 0;
green = 0;
red = 0;
}
img[x, y] = System.Drawing. Color.FromArgb( red, green, blue);

and I still got Gray. Thanks. I'm just not getting this,.

Feb 20 '06 #8
I am wondering why you are still trying to use unsafe image processing.
Instead, try using GetPixel and SetPixel. Here's an example.

Bitmap oldImage = new Bitmap(pictureB oxMainImage.Ima ge);
Bitmap newImage = new Bitmap(oldImage .Width, oldImage.Height );

for (int x=0; x<pictureBoxMai nImage.Image.Wi dth; x++)
{
for (int y=0; y<pictureBoxMai nImage.Image.He ight; y++)
{
// get the pixel color from the old image and find its average
rgb value
Color color = oldImage.GetPix el(x, y);
int avg = (color.R + color.G + color.B) / 3;

// select a new color for the new image
Color newColor = Color.White; // default to white
if (avg > 127) // set to black if the
average is above 127
{
newColor = Color.Black;
}

// set the new color in the new image
newImage.SetPix el(x, y, newColor);
}
}

// do whatever with your new image...I just set it back to the picture
box
pictureBoxMainI mage.Image = newImage;

Feb 20 '06 #9
Oops! Just reverse those two color statements above.

Color newColor = Color.Black;
if (avg > 127)
{
newColor = Color.White;
}

But, you get the gist. You should really check out the GDI+ library
for your other filters as well. I believe Chris posted a link to one
above. Anyway, hope this helps.

Cheers ;)

Feb 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5428
by: Rune Johansen | last post by:
Hi. I'm doing some image manipulation in an applet using the example code on this page: http://www.akop.org/art/pixels3.htm However, I really want an application rather than an applet, I just can't figure out how to do the image loading and manipulation when it's not from inside an applet. Any hints on the subject are appreciated.
6
1764
by: Giggle Girl | last post by:
Overall Background: I am in charge of migrating an already-made Content Mangement System from ASP to PHP. I do not know PHP -- yet! I am trying to foresee potential issues, and here is one. Image Specific Background: The ASP version of the CMS uses a 3rd party component to accept the uploading of images, and then to resize them so that...
2
9876
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular bitmap file (filename.bmp) I am experiencing some difficulties with writing a bitmap to a http response stream. I load the bitmap from a file. and i...
9
2133
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.? Or are 3rd party solutions needed to do this? I am a php/codfusion programmer and know nothing about asp, so I'm hoping somebody here can help me...
9
3348
by: zacariaz | last post by:
I dont know, and i dont much like javascript, however, i am told that the only way to do want i want to do, is with javascript, so here goes. zoom and cut is the only features i need. 1. the image that need manipulating is places on the server. dont need js for that ;-) 2. the client has the oppotunity to manipulate the image. cut and...
10
3472
by: Pulzar | last post by:
Hi there, I want to show a simple image on a web page, and allow the viewer to select and change one of the colours used in the image, and immediately preview the result. I'd like to keep the image processing away from the server to make the colour selection/preview process quicker, and I also don't want to pre-generate all possible images...
3
26740
by: jon | last post by:
Hello, I've had long standing code that runs in IE, that I'm testing with firefox unsuccessfully now. The problem seems to be that images that I dynamically create don't fire their onload event in firefox. The onload method I assign is never being called. Any ideas why firefox isn't calling this, or what I can do for a workaround? Below...
8
1987
by: shotokan99 | last post by:
i have this situation. i have a query string: http://www.myquerystring.com?x=xxxxx what this url does is it will return or start downloading a .png file. what i wanted to do is trap this png file and manipuate the image like resizing, color...etc. how can i do this?
12
2950
by: laredotornado | last post by:
Hi, I'm using PHP 5. I was wondering given an image, a.jpg, how can I make an image that would look like you slid a white index card (which I have a file, white.jpg with the same dimensions as a.jpg) over a.jpg? Note that you'd be sliding the image from top to bottom or from left to right, but not both. Thanks, let me know if this...
5
1160
by: Jumping Arne | last post by:
I'm going to try to write some imange manipulation code (scaling, reading EXIF and IPTC info) and just want to ask if PIL is *THE* library to use? I looked at <http://www.pythonware.com/products/pil/and noticed that the latest version is from Dec 2006. In my experience that means that either it's abandoned or that it's very good and...
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.