WPF Image, changing color of pixels - wpf

I have a problem. I need to fill (or repaint) some pixels at image, stored in Image control. This is a png image. I mean, that all black pixels should be filled with, for example, red color. How can I do this? I thought I can access directly to pixels and using XOR change special bits, but I don't know how to do this. Or maybe there is an easier way?

The GetPixel and SetPixel methods should work for what you need.
This answer has a code sample that you should be able to adopt for your use.

Related

question about gif rendering with transparency

I managed to show a gif almost perfectly with the disposal methods and everything, but I have a big trouble with the transparent color.
I saw other apps that draw a previous bmp on the next successfully.
I don't know how to do it.
I think I need to use transparentblt but I don't know how to set the transparent color. I work with 32-bit bitmaps, the transparent color of each frame I take correctly from the gif frame palette,
but I don't know if I should and how to set the alpha channel of the color.
In the beginning I tried to replace each transparent color with 0x80000000 and use the bitmap with both bitblt/transparentblt,
but no luck.
Maybe my problem is only the alpha channel of the transparent color? What value should it get?
I actually think my problem is only drawing bitmap on another with transparent color.
Since I have the transparent color I'll think I can do it myself
even if I'll have to transfer the bytes byte by byte.
I still don't know whether TransparentBlt API can do it though.

How to work with pixels in WP7 Silverlight?

I have an image, I want to get a pixel color, replace a pixel color with another, and other operations on pixels.
Is there a way to do that? And what libraries are needed?
Have a look at the WriteableBitmapEx library. This will allow you to manipulate pixels directly.

Rectangular shaped Gradient Fill

I'm currently looking to achieve a gradient effect a bit like the rectangle in http://pjnicholson.com/Fireworks/fillgradients.htm
If I compromise a little I can get close to this using RadialGradientBrush... but is there any (not too painful) way to achieve the rectangular effect?
Use an ImageBrush instead and use this image (or a similar image generated using some image editor) for the background of your rectangle.
One solution a colleague and I came with was to derive a new Panel that used a WriteableBitmap as the source for its background.
The panel will give you the dimensions you need to make your WriteableBitmap. Using whatever algorithm you want you can fill it appropriately. In our case, we needed a radial or cone gradient, but the same concept applies.
Additionally, you can create several properties on your new control to specify the colors for the gradient. We adapted a LinearGradientBrush for our needs, but if you're working on just two colors, simple properties may suffice.
I don't have the code handy but will try to find it and post an update later. But the above should get you going.

Make part of an image transparent

I want to put an image on a button, but I want part of the image to be transparent. How do I do this?
Try the Image.OpacityMask property. You can give it a brush that specifies the region you want to be transparent.
EDIT: From MSDN:
There is no direct support for
color-keying a bitmap in WPF.
However, it is fairly easy to
implement on your own. Dwayne has
implemented a ColorKeyBitmap on his
blog:
http://blogs.msdn.com/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
I believe it links to the code on
Codeplex as well. You could also
accomplish this simply by reading your
bitmap into system memory, iterating
through all the pixels and setting
their values yourself, and
constructing a new bitmap out of that
array.
Use a paint program (I use Paint.Net) to change the area you want transparent to an alha=0 color. Then save the image (mine was JPG) as a PNG. Seemed to work fine for me in the WPF Image control.

Change pixels to fill with color in a WPF image

I'm trying to fill an area of an image in WPF. Something similar to the bucket in Paint. How should I do this? I'm thinking of getting the pixel color under the mouse, and change all the pixels with the same color near. Is there a simpler way?
What should I use? WriteableBitmap?
Thank you
Yes, WriteableBitmap is the best way to go on this one IMHO.

Resources