In C #, Image, Bitmap, and BitmapData,

Source: Internet
Author: User

In C #, Image, Bitmap, and BitmapData,

First, Image is an Image and cannot be instantiated. It provides functions for bitmap and source file operations. This article describes how to convert Bitmap into BitmapSource.

1 [DllImport ("gdi32")] 2 static extern int DeleteObject (IntPtr o ); 3 /// <summary> 4 // convert bitmap to bitmapsource to adapt to image 5 of wpf // </summary> 6 /// <param name = "pic"> </param> 7 // <returns> </returns> 8 public static BitmapSource GetMapSource (Bitmap pic) 9 {10 IntPtr ip = pic. getHbitmap (); 11 BitmapSource bitmapSource = System. windows. interop. imaging. createBitmapSourceFromHBitmap (12 ip, IntPtr. zero, Int32Rect. empty, 13 System. windows. media. imaging. bitmapSizeOptions. fromEmptyOptions (); 14 DeleteObject (ip); 15 return bitmapSource; 16}

Next we will talk about Bitmap and BitmapData.

Bitmap class
The Bitmap object encapsulates a Bitmap in GDI +, which is composed of the pixel data of the graphic image and its attributes. therefore, Bitmap is an object used to process images defined by pixel data. the main methods and attributes of this class are as follows:
1. GetPixel method and SetPixel method: Get and set the color of a specified pixel of an image.
2. PixelFormat attribute: return the pixel format of the image.
3. Palette attribute: gets and sets the color Palette used by the image.
4. Height Width attribute: returns the Height and Width of the image.
5. lockBits method and UnlockBits method: respectively lock and unlock bitmap pixels in the system memory. using LockBits and UnlockBits in pixel-based image processing is a good method. These two methods allow us to specify the pixel range to control any part of the bitmap, in this way, bitmap pixels are processed one by one through loops. Each time LockBits is called, UnlockBits should be called once.
BitmapData class
The BitmapData object specifies the properties of the bitmap.
1. Height attribute: the Height of the locked bitmap.
2. Width attribute: the height of the locked bitmap.
3. PixelFormat attribute: the actual pixel format of the data.
4. Scan0 attribute: the first byte address of the locked array. If the entire image is locked, it is the first byte address of the image.
5. Stride attribute: Stride, also known as scan width.

Here we will focus on the Stride attribute. What is the difference between this and Width? You can say that if your image size is an integer multiple of 4, the Stride and Width are equal. Otherwise, the Stride is an integer multiple that is greater than the minimum 4 of the Width. In the process, Stride must be an integer multiple of 4. This is a pitfall...

Steal images and connect them to the bottom of the article

First, let's take a look at the BitmapData application. My scenario is that I have a one-dimensional pixel dot matrix array with the gray value of each pixel in mind that the width and height should be converted to bitmap.

1 /// <summary> 2 // convert the pixel lattice to bitmap 3 /// </summary> 4 /// <param name = "rawValues"> byte [] Array </param> 5 // <param name = "width"> image width </param> 6 /// <param name = "height"> image height </param name = "height">/ param> 7 // <returns> bitmap image </returns> 8 public static Bitmap ToGrayBitmap (byte [] rawValues, int width, int height) 9 {10 Bitmap bmp = new Bitmap (width, height, System. drawing. imaging. pixelFormat. format8bppIndexed); 11 BitmapData bmp DATA = bmp. lockBits (new System. drawing. rectangle (0, 0, width, height), ImageLockMode. writeOnly, System. drawing. imaging. pixelFormat. format8bppIndexed); 12 // get image parameter 13 // BMP data. stride = width; 14 int stride = BMP data. stride; // scan line width 15 int offset = stride-width; // gap between display width and scan line width 16 IntPtr iptr = BMP data. scan0; // the starting position of the memory for obtaining the BMP data 17 int scanBytes = stride * height; // use the stride width, indicates that this is the size of the memory area 18 /// the original display size byte array is converted to the actually stored byte array in the memory 19 int posScan = 0, posReal = 0; // set two position pointers respectively, pointing to the source array and the target array 20 byte [] pixelValues = new byte [scanBytes]; // allocate memory 21 for the target array (int x = 0; x 

This code is also found on the Internet and has been forgotten. As for the 24-Bit Bitmap data, a pixel has three rgb values.

Similarly, we can obtain its grayscale Array Based on the image.

1 // an 8-Bit Bitmap is used to obtain a grayscale array 2 3 4 BitmapData bmp data = map. lockBits (new System. drawing. rectangle (0, 0, map. width, map. height), ImageLockMode. readOnly, System. drawing. imaging. pixelFormat. format8bppIndexed); 5 6 // obtain the image parameter 7 8 int stride = BMP data. stride; // scan line width 9 10 int offset = stride-map. width; // gap between display Width and scan Width 11 12 IntPtr iptr = BMP data. scan0; // get the starting position of the memory for the BMP data 13 14 int scanBytes = stride * map. height; // use stride width, this indicates that the size of the memory area is 15 16 /// the original display size byte array is converted to the actual storage byte array 17 18 mapdata = new byte [scanBytes] below; // allocate memory 19 20 System for the target array. runtime. interopServices. marshal. copy (iptr, mapdata, 0, scanBytes); // copy the data in the memory to the array

Here, the Operation Method for bitmapdata is ReadOnly.

Why is stride a pitfall? Because at work, I have a file with a size not 4 as an integer multiple, convert them into images through the above method, and then I need to save it back after the operation, continue to save it as a file. If you save it directly, you will find that your file has become larger. In this case, stride needs to be avoided. In fact, the space occupied by stride has nothing to do. How can we traverse and construct images, we can reverse traverse the array.

public static byte[] GetMapData(byte[] MapData,int width,int height){    var length = MapData.Length;    if(width==length/height)    {        return MapData;    }    int offset=length/height-width;    var scanBytes = width * height;    byte[] RawMapData = new byte[scanBytes];    int posScan = 0, posReal = 0;    for(int x=0;x

As for the conversion of 24-Bit Bitmap to 8-Bit Bitmap, I still think opencv is quick and convenient.

Http://blog.csdn.net/jiangxinyu/article/details/6222302

In addition, you can see how c # processes images, such as lighting, atomization, and relief.

Http://www.pin5i.com/showtopic-20228.html

The best time to plant a tree is ten years ago, followed by the present.

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.