-
February 4th, 2013, 06:54 PM
#1
display average pixel values
Hi.
I have a project which opens an image in PictureBox1, no problem. But the Public Static Color method at the bottom of the code is not working. I was hoping it would display the average value of the pixels in the image. Can anybody help me to understand why it is not working please? Thank you.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace imageAlign
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap myImage1 = (Bitmap)pictureBox1.Image;
OpenFileDialog ofd1 = new OpenFileDialog();
if (ofd1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(ofd1.FileName);
Image k = Image.FromFile(ofd1.FileName);
Graphics g = Graphics.FromImage(k);
g.FillRectangle(Brushes.White, 155, 235, 120, 70);
g.DrawRectangle(Pens.YellowGreen, 155, 235, 120, 70);
StringFormat sf = (StringFormat)StringFormat.GenericTypographic.Clone();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(DateTime.Now.ToShortDateString(), new
Font("Arial", 14, GraphicsUnit.Point), Brushes.Black, new RectangleF(157, 237, 114, 65), sf);
g.Dispose();
k.Save("C:\\testimage.jpeg", ImageFormat.Jpeg);
Image image_rect = Image.FromFile("C:\\testimage.jpeg");
pictureBox3.Image = image_rect;
//pictureBox3.Height = image_rect.Height;
//pictureBox3.Width = image_rect.Width;
k.Dispose();
}
}
public static Color getDominantColor(Bitmap myImage1)
{
//Used for tally
int i = 0;
int j = 0;
int r = 0;
int g = 0;
int b = 0;
int total = 0;
for (i = 0; i < myImage1.Width; i++)
{
for (j = 0; j < myImage1.Height; j++)
{
Color clr = myImage1.GetPixel(i, j);
r += clr.R;
g += clr.G;
b += clr.B;
total++;
}
}
//Calculate average
r /= total;
g /= total;
b /= total;
Console.WriteLine(j.ToString() + " " + i.ToString());
return Color.FromArgb(r, g, b);
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap myImage2 = (Bitmap)pictureBox2.Image;
OpenFileDialog ofd2 = new OpenFileDialog();
if (ofd2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox2.Image = Image.FromFile(ofd2.FileName);
}
}
}
}
-
February 4th, 2013, 07:05 PM
#2
Re: display average pixel values
Other than that being a slow way to access pixel values (see my blog post on this issue), I see nothing obviously wrong. You will have to be more specific: what do you expect to happen? what is actually happening?
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 4th, 2013, 07:08 PM
#3
Re: display average pixel values
I thought the console would open and display the average values of the image in PictureBox1 in it.
-
February 4th, 2013, 07:13 PM
#4
Re: display average pixel values
Originally Posted by defunktlemon
I thought the console would open and display the average values of the image in PictureBox1 in it.
Post the code that calls getDominantColor() and displays the console window.
-
February 4th, 2013, 07:15 PM
#5
Re: display average pixel values
Ah, I see. So first, while a WinForms app can output to the console [EDIT: I'm actually not sure about this, but probably], it won't open one (unless you ran the program from the console, which is not very common for a graphical application). It would be better to report the average value within the GUI, for example to a label visible on the form. If you are a committed to console output, try running your program directly from a cmd console.
Second, I didn't read the rest of your code initially, but you don't seem to ever call getDominantColor(...). That code will not execute unless the method is called.
Last edited by BioPhysEngr; February 4th, 2013 at 07:18 PM.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 4th, 2013, 09:46 PM
#6
Re: display average pixel values
I would agree with what BioPhysEngr is saying, the method for pixel retrieval or whatever you want to term it is slow. When I was encrypting data to a compressed bitmap in one of my hobby projects, I noticed and did lots of analysis on both the structure of a PNG image file, and a BMP, and the way pixels were stored in an uncompressed bitmap stuck out like a sore thumb. This made me realize that I could read the pixel data by reading the binary stream of the file chunk by chunk. Here's probably what you would want to "adapt" your code towards as well. I haven't done much image modification, but I have done lots of GDI. Here's an example of one of my projects which utilizes the golden ratio to draw a series of lines and circles:
[sigpic][/sigpic]
Microsoft MVP .NET Programming (2012 - Present)
®Crestron DMC-T Certified Automation Programmer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|