8 Replies - 1635 Views - Last Post: 30 December 2008 - 12:58 AM Rate Topic: -----

#1 nettaimookki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-December 08

problem in storing the green color value of bmp image

Posted 28 December 2008 - 05:47 AM

here i am reading the green value of the bitmap image and displaying it in the rich text box.but it is taking too much time to read the values.how to avoid this problem..

namespace WindowsFormsApplication5
{
	public partial class Form1 : Form
	{	 
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Bitmap bitmap = new Bitmap("C:\\babes.bmp");
			int width = bitmap.Width;
			int height = bitmap.Height;

			using (FileStream fs = f.Create())
			{
				int i, j;
				for (i = 0; i < width; i++)
				{
					for (j = 0; j < height; j++)
					{

						Color pixelColor = bitmap.GetPixel(i, j);
						int  m= pixelColor.G;
						richTextBox1.Text += m + "  ";
   
					}
				}
				   
			}
	
		 }
	  
	  }
 }


This post has been edited by nettaimookki: 28 December 2008 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem in storing the green color value of bmp image

#2 nettaimookki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-December 08

Re: problem in storing the green color value of bmp image

Post icon  Posted 28 December 2008 - 05:51 AM

here i am reading the green value of the bitmap image and displaying it in the rich text box.but it is taking too much time to read the values.how to avoid this problem..

namespace WindowsFormsApplication5
{
	public partial class Form1 : Form
	{

		
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			int c = 0;

			Bitmap bitmap = new Bitmap("C:\\babes.bmp");
			int width = bitmap.Width;
			int height = bitmap.Height;

			using (FileStream fs = f.Create())
			{
				

				int i, j;
				for (i = 0; i < 10; i++)
				{
					for (j = 0; j < 50; j++)
					{

						Color pixelColor = bitmap.GetPixel(i, j);
						int  m= pixelColor.G;
						richTextBox1.Text += m + "  ";
				  

					}
				}
				   
			}
	
		 }
	  
	  }
 }


Was This Post Helpful? 0
  • +
  • -

#3 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: problem in storing the green color value of bmp image

Posted 29 December 2008 - 12:56 AM

It will actually take a longer time to perform the operation you mentioned with large images. If the main problem is that you can't do anything with the application while the pixel data is being processed, I would recommend transferring the process in another thread, so the main thread won't be loaded with another process, that requires a lot of system resources.

To execute the process in another thread, add the following code before the form declaration (public Form1()):

delegate void SetListCallback(string text);
private Thread imageProcessing = null;



Then, add the declaration for the new thread, that will actually handle the processing of the image (extract the pixel information):

		private void processImage()
		{
			Bitmap myBitmap = new Bitmap ("C:\\Images\\myImage.jpg");
			for (int i = 0; i < myBitmap.Width; i++)
			{
				for (int j = 0; j < myBitmap.Height; j++)
				{
					Color myColor = myBitmap.GetPixel(i, j);
					int myPixel = myColor.G;
					addItems(myPixel.ToString());
				}
			}
		}



Now, add this code for the event handler that will start the process of information extraction:

this.imageProcessing = new Thread(new ThreadStart(this.processImage));
this.imageProcessing.Start();



Now, add this piece of code (method) that will handle the cross-thread operations and will add the pixel information to a ListBox control (you can easily adapt this part to your TextBox control - it is just easier to view the information in a ListBox):

		private void addItems(string text)
		{
			if (listBox1.InvokeRequired)
			{
				SetListCallback myCallback = new SetListCallback(addItems);
				try
				{
					this.Invoke(myCallback, new object[] { text });
				}
				catch
				{
					this.imageProcessing.Abort();
				}
			}
			else
			{
				listBox1.Items.Add(text);
			}
		}



This will pretty much solve the problem of 'freezing' in the main thread in your situation (if that was the problem).
Was This Post Helpful? 1
  • +
  • -

#4 nettaimookki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-December 08

Re: problem in storing the green color value of bmp image

Posted 29 December 2008 - 10:01 PM

thanks man...because of u i have understood the thread concept...but i don't know what is the reference for SetListCallback method......i am recieving the error messages.


Error 1 The type or namespace name 'SetListCallback' could not be found (are you missing a using directive or an assembly reference?) C:\Users\We are Masters\AppData\Local\Temporary Projects\coreidea\Form1.cs 49 17 coreidea

Error 2 The type or namespace name 'SetListCallback' could not be found (are you missing a using directive or an assembly reference?) C:\Users\We are Masters\AppData\Local\Temporary Projects\coreidea\Form1.cs 49 50 coreidea


Error 3 The best overloaded method match for 'System.Windows.Forms.Control.Invoke(System.Delegate, params object[])' has some invalid arguments C:\Users\We are Masters\AppData\Local\Temporary Projects\coreidea\Form1.cs 52 21 coreidea

Error 4 Argument '1': cannot convert from 'SetListCallback' to 'System.Delegate' C:\Users\We are Masters\AppData\Local\Temporary Projects\coreidea\Form1.cs 52 33 coreidea

This post has been edited by nettaimookki: 29 December 2008 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: problem in storing the green color value of bmp image

Posted 29 December 2008 - 10:22 PM

SetListCallback is the delegate that allows you to make asynchronous calls to change the property of the control (ListBox in my code), that is in another thread (basically, allows the cross-thread property change).

Seems like you are getting the error messages because you declared the SetListCallback in the wrong place. Make sure that it is declared in the same place as I will show here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace MyCSharpProject
{
	
	public partial class Form1 : Form
	{
		delegate void SetListCallback(string text);
		private Thread imageProcessing = null;

	   // The rest of the code.



This post has been edited by Core: 29 December 2008 - 10:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 nettaimookki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-December 08

Re: problem in storing the green color value of bmp image

Posted 29 December 2008 - 10:58 PM

here is the program.......i am recieving the same error messages........


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;


namespace coreidea
{
	public partial class Form1 : Form
	{
		delegate void SetListCallBack(string text);
		private Thread imageProcessing = null;


		public Form1()
		{
			InitializeComponent();

			
		}
		private void processImage()
		{
			Bitmap myBitmap = new Bitmap("C:\\Images\\myImage.jpg");
			for (int i = 0; i < myBitmap.Width; i++)
			{
				for (int j = 0; j < myBitmap.Height; j++)
				{
					Color myColor = myBitmap.GetPixel(i, j);
					int myPixel = myColor.G;
					addItems(myPixel.ToString());
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.imageProcessing = new Thread(new ThreadStart(this.processImage));
			this.imageProcessing.Start();

		}
		private void addItems(string text)
		{
			if (listBox1.InvokeRequired)
			{
				SetListCallback myCallback = new SetListCallback(addItems);
				try
				{
					this.Invoke(myCallback, new object[] { text });
				}
				catch
				{
					this.imageProcessing.Abort();
				}
			}
			else
			{
				listBox1.Items.Add(text);
			}
		}



	}
}



Was This Post Helpful? 0
  • +
  • -

#7 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: problem in storing the green color value of bmp image

Posted 29 December 2008 - 11:38 PM

C# is a case-sensitive language, so SetListCallBack is not the same as SetListCallback. You declared the delegate as SetListCallBack but later calling it through SetListCallback. That is the source of the errors in your situation.
Was This Post Helpful? 1
  • +
  • -

#8 nettaimookki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-December 08

Re: problem in storing the green color value of bmp image

Posted 30 December 2008 - 12:45 AM

yeah.....i got the output..thanks man....u have done a great job man...
Was This Post Helpful? 0
  • +
  • -

#9 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: problem in storing the green color value of bmp image

Posted 30 December 2008 - 12:58 AM

Glad it worked. Congratulations!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1