473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

why this program run so slow??

I wrote a program to convert a bitmap to gray(in c#) . But why it runs
so slow?? More slower than I wrote in Delphi?? They have the same
algorithm.

Bitmap openBitmap = new Bitmap(openBitmapDialog.FileName);

Color oldColor = new Color();
int grayValue;
for (int i = 0; i < openBitmap.Width; i++)
for (int j = 0; j < openBitmap.Height; j++)
{
oldColor = openBitmap.GetPixel(i, j);
grayValue = (int)(oldColor.R * 0.299 +
oldColor.G * 0.587 + oldColor.B * 0.114);
oldColor = Color.FromArgb(grayValue, grayValue,
grayValue);
openBitmap.SetPixel(i, j, oldColor);
}
sourcePictureBox.Image = openBitmap;
Thx for your
replies

Dec 3 '06 #1
5 5471
They do *not* have "the same algorithm." You are using the .Net
System.Drawing.Bitmap.GetPixel method to work with the individual pixels in
your image. This is what is slowing you down. See:

http://www.bobpowell.net/lockingbits.htm

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.

"xhy_China" <xu*********@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>I wrote a program to convert a bitmap to gray(in c#) . But why it runs
so slow?? More slower than I wrote in Delphi?? They have the same
algorithm.

Bitmap openBitmap = new Bitmap(openBitmapDialog.FileName);

Color oldColor = new Color();
int grayValue;
for (int i = 0; i < openBitmap.Width; i++)
for (int j = 0; j < openBitmap.Height; j++)
{
oldColor = openBitmap.GetPixel(i, j);
grayValue = (int)(oldColor.R * 0.299 +
oldColor.G * 0.587 + oldColor.B * 0.114);
oldColor = Color.FromArgb(grayValue, grayValue,
grayValue);
openBitmap.SetPixel(i, j, oldColor);
}
sourcePictureBox.Image = openBitmap;
Thx for your
replies

Dec 3 '06 #2
GetPixel is slow.

The GDI+ FAQ has several different methods of accomplishing this goal. The
most efficient method is to draw the bitmap with a color transform that
removes the image colour saturation.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"xhy_China" <xu*********@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>I wrote a program to convert a bitmap to gray(in c#) . But why it runs
so slow?? More slower than I wrote in Delphi?? They have the same
algorithm.

Bitmap openBitmap = new Bitmap(openBitmapDialog.FileName);

Color oldColor = new Color();
int grayValue;
for (int i = 0; i < openBitmap.Width; i++)
for (int j = 0; j < openBitmap.Height; j++)
{
oldColor = openBitmap.GetPixel(i, j);
grayValue = (int)(oldColor.R * 0.299 +
oldColor.G * 0.587 + oldColor.B * 0.114);
oldColor = Color.FromArgb(grayValue, grayValue,
grayValue);
openBitmap.SetPixel(i, j, oldColor);
}
sourcePictureBox.Image = openBitmap;
Thx for your
replies

Dec 3 '06 #3
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.netwrote:
>The GDI+ FAQ has several different methods of accomplishing this goal. The
most efficient method is to draw the bitmap with a color transform that
removes the image colour saturation.
That's not true. The most efficient way is to use win32 graphics
calls. It works out about 4x faster than the colour-transform
suggested in the gdi+ faq.

I wrote some C# code to convert to monochrome using win32 here:
http://www.wischik.com/lu/Programmer/1bpp

Incidentally, this code returns a "true" 1bpp monochrome bitmap,
rather than the GDI+ way that returns a 32bpp bitmap that only happens
to use black and white.

--
Lucian
Dec 3 '06 #4
Hi Lucian.
There's a difference between a grayscale and a 1 bit per pixel file.

The 1Bpp code in the GDI+ FAQ uses the Marshal class because I waned to show
exact equivalent code for C# and VB users.

I have much faster versions of the 1bpp code that use C# pointers

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Lucian Wischik" <lu***@wischik.comwrote in message
news:sq********************************@4ax.com...
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.netwrote:
>>The GDI+ FAQ has several different methods of accomplishing this goal. The
most efficient method is to draw the bitmap with a color transform that
removes the image colour saturation.

That's not true. The most efficient way is to use win32 graphics
calls. It works out about 4x faster than the colour-transform
suggested in the gdi+ faq.

I wrote some C# code to convert to monochrome using win32 here:
http://www.wischik.com/lu/Programmer/1bpp

Incidentally, this code returns a "true" 1bpp monochrome bitmap,
rather than the GDI+ way that returns a 32bpp bitmap that only happens
to use black and white.

--
Lucian

Dec 3 '06 #5
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.netwrote:
>The 1Bpp code in the GDI+ FAQ uses the Marshal class because I waned to show
exact equivalent code for C# and VB users.
I have much faster versions of the 1bpp code that use C# pointers
The code I posted is 4 times as fast as your C# pointer version. Go to
http://www.wischik.com/lu/Programmer/1bpp
and download the sample app, which includes both your C# pointer
method and my calls to win32-library-functions. (Sorry, I was wrong, I
didn't do a speed test against your colour-matrix version. But since
you say it's slower there's not much point.)
>There's a difference between a grayscale and a 1 bit per pixel file.
Using the gdi technique I posted, converting to greyscale is pretty
much the same as converting to 1bpp. The only difference is that for
greyscale you create your target bitmap with 256 palette entries,
whereas for 1bpp you create it with 2 palette entries. Once you've
created it, though, it's exactly the same BitBlt routine to convert to
the target format.
--
Lucian
Dec 3 '06 #6

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

Similar topics

2
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File...
22
by: Brad Tilley | last post by:
Is it possible to write a file open, then read program in C and then call the C program from a Python script like this: for root, files, dirs in os.walk(path) for f in files: try:...
11
by: Brett | last post by:
Hi. I wrote a program in C that spends most of its time doing integer arithmetic (on a data set loaded at run time), with a negligible amount of I/O. I compiled it with lcc-win32 as a console...
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
10
by: An Ony | last post by:
Hi, I made a console program in C# and it works for me. Now I sent it to someone else and he can't run it. Program error or something. His OS is in Swedish. What could this be? Does he have to...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
23
by: PeterOut | last post by:
If I had code like this. unsigned short usLimit=10 int a, i; for (i=0; i<(int)usLimit; ++i) { a=(int)usLimit; }
4
by: driplet | last post by:
Hi there: I made a very samll windows application program in C++. I found my computer becomes very slow after this program loaded. I checked windows task manager and found this small program takes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.