How to use C # To degrayscale an image

Source: Internet
Author: User

A grayscale image is used to discard all the color information of the image and display the 24-Bit Bitmap information in 8 bits. The grayscale image has a total gray level of 256, that is to say, to convert a point of a 24-Bit Bitmap (such as (255,255,255) to 255, so the coefficients multiplied by the values of R, G, and B are 1.
The pseudo statement can be used to represent the following:

Public bitmap GrayScal (bitmap orgbmp)
{
Create a large 8-bit image with the original image
Retrieve each vertex in the original image
New image points = original image points Red amount * coefficient 1 + green amount * coefficient 2 + yellow amount * system 3
Return New Image
}

Code              PointTrans    {           cb;           cg;           cr;                                                         PointTrans( cr,  cg,  cb)        {            .cr = cr;            .cg = cg;            .cb = cb;        }          Bitmap GrayScaleBmp(Bitmap orgData)        {             bmpWidth = orgData.Width, bmpHeight = orgData.Height;            Bitmap destData = ImageTools.CreateGrayscaleImage(bmpWidth, bmpHeight);            Rectangle bmpRect= Rectangle(0,0,bmpWidth,bmpHeight);            BitmapData orgBmpData = orgData.LockBits(bmpRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);            BitmapData destBmpData = destData.LockBits(bmpRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);            ProcessFilter(orgBmpData,destBmpData);            orgData.UnlockBits(orgBmpData);            destData.UnlockBits(destBmpData);             destData;        }           ProcessFilter(BitmapData sourceData, BitmapData destinationData)        {                         width = sourceData.Width;             height = sourceData.Height;             srcOffset = sourceData.Stride - width*3;             dstOffset = destinationData.Stride - width;                        * src = (*) sourceData.Scan0.ToPointer();            * dst = (*) destinationData.Scan0.ToPointer();                         ( y = 0; y < height; y++)            {                                 ( x = 0; x < width; x++, src += 3, dst++)                {                    *dst = () (cr*src[RGB.R] + cg*src[RGB.G] + cb*src[RGB.B]);                }                src += srcOffset;                dst += dstOffset;            }        }    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.