A detailed description of grayscale technology in C #

Source: Internet
Author: User

Last year bought this Digital image processing algorithm, has not seen, the first few weeks have been busy working on the work, take advantage of this stage leisurely, play a picture processing, this thing is still very interesting.

Before we do the user registration on the web, usually do a verification code, we all know to prevent violent registration, of course, mention the verification code we all know C # inside there is a bitmap class specifically used to deal with the picture, OK, this one from the simplest "image grayscale" speaking.


One: Image grayscale

As we all know, bitmaps are made up of a single pixel point, which may be red, orange, pink, and so on, which we all know are represented by RGB.

Each color component is a byte (0-255), so the average pixel point is 24 bits, of course there are 32 bits, 64 bits, when RGB is a different value between 0-255, then the pixel is rendered "colorful", and when RGB is the same value is, then the pixel is rendered "gray", If you have played CSS, you must know that giving a font color is usually #999999, #666666, #333333这些不同深度的灰色.


1. Calculation formula

How do we set a reasonable grayscale value? Of course, use the current RGB as a template, and then multiply the RGB by a reasonable weight is OK


The code is as follows:


Gary (i,j) =0.299*r (i,j) +0.587*g (i,j) +0.114*b (I,J);


2. Programming

With the formula, it is no problem to realize it. There is a getpixel/setpixel in the bitmap class that can get and set the current pixel point.

static void Main (string[] args) {  Bitmap Bitmap = new Bitmap (environment.currentdirectory + "//1.jpg");  for (int i = 0; i < bitmap. Width; i++)  {for    (int j = 0; J < bitmap. Height; J + +)    {      //Take picture the current pixel point      var color = bitmap. GetPixel (i, j);      var gray = (int) (color. R * 0.299 + color. G * 0.587 + color. B * 0.114);      Re-sets the current pixel point      bitmap. SetPixel (i, J, Color.FromArgb (Gray, Gray, Gray));    }  }  Bitmap. Save (environment.currentdirectory + "//2.jpg");}


3. Improvements

The above method is very simple, get/set is OK, of course, this is our point of view of the pixel point to consider the problem, it seems as long as O (N2) time can KO problem, but Get/set is far from O (1), based on performance considerations, we can have better methods, At this point we can stand in the perspective of the byte, but here we have to pay attention to a problem is: width=21px, a pixel occupies 3 bytes, but 21 pixels does not necessarily occupy 63 bytes, because the system based on performance considerations, in each row is stored in a "unused area", To make sure that the number of bytes in each row of a picture is a multiple of 4, how do you read the number of bits in a row?

C # There is a stride attribute that can be used to get it, very simple.

static void Main (string[] args) {Bitmap Bitmap = new Bitmap (environment.currentdirectory + "//1.jpg"); Defines a specified range of bounds for a rect that locks bitmap Rectangle rect = new Rectangle (0, 0, bitmap. Width, Bitmap.      Height); Add lock area pixel var bitmapData = bitmap. LockBits (Rect, Imagelockmode.readwrite, bitmap.      PixelFormat);      The first address of the bitmap is var ptr = bitmapdata.scan0; Stride: Scan line int len = bitmapdata.stride * Bitmap.      Height;      var bytes = new Byte[len];      The pixel value of the locked area is copied to the byte array in marshal.copy (PTR, bytes, 0, Len); for (int i = 0; i < bitmap. Height; i++) {for (int j = 0; J < bitmap. Width * 3; j = j + 3) {var color = bytes[i * Bitmapdata.stride + j + 2] * 0.299 + bytes[i * bitmapdat          A.stride + j + 1] * 0.597 + bytes[i * bitmapdata.stride + j] * 0.114; Bytes[i * bitmapdata.stride + j] = Bytes[i * Bitmapdata.stride + j + 1] = Bytes[i * BitmapData . Stride + j + 2] = (byte) COLor      }}//copy back to bitmap marshal.copy (bytes, 0, PTR, Len); Unlock bitmap.      Unlockbits (BitmapData); Bitmap.    Save (environment.currentdirectory + "//3.jpg"); }

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
A detailed description of grayscale technology in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23668.html






Related Content C # implements a method of converting 32-bit MD5 digest strings to 128-bit binary string C # Jsonhelper Operations Helper class, take a simple instance of a Windows system that uses C # to write a Bluetooth communication program. A queue (Quene) instance of C # data structure
C # Operations JSON method Rollup C # Implementation custom dictionary class instance C # Generate Word Document code example C # use GDI + to create a thumbnail instance

A detailed description of grayscale technology in C #

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.