How to crop part of gif image? - c#

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

Related

C# Fast load Bitmap into PictureBox

I receve Bitmap image from a camera at 30 fps, and I need to display all images in a pictureBox.
The problem is that the PictureBox is very slow!
I have try to implement a custom PictureBox with DoubleBuffer enabled but the problem is not resolved.
Do you have a custom PictureBox or an user control or a solution that can display the image faster?
Additional information:
The image resolution is 2048x1088 with 256 graylevel (8bit image).
I use AForge.NET for elaborate the images.
Thank you
That image gets expensive to draw when it has to be resized to fit the PB's client area. Which is very likely in your case because your images are pretty large. It uses a high-quality bi-cubic filter to make the resized image look good. That's pretty expensive, albeit that the result is good.
To avoid that expense, resize the image yourself before assigning it to the Image property. Make it just as large as the PB's ClientSize.
That's going to make a big difference in itself. The next thing you can do is to create the scaled bitmap with the 32bppPArgb pixel format. It's the format that's about 10 times faster then any other because it matches the video adapter on most machines so no pixel format conversions are necessary.

How to show a YCbCr image using Emgu in C#?

I got a bitmap as a source;
I created a Emgu image with Image<Bgr,Byte> img = new Image<Bgr,Byte>(bmp);
I converted it to a YCbCr image using Image<Ycc,Byte> YCB = img.Convert<Ycc,Byte>();
I dragged a imagebox from the toolbox and assigned it with YCB -----> imagebox1.Image=YCB;
but the result shows the image in RGB format just like source bitmap
I don't understand where went wrong
Could someone give me some clues?
Have you made any alterations to YCB? If you simply display it then it will look identical to the original.
If you right click on your imagebox when running your program and select property it will tell you the type of image and show you the data held within the YCB image this should be different from your original. Alternatively just show 1 channel of your image matrix so for your BGR show the blue colour this will obviously be displayed as a single colour grayscale image. Now for the YCB show the Luma channel again this will be displayed as a single colour grayscale image. You will notice a slight change between them as the luma represents the luminance of all 3 colour spectrums.
CvInvoke.cvShowImage("Blue", img [0]);
CvInvoke.cvShowImage("Luma", YCB[0]);
If you want to see a greater difference multiply the Luma by 2 and you have a completely different image.
YCB[0] *= 2;
CvInvoke.cvShowImage("Luma Change", YCB);
The CvInvoke.cvShowImage() method is a great tool for debugging your code and seeing what your code is doing to images step by step.
For the benefits of others Ycc is YCbCr colour space: http://en.wikipedia.org/wiki/YCbCr
Cheers,
Chris

Changing pixels in Image

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/

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 I check the color depth of a Bitmap?

I'm working on an application that prints a folder of image files, including JPEG and TIFF. The TIFF images are usually Black and White (1bpp).
After loading the image, I want to determine if the image is Color or B&W or Grayscale so I can send the image to the right printer (color printer or black and white printer).
I'm using the Bitmap constructor image = new Bitmap(filename); to load the image.
EDIT: The answer to check the pixel depth is great for B&W. Any ideas on checking if the image is grayscale without iterating through every pixel?
Just check this property
image.PixelFormat
It will match one of the values in System.Drawing.Imaging.PixelFormat
Though you would want to send more than just Black & White to the B&W printer, you should also send any gray scales there as well.
The proper way to check this is:
For JPEG files you should check the appropriate properties using the PropertyItems collection of the Bitmap. This may contain the appropriate EXIF tags to help determine the bit depth. The next step would be to parse the JPEG header and look for the 'start of frame' marker and then the number of components in the image.
The final method is to load the JPEG into a Bitmap object and compare the number of pixels with the forumla (width * height * bytes_per_pixel). So if you load the bitmap and number of bytes of actual raw data is equal to (width * height) then you know it's a safe bet that the image has 1 byte per pixel and as such is grayscale.
The last thing you'll want to check is the PixelFormat of the bitmap itself.
For the TIFF file format you should do the same thing using the PropertyItems collection and check the appropriate tag mentioned in the spec. If these fail then do the image byte comparison and finally use the PixelFormat property as a last resort.

Categories

Resources