why this program run so slow??

  • Thread starter Thread starter xhy_China
  • Start date Start date
X

xhy_China

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
 
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.
 
Bob Powell said:
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.
 
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.
 
Bob Powell said:
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.
 
Back
Top