Changing pixels in Image - c#

I have program in which i want to do some graphic algorithms. I found some code snippets for C# that works on bitmaps. To change pixel or something like that do i have to convert image to bitmap first or is there some methods to change pixels in Image? Something like image1.SetPixel(29,201, color1); ?

You can use the WriteableBitmap class to create a bitmap whose pixels you can modify.
You can then set that Bitmap as the Source for an Image element.
http://books.google.co.uk/books?id=nYl7J7z3KssC&pg=PA416&lpg=PA416&dq=wpf+writeablebitmap&source=bl&ots=V533ojV65x&sig=KJeSje1WCXaS_MT78cR4PPZMFio&hl=en#v=onepage&q=wpf%20writeablebitmap&f=false
http://www.i-programmer.info/programming/wpf-workings/527-writeablebitmap.html?start=1
http://www.nerdparadise.com/tech/csharp/wpfimageediting/
There's a 3rd party library which can help working with WriteableBitmaps more natural i.e. SetPixel and GetPixel methods instead of having to calculate offsets to the pixel data in the buffer.
http://writeablebitmapex.codeplex.com/

Related

How to convert a Grey Scale image to RGB in C#?

How to convert a Grey scale image to RGB format? I have added Aforge library too but unable to define thresholds for coloring.
I'm not understand clearly what do you mean under Convert gray scale image to RGB and suppose that you are trying to revive colors. Unfortunately it's not possible to do it precisely. I mean you can revive colors from grayscale image but this colors would not exactly the colors was befor transforming to gray.
Standard transformation from color to grayscale assumes that every pixel of image transformed by formula:
G(R,G,B) = 0.299*R + 0.587*G + 0.114*B
where R, G and B are RGB values of pixel
As you understand reverse that transformation may have many different results. Look at http://en.wikipedia.org/wiki/Grayscale for more info.
You also may take a look at this project. It implements solution that makes attempt to revitalize colors from grayscale image. But again, take im mind that this colors would not be an original colors
Anton has a good point which is the resulting colored image will not have the colors in the original image.
This is why coloring a grayscale image is called false coloring I believe.
For images with palette, you can just replace the palette.
For images without palettes, you will have to define a formula to work with.
In our application, we used sdk called leadtools that had functions for coloring gray images and we find it to be very helpful.
Using Aforge do this:
var filter = new GrayscaleToRGB();
var RGBBitmap = filter.Apply(AForge.Imaging.Image.Convert16bppTo8bpp(bitmap));
(if you already have a 8bpp bitmap you can leave out the conversion)
GrayscaleToRGB is in AForge.Imaging.Filters

manipulate images at pixel level in C#

I have a ppm file and I figured out how to mirror the image from the middle but I can't figure out how to horizontally flip it.
this is the code for the mirror function:
{R[x][y]=R[WIDTH-x][y];
G[x][y]=G[WIDTH-x][y];
B[x][y]=B[WIDTH-x][y];}}
the horizontal flip function should look almost the same as above. but achieve a flipped image.
Use System.Drawing.Bitmap
GetPixel()
SetPixel()
The MSDN Documentation for System.Drawing.Bitmap

C# Using Emgu to crop an image by pixel color

Hey i was wondering if anyone had any insight on how to crop an image by pixel color using the Emgu Wrapper.
I have it already turn the image grayscale for processing and all the image that i don't need is black. Is there any way to crop these pixels out? Now I'm not talking about making them transparent, i physically want to make the output image smaller.
Thanks!
It's really simple... all you have to do is set your input image ROI property on the non black pixels using a rectangle, then you create the destination image with image equals to ROI size and use Copy() method on your input image choosing as destination your dest image.
HTH, Luca

How can you draw 32bppargb images with the Win32 AlphaBlend function?

I've been fussing with this for the better part of the night, so maybe one of you can give me a hand.
I have found GDI+ DrawImage in C# to be far too slow for what I'm trying to render, and from the looks of forums, it's the same for other people as well.
I decided I would try using AlphaBlend or BitBlt from the Win32 API to get better performance. Anyway, I've gotten my images to display just fine except for one small detail—no matter what image format I use, I can't get the white background to disappear from my (transparent) graphics.
I've tried BMP and PNG formats so far, and verified that they get loaded as 32bppargb images in C#.
Here's the call I'm making:
// Draw the tile to the screen.
Win32GraphicsInterop.AlphaBlend(graphicsCanvas, destination.X, destination.Y, this.TileSize, this.TileSize,
this.imageGraphicsPointer, tile.UpperLeftCorner.X, tile.UpperLeftCorner.Y,
this.TileSize, this.TileSize,
new Win32GraphicsInterop.BLENDFUNCTION(Win32GraphicsInterop.AC_SRC_OVER, 0,
Convert.ToByte(opacity * 255),
Win32GraphicsInterop.AC_SRC_ALPHA));
For the record, AC_SRC_OVER is 0x00 and AC_SRC_ALPHA is 0x01 which is consistent with what MSDN says they ought to be.
Do any of you guys have a good solution to this problem or know a better (but still fast) way I can do this?
Graphics.DrawImage() speed is critically dependent on the pixel format. Format32bppPArgb is 10 times faster than any other one on any recent machine I've tried.
Also make sure you the image doesn't get resized, be sure to use a DrawImage() overload that sets the destination size equal to the bitmap size. Very important if the video adapter's DPI setting doesn't match the resolution of the bitmap.
Have you tried an opacity of just 255 rather than a calculated one?
This blog post describes what you're trying to do:-
http://blogs.msdn.com/andreww/archive/2007/10/10/preserving-the-alpha-channel-when-converting-images.aspx
Critical thing is that he carries out a conversion of the image to make the alpha channel compatible..
Ok. From a pure Win32 perspective:
In order for AlphaBlend to actually alpha blend... it needs the source device context to contain a selected HBITMAP representing an image with 32bpp bitmap with a pre-multiplied alpha channel.
To get a device bitmap with 32bpp you can either call one of the many functions that will create a screen compatible device bitmap and hope like hell the user has selected 32bpp as the desktop bitdepth. OR, ensure that the source bitmap is a DIBSection. Well, the library or framework that is creating it from the loaded image for you.
So, C# is loading your images with 32bpp argb, BUT, how are you converting that C# representation of the bitmap into a HBITMAP? You need to ensure that a DIB Section is being created, not a DDB (or device dependent bitmap), and that the DIB Section is 32bpp.

How to crop part of gif image?

Hi every one,
i wrote some code to crop large image to small one.
Everything is OK but when i tried GIF images i get the following exception
"A Graphics object cannot be created from an image that has an indexed pixel format."
I'm using Graphics class to do this
Graphics gfx = Graphics.FromImage(croppedBitmap)
thank you.
The pixels of GIF images are not RGB values, they are indexes of a color table array stored in the header of the file. Graphic objects only support non-indexed pixel format.
If you want to use the Graphic object, you must convert your image into a non-indexed pixel format.
This is exactly what this guy did:
http://www.codeproject.com/KB/cs/WriteTextToGif.aspx?display=PrintAll

Categories

Resources