Image, Bitmap, and BitmapData in C #

Source: Internet
Author: User

The first thing to say is that Image,image is an image that cannot be instantiated and provides functions for bitmap and source file manipulation. This article he is to soy sauce, here provides a bitmap turn into BitmapSource method.

1[DllImport ("GDI32")]2 Static extern intDeleteObject (IntPtr o);3 /// <summary>4 ///Bitmap converted to BitmapSource to accommodate the WPF image5 /// </summary>6 /// <param name= "pic" ></param>7 /// <returns></returns>8  Public Staticbitmapsource Getmapsource (Bitmap pic)9 {TenINTPTR IP =pic. Gethbitmap (); OneBitmapSource BitmapSource =System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap ( A IP, IntPtr.Zero, Int32rect.empty, - System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions ()); - DeleteObject (IP); the     returnBitmapSource; -}

Next, bitmap and BitmapData.

Bitmap class
The bitmap object encapsulates a bitmap in GDI + that consists of the pixel data of the graphics image and its properties. Therefore, bitmap is an object used to process images defined by pixel data. The main methods and properties of this class are as follows:
1. GetPixel method and SetPixel method: Gets and sets the color of the specified pixel of an image.
2. PixelFormat property: Returns the pixel format of the image.
3. Palette properties: Gets and sets the color palette used by the image.
4. Height Width Property: Returns the height and width of the image.
5. LockBits method and Unlockbits method: Lock and unlock the bit pixels in the system memory respectively. Using LockBits and unlockbits in pixel-based image processing methods is a good way to allow us to specify the extent of the pixel to control any part of the bitmap, eliminating the Cycles through the pixels of a bitmap, and should call lockbits after each call to Unlockbits.
BitmapData class
The BitmapData object specifies the properties of the bitmap
1. Height property: How high the bitmap is locked.
2. Width property: The height of the locked bitmap.
3. PixelFormat property: The actual pixel format of the data.
4. Scan0 property: The first byte address of an array that is locked, or the initial byte address of the image if the entire image is locked.
5. Stride Properties: Stride, also known as scan width.

Here to focus on the stride attribute, what is the difference between this and width, so to speak, if your picture size is the picture byte is an integer multiple of 4, then stride and width is equal, otherwise stride is greater than the width of the minimum 4 integer times. In the process, stride must be 4 of the integer times, here is a pit ah ...

Steal a picture, connect it at the bottom of the article

First look at the application of BitmapData, my scene is, I have a one-dimensional pixel lattice array, which is placed in the gray value of each pixel point, know the width and height, to convert to bitmap

1 /// <summary>2 ///pixel dot matrix converted to bitmap3 /// </summary>4 /// <param name= "Rawvalues" >byte[] Array</param>5 /// <param name= "width" >the width of the picture</param>6 /// <param name= "height" >the height of the picture</param>7 /// <returns>Bitmap Pictures</returns>8  Public StaticBitmap Tograybitmap (byte[] rawvalues,intWidthintheight)9 {TenBitmap BMP =NewBitmap (width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); OneBitmapData bmpdata = bmp. LockBits (NewSystem.Drawing.Rectangle (0,0, width, height), imagelockmode.writeonly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); A     ////Get Image parameters -     //bmpdata.stride = width; -     intStride = Bmpdata.stride;//width of scan line the     intoffset = stride-width;//gap between the width of the display and the width of the scan line -IntPtr iptr = bmpdata.scan0;//get the Bmpdata memory start location -     intScanbytes = Stride * height;//Use the stride width to indicate that this is the size of the memory area -     ////below converts the original display size byte array to the actual byte array in memory +     intPosscan =0, Posreal =0;//set two position pointers, pointing to source and destination arrays, respectively -     byte[] Pixelvalues =New byte[Scanbytes];//allocating memory for the destination array +      for(intx =0; x < height; X + +) A     { at         ////The following loop section is a simulated row scan -          for(inty =0; Y < width; y++) -         { -pixelvalues[posscan++] = rawvalues[posreal++]; -         } -Posscan + = offset;//The end of the line scan to move the target position pointer over the "GAP" in     } -     ////Copy the memory byte array you just obtained into bitmapdata using the marshal copy method toSystem.Runtime.InteropServices.Marshal.Copy (Pixelvalues,0, Iptr, scanbytes); +Bmp. Unlockbits (Bmpdata);//Unlock Memory Area -     ////The following code is to modify the index table of the generated bitmap from the pseudo-color to grayscale the ColorPalette Temppalette; *     using(Bitmap tempbmp =NewBitmap (1,1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed)) $     {Panax NotoginsengTemppalette =Tempbmp.palette; -     } the      for(inti =0; I < the; i++) +     { ATemppalette.entries[i] =System.Drawing.Color.FromArgb (i, I, i); the     } +  -Bmp. Palette =Temppalette; $  $     ////The algorithm ends here and returns the result -     returnbmp; -}

This code is also found on the Internet, where the specific has been forgotten. As for the 24-bit bitmap data is actually a pixel point has RGB three values, the same reason.

Similarly, we can also get his grey degree group according to the picture

1 //8-bit bitmap gets a gray-degree group that removes the header information from the file2 3 4BitmapData bmpdata = map. LockBits (NewSystem.Drawing.Rectangle (0,0, map. Width, map. Height), imagelockmode.readonly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);5 6 ////Get Image parameters7 8 intStride = Bmpdata.stride;//width of scan line9 Ten intoffset = Stride-map. Width;//gap between the width of the display and the width of the scan line One  AIntPtr iptr = bmpdata.scan0;//get the Bmpdata memory start location -  - intScanbytes = Stride * Map. Height;//Use the stride width to indicate that this is the size of the memory area the  - ////below converts the original display size byte array to the actual byte array in memory -  -MapData =New byte[Scanbytes];//allocating memory for the destination array +  -System.Runtime.InteropServices.Marshal.Copy (Iptr, MapData,0, scanbytes);//Copy the in- memory data into the array

Here is the way to operate with BitmapData is ReadOnly

Why say stride is a pit, because in the work, I have a size not 4 integer times the file, through the above method to convert them to pictures, and then after the operation I need to save back, continue to save in the form of files, if you save directly back you will find that your file has become larger. This is the time to avoid stride. In fact, the space occupied by stride nothing to do, how we traverse the construction of the image, how to back the array can be

 Public Static byte[] Getmapdata (byte[] MapData,intWidthintheight) {    varLength =mapdata.length; if(width==length/height) {        returnMapData; }    intoffset=length/height-width; varScanbytes = width *height; byte[] Rawmapdata =New byte[Scanbytes]; intPosscan =0, Posreal =0;  for(intx=0; x)    {         for(inty=0; y<width;y++) {Rawmapdata[posscan+ +] = mapdata[posreal++]; } posreal+=offset; }    returnRawmapdata;}

As for the 24-bit bitmap to 8-bit bitmap, or look at the blogger's blog, he summed up a lot, I still think OpenCV is relatively fast and convenient.

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

Also see the C # Processing of pictures, such as lighting, fog, relief, etc., please follow the link below

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

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

Image, Bitmap, and BitmapData in C #

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.