Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to process an image in WPF
I found no way to apply getpixel function except for:
public Color GetPixel(BitmapSource bitmap, int x, int y)
{
    CroppedBitmap cb = new CroppedBitmap(bitmap, new Int32Rect(x, y, 1, 1));
    byte[] pixel = new byte[bitmap.Format.BitsPerPixel / 8];
    cb.CopyPixels(pixel, bitmap.Format.BitsPerPixel / 8, 0);
    return Color.FromRgb(pixel[2], pixel[1], pixel[0]);
}

but since this function is run inside a loop it is very slow.
is there any way to make it fast?
Posted
Comments
hzawary 2-Oct-11 15:00pm    
---start by copying the entire data to an array---
Oh my God, Bang's solutions will revolutionize in the Image Processing!!!
Yes, WIC dont know about this solution!!!

I suggest you take another approach instead of having a loop that calls your function, where you each and every time copy the part of your image, as this has a massive overhead.

The approach I would suggest is that you start by copying the entire data to an array, not just one pixel at a time. And then in your loop you index into your array to get the color.

The solution by hzawary I suggest you disregard, the only reason you'll ever use unsafe code would be because you had a 3rd party library which required it. And that library would then also be poorly designed so you shouldn't choose it to start with :)
 
Share this answer
 
Comments
cardano7 2-Oct-11 22:29pm    
well it quite simple
int offset = y*widthOfImage*3+x*3;
byte blue = myArray[offset];
byte green = myArray[offset+1];
byte red = myArray[offset+2];
Simon Bang Terkildsen 3-Oct-11 0:09am    
Seems I f***ed up when replying to your comment, and somehow replace your comment with my answer. In any case there is my answer :)
Search the articles for "Image processing for dummies". You'll find a series of articles that demonstrates how to access the Bitmap data directly, without using GetPixel.
 
Share this answer
 
I also very research for this problem that find this link:
wpf-image-processing
 
Share this answer
 
GetPixel is a wrapper around the GDI drawing components.
Calling the GDI methods directly[^] could help improve speed.
 
Share this answer
 
v2
Comments
cardano7 2-Oct-11 7:58am    
Thanks
but WPF doesn't support many features of WinForm.
what to do with this line?
IntPtr hDC = GetDC(control.Handle);
control.Handle is not recognized
Philippe Mori 2-Oct-11 21:21pm    
Help reduce speed... LOL. If speed is reduced, then it will be even slower!!!
Abhinav S 3-Oct-11 0:43am    
Fixed. Thanks for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900